Skip to content

Commit

Permalink
replace go-term-markdown with glamour, pre-render Markdown files to r…
Browse files Browse the repository at this point in the history
…educe runtime dependencies, revamp docs
  • Loading branch information
xrstf committed Dec 13, 2023
1 parent 018d3d0 commit 00deb39
Show file tree
Hide file tree
Showing 250 changed files with 5,008 additions and 1,602 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ generate:

.PHONY: docs
docs:
go run hack/docs-toc/main.go
cd hack/docs-toc && go run .
cd hack/docs-prerender && go run .

.PHONY: clean
clean:
Expand All @@ -32,6 +33,7 @@ clean:
build:
mkdir -p _build
cd cmd/rudi && go build $(GO_BUILD_FLAGS) -o ../../_build .
cd cmd/example && go build $(GO_BUILD_FLAGS) -o ../../_build .

.PHONY: install
install:
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ like those available in JSON (numbers, bools, objects, vectors etc.). A statemen
* [Documentation](#documentation)
* [Language Description](docs/language.md)
* [Type Handling](docs/coalescing.md)
* [Standard Library](docs/functions/README.md)
* [Extended Library](docs/modules.md)
* [Standard Library](docs/stdlib/README.md)
* [Extended Library](docs/extlib/README.md)
* [Usage](#usage)
* [Command Line](#command-line)
* [Embedding](#embedding)
Expand Down Expand Up @@ -75,8 +75,8 @@ from GitHub.
Make yourself familiar with Rudi using the documentation:

* The [Language Description](docs/language.md) describes the Rudi syntax and semantics.
* All built-in functions are described in the [standard library](docs/functions/README.md).
* Additional functions are available in the [extended library](docs/modules.md).
* All built-in functions are described in the [standard library](docs/stdlib/README.md).
* Additional functions are available in the [extended library](docs/extlib/README.md).
* [Type Handling](docs/coalescing.md) describes how Rudi handles, converts and compares values.

## Usage
Expand Down Expand Up @@ -132,6 +132,7 @@ import (
"log"

"go.xrstf.de/rudi"
"go.xrstf.de/rudi/pkg/coalescing"
)

const script = `(set! .foo 42) (+ $myvar 42 .foo)`
Expand All @@ -143,7 +144,7 @@ func main() {
documentData := map[string]any{"foo": 9000}

// parse the script (the name is used when generating error strings)
program, err := rudi.ParseScript("myscript", script)
program, err := rudi.Parse("myscript", script)
if err != nil {
log.Fatalf("The script is invalid: %v", err)
}
Expand Down Expand Up @@ -198,7 +199,7 @@ out there, allbeit with slightly different ideas and goals than Rudi:
* [Go Templates](https://pkg.go.dev/text/template) – I really don't like Go's template syntax for
more than simple one-liners. I liked and copied its concept of ranging over things, as templates
do not allow unbounded loops (just like Rudi), but apart from being safe to embed, Go templates do
not offer enough functionality to modify a data structure. Like Jsonnet, templates shine whe
not offer enough functionality to modify a data structure. Like Jsonnet, templates shine when
creating/outputting entire _new_ documents.

_Bonus mention:_ Mastermind's [sprig](https:/Masterminds/sprig) served as inspiration
Expand Down
2 changes: 1 addition & 1 deletion aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewFunctions() Functions {

// NewBuiltInFunctions returns a copy of all the built-in Rudi functions.
func NewBuiltInFunctions() Functions {
return builtin.AllFunctions.DeepCopy()
return builtin.Functions.DeepCopy()
}

// NewVariables returns an empty set of runtime variables.
Expand Down
7 changes: 7 additions & 0 deletions cmd/example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module go.xrstf.de/rudi/cmd/example

go 1.18

require go.xrstf.de/rudi v0.2.1-0.20231205152938-7272c971e798

replace go.xrstf.de/rudi => ../../
1 change: 1 addition & 0 deletions cmd/example/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
48 changes: 48 additions & 0 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"log"

"go.xrstf.de/rudi"
"go.xrstf.de/rudi/pkg/coalescing"
)

const script = `(set! .foo 42) (+ $myvar 42 .foo)`

func main() {
// Rudi programs are meant to manipulate a document (path expressions like
// ".foo" resolve within that document). The document can be anything,
// but is most often a JSON object.
documentData := map[string]any{"foo": 9000}

// parse the script (the name is used when generating error strings)
program, err := rudi.Parse("myscript", script)
if err != nil {
log.Fatalf("The script is invalid: %v", err)
}

// evaluate the program;
// this returns an evaluated value, which is the result of the last expression
// that was evaluated, plus the final document state (the updatedData) after
// the script has finished.
updatedData, result, err := program.Run(
documentData,
// setup the set of variables available by default in the script
rudi.NewVariables().Set("myvar", 42),
// Likewise, setup the functions available (note that this includes
// functions like "if" and "and", so running with an empty function set
// is generally not advisable).
rudi.NewBuiltInFunctions(),
// Decide what kind of type strictness you would like; pedantic, strict
// or humane; choose your own adventure (strict is default if you use nil
// here; humane allows conversions like 1 == "1").
coalescing.NewStrict(),
)
if err != nil {
log.Fatalf("Script failed: %v", err)
}

fmt.Println(result) // => 126
fmt.Println(updatedData) // => {"foo": 42}
}
118 changes: 118 additions & 0 deletions cmd/rudi/batteries/modules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// SPDX-FileCopyrightText: 2023 Christoph Mewes
// SPDX-License-Identifier: MIT

package batteries

import (
coalescemod "go.xrstf.de/rudi/pkg/builtin/coalesce"
coalescedocs "go.xrstf.de/rudi/pkg/builtin/coalesce/docs"
comparemod "go.xrstf.de/rudi/pkg/builtin/compare"
comparedocs "go.xrstf.de/rudi/pkg/builtin/compare/docs"
coremod "go.xrstf.de/rudi/pkg/builtin/core"
coredocs "go.xrstf.de/rudi/pkg/builtin/core/docs"
datetimemod "go.xrstf.de/rudi/pkg/builtin/datetime"
datetimedocs "go.xrstf.de/rudi/pkg/builtin/datetime/docs"
encodingmod "go.xrstf.de/rudi/pkg/builtin/encoding"
encodingdocs "go.xrstf.de/rudi/pkg/builtin/encoding/docs"
hashingmod "go.xrstf.de/rudi/pkg/builtin/hashing"
hashingdocs "go.xrstf.de/rudi/pkg/builtin/hashing/docs"
listsmod "go.xrstf.de/rudi/pkg/builtin/lists"
listsdocs "go.xrstf.de/rudi/pkg/builtin/lists/docs"
logicmod "go.xrstf.de/rudi/pkg/builtin/logic"
logicdocs "go.xrstf.de/rudi/pkg/builtin/logic/docs"
mathmod "go.xrstf.de/rudi/pkg/builtin/math"
mathdocs "go.xrstf.de/rudi/pkg/builtin/math/docs"
stringsmod "go.xrstf.de/rudi/pkg/builtin/strings"
stringsdocs "go.xrstf.de/rudi/pkg/builtin/strings/docs"
typesmod "go.xrstf.de/rudi/pkg/builtin/types"
typesdocs "go.xrstf.de/rudi/pkg/builtin/types/docs"
"go.xrstf.de/rudi/pkg/docs"

semvermod "go.xrstf.de/rudi-contrib/semver"
semverdocs "go.xrstf.de/rudi-contrib/semver/docs"
uuidmod "go.xrstf.de/rudi-contrib/uuid"
uuiddocs "go.xrstf.de/rudi-contrib/uuid/docs"
yamlmod "go.xrstf.de/rudi-contrib/yaml"
yamldocs "go.xrstf.de/rudi-contrib/yaml/docs"
)

var (
// BuiltInModules look alphabetically sorted, except that "core" is always the first item,
// because it's the most important module and should be first in the documentation. Order here
// does not matter otherwise anyway.
BuiltInModules = []docs.Module{
{
Name: "core",
Functions: coremod.Functions,
Documentation: coredocs.Functions,
},
{
Name: "coalesce",
Functions: coalescemod.Functions,
Documentation: coalescedocs.Functions,
},
{
Name: "compare",
Functions: comparemod.Functions,
Documentation: comparedocs.Functions,
},
{
Name: "datetime",
Functions: datetimemod.Functions,
Documentation: datetimedocs.Functions,
},
{
Name: "encoding",
Functions: encodingmod.Functions,
Documentation: encodingdocs.Functions,
},
{
Name: "hashing",
Functions: hashingmod.Functions,
Documentation: hashingdocs.Functions,
},
{
Name: "lists",
Functions: listsmod.Functions,
Documentation: listsdocs.Functions,
},
{
Name: "logic",
Functions: logicmod.Functions,
Documentation: logicdocs.Functions,
},
{
Name: "math",
Functions: mathmod.Functions,
Documentation: mathdocs.Functions,
},
{
Name: "strings",
Functions: stringsmod.Functions,
Documentation: stringsdocs.Functions,
},
{
Name: "types",
Functions: typesmod.Functions,
Documentation: typesdocs.Functions,
},
}

ExtendedModules = []docs.Module{
{
Name: "semver",
Functions: semvermod.Functions,
Documentation: semverdocs.Functions,
},
{
Name: "uuid",
Functions: uuidmod.Functions,
Documentation: uuiddocs.Functions,
},
{
Name: "yaml",
Functions: yamlmod.Functions,
Documentation: yamldocs.Functions,
},
}
)
34 changes: 16 additions & 18 deletions cmd/rudi/cmd/console/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,47 @@
package console

import (
_ "embed"
"errors"
"fmt"
"strings"

"go.xrstf.de/rudi"
"go.xrstf.de/rudi/cmd/rudi/docs"
cmdtypes "go.xrstf.de/rudi/cmd/rudi/types"
"go.xrstf.de/rudi/cmd/rudi/util"
"go.xrstf.de/rudi/docs"
"go.xrstf.de/rudi/pkg/eval/types"

colorjson "github.com/TylerBrock/colorjson"
"github.com/chzyer/readline"
)

//go:embed help.md
var helpText string

func printPrompt() {
fmt.Print("⮞ ")
}

func helpCommand(ctx types.Context, helpTopics []docs.Topic, opts *cmdtypes.Options) error {
fmt.Println(util.RenderMarkdown(strings.TrimSpace(helpText), 2))
fmt.Println(util.RenderHelpTopics(helpTopics, 2))
func helpCommand(ctx types.Context, opts *cmdtypes.Options) error {
content, err := docs.RenderFile("cmd-console.md", nil)
if err != nil {
return err
}

fmt.Print(content)

return nil
}

func helpTopicCommand(helpTopics []docs.Topic, topic string) error {
rendered, err := util.RenderHelpTopic(helpTopics, topic, 2)
func helpTopicCommand(topic string) error {
rendered, err := util.RenderHelpTopic(topic, 2)
if err != nil {
return err
}

fmt.Println(rendered)
fmt.Print(rendered)

return nil
}

type replCommandFunc func(ctx types.Context, helpTopics []docs.Topic, opts *cmdtypes.Options) error
type replCommandFunc func(ctx types.Context, opts *cmdtypes.Options) error

var replCommands = map[string]replCommandFunc{
"help": helpCommand,
Expand All @@ -70,8 +70,6 @@ func Run(opts *cmdtypes.Options, args []string, rudiVersion string) error {
fmt.Println("Type `help` fore more information, `exit` or Ctrl-C to exit.")
fmt.Println("")

helpTopics := docs.Topics()

for {
line, err := rl.Readline()
if err != nil { // io.EOF
Expand All @@ -82,7 +80,7 @@ func Run(opts *cmdtypes.Options, args []string, rudiVersion string) error {
continue
}

newCtx, stop, err := processInput(ctx, helpTopics, opts, line)
newCtx, stop, err := processInput(ctx, opts, line)
if err != nil {
parseErr := &rudi.ParseError{}
if errors.As(err, parseErr) {
Expand All @@ -104,14 +102,14 @@ func Run(opts *cmdtypes.Options, args []string, rudiVersion string) error {
return nil
}

func processInput(ctx types.Context, helpTopics []docs.Topic, opts *cmdtypes.Options, input string) (newCtx types.Context, stop bool, err error) {
func processInput(ctx types.Context, opts *cmdtypes.Options, input string) (newCtx types.Context, stop bool, err error) {
if command, exists := replCommands[input]; exists {
return ctx, false, command(ctx, helpTopics, opts)
return ctx, false, command(ctx, opts)
}

if prefix := "help "; strings.HasPrefix(input, prefix) {
topicName := strings.TrimPrefix(input, prefix)
return ctx, false, helpTopicCommand(helpTopics, topicName)
return ctx, false, helpTopicCommand(topicName)
}

if strings.EqualFold("exit", input) {
Expand Down
Loading

0 comments on commit 00deb39

Please sign in to comment.