Skip to content

Commit

Permalink
fix and improve docs alias handling (closes #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
xrstf committed Dec 21, 2023
1 parent e84576d commit 2387c48
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
14 changes: 14 additions & 0 deletions cmd/rudi/docs/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import (
"go.xrstf.de/rudi/pkg/docs"
)

var Aliases = map[string]string{
// math module

"+": "add",
"-": "sub",
"*": "mult",
"/": "div",
}

//go:embed data/*
var embeddedFS embed.FS

Expand Down Expand Up @@ -49,6 +58,11 @@ func RenderFile(filename string, painter PainterFunc) (string, error) {
}

func ReadFunction(funcName string) (string, error) {
realName, ok := Aliases[funcName]
if ok {
funcName = realName
}

return ReadFile(fmt.Sprintf("functions/%s.md", docs.Normalize(funcName)))
}

Expand Down
6 changes: 6 additions & 0 deletions hack/docs-prerender/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"

"go.xrstf.de/rudi/cmd/rudi/batteries"
rudidocs "go.xrstf.de/rudi/cmd/rudi/docs"
"go.xrstf.de/rudi/hack/docs-prerender/ansidoc"
"go.xrstf.de/rudi/pkg/docs"
)
Expand Down Expand Up @@ -131,6 +132,11 @@ func dumpModules(mods []docs.Module, library string, wipe bool) {
}

for funcName := range mod.Functions {
// ignore aliases (mostly math aliases like "+" or "/")
if _, ok := rudidocs.Aliases[funcName]; ok {
continue
}

doc, err := mod.Documentation.Documentation(funcName)
if err != nil {
log.Printf("Warning: failed to get docs for %s/%s: %v", mod.Name, funcName, err)
Expand Down
11 changes: 7 additions & 4 deletions hack/docs-toc/toc-library.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sort"
"strings"

rudidocs "go.xrstf.de/rudi/cmd/rudi/docs"
"go.xrstf.de/rudi/pkg/docs"
)

Expand All @@ -20,8 +21,9 @@ func renderLibraryTOC(lib []docs.Module, linkPrefix string) string {

functions := []string{}
for funcName := range module.Functions {
// Hack: ignore math function aliases
if funcName == "+" || funcName == "-" || funcName == "*" || funcName == "/" {
// Hack: ignore aliases, mostly because the math functions have names
// that are incompatible with the filesystem (cannot have a "/.md" file).
if _, exists := rudidocs.Aliases[funcName]; exists {
continue
}

Expand Down Expand Up @@ -53,8 +55,9 @@ func renderHelpLibraryTOC(lib []docs.Module) string {

functions := []string{}
for funcName := range module.Functions {
// Hack: ignore math function aliases
if funcName == "+" || funcName == "-" || funcName == "*" || funcName == "/" {
// Hack: ignore aliases, mostly because the math functions have names
// that are incompatible with the filesystem (cannot have a "/.md" file).
if _, exists := rudidocs.Aliases[funcName]; exists {
continue
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/builtin/math/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ var (
divideRudiFunction = functions.NewBuilder(numberDivFunction).WithDescription("returns arg1 / arg2 / .. / argN (always a floating point division, regardless of arguments)").Build()

Functions = types.Functions{
// These are the main functions, but within the documentation these are
// considered to be aliases because their names cannot be used in Markdown
// filenames.
"+": addRudiFunction,
"-": subRudiFunction,
"*": multiplyRudiFunction,
Expand Down

0 comments on commit 2387c48

Please sign in to comment.