From 4cacc2d9bcf6cf8578295aa51837d78df112058b Mon Sep 17 00:00:00 2001 From: ccamel Date: Mon, 23 Oct 2023 19:31:11 +0200 Subject: [PATCH] build(scripts): replace go function name with predicate name --- scripts/generate_predicates_doc.go | 62 +++++++++++++++++++++--------- scripts/templates/func.gotxt | 7 +++- scripts/templates/text.gotxt | 9 +++-- 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/scripts/generate_predicates_doc.go b/scripts/generate_predicates_doc.go index 8a963413..e0ea555b 100644 --- a/scripts/generate_predicates_doc.go +++ b/scripts/generate_predicates_doc.go @@ -11,7 +11,6 @@ import ( "github.com/Masterminds/sprig/v3" "github.com/princjef/gomarkdoc" - "github.com/princjef/gomarkdoc/format" "github.com/princjef/gomarkdoc/lang" "github.com/princjef/gomarkdoc/logger" ) @@ -19,24 +18,12 @@ import ( //go:embed templates/*.gotxt var f embed.FS +// globalCtx used to keep track of contexts between templates. +// (yes it's a hack). +var globalCtx = make(map[string]interface{}) + func GeneratePredicateDocumentation() error { - out, err := gomarkdoc.NewRenderer( - gomarkdoc.WithTemplateOverride("text", readTemplateMust("text.gotxt")), - gomarkdoc.WithTemplateOverride("doc", readTemplateMust("doc.gotxt")), - gomarkdoc.WithTemplateOverride("list", readTemplateMust("list.gotxt")), - gomarkdoc.WithTemplateOverride("import", ""), - gomarkdoc.WithTemplateOverride("package", readTemplateMust("package.gotxt")), - gomarkdoc.WithTemplateOverride("file", ""), - gomarkdoc.WithTemplateOverride("func", readTemplateMust("func.gotxt")), - gomarkdoc.WithTemplateOverride("index", ""), - gomarkdoc.WithTemplateFunc("snakecase", sprig.TxtFuncMap()["snakecase"]), - gomarkdoc.WithTemplateFunc("hasSuffix", sprig.TxtFuncMap()["hasSuffix"]), - gomarkdoc.WithTemplateFunc("countSubstr", func(substr string, s string) int { - return strings.Count(s, substr) - }), - gomarkdoc.WithTemplateFunc("dict", sprig.TxtFuncMap()["dict"]), - gomarkdoc.WithFormat(&format.GitHubFlavoredMarkdown{}), - ) + out, err := createRenderer() if err != nil { return err } @@ -56,6 +43,7 @@ func GeneratePredicateDocumentation() error { if err != nil { return err } + content, err := out.Package(pkg) if err != nil { return err @@ -64,6 +52,40 @@ func GeneratePredicateDocumentation() error { return writeToFile("docs/predicate/predicates.md", content) } +func createRenderer() (*gomarkdoc.Renderer, error) { + templateFunctionOpts := make([]gomarkdoc.RendererOption, 0) + + templateFunctionOpts = append( + templateFunctionOpts, + gomarkdoc.WithTemplateOverride("text", readTemplateMust("text.gotxt")), + gomarkdoc.WithTemplateOverride("doc", readTemplateMust("doc.gotxt")), + gomarkdoc.WithTemplateOverride("list", readTemplateMust("list.gotxt")), + gomarkdoc.WithTemplateOverride("import", ""), + gomarkdoc.WithTemplateOverride("package", readTemplateMust("package.gotxt")), + gomarkdoc.WithTemplateOverride("file", ""), + gomarkdoc.WithTemplateOverride("func", readTemplateMust("func.gotxt")), + gomarkdoc.WithTemplateOverride("index", ""), + ) + + for k, v := range sprig.GenericFuncMap() { + templateFunctionOpts = append( + templateFunctionOpts, + gomarkdoc.WithTemplateFunc(k, v), + ) + } + + templateFunctionOpts = append( + templateFunctionOpts, + gomarkdoc.WithTemplateFunc("countSubstr", func(substr string, s string) int { + return strings.Count(s, substr) + }), + gomarkdoc.WithTemplateFunc("globalCtx", func() map[string]interface{} { + return globalCtx + }), + ) + return gomarkdoc.NewRenderer(templateFunctionOpts...) +} + func readTemplateMust(templateName string) string { template, err := f.ReadFile("templates/" + templateName) if err != nil { @@ -78,7 +100,9 @@ func writeToFile(filePath string, content string) error { if err != nil { return fmt.Errorf("failed to create file %s: %w", filePath, err) } - defer file.Close() + defer func(file *os.File) { + _ = file.Close() + }(file) w := bufio.NewWriter(file) if _, err = w.WriteString(content); err != nil { diff --git a/scripts/templates/func.gotxt b/scripts/templates/func.gotxt index cbc01aa5..9bd6d7c4 100644 --- a/scripts/templates/func.gotxt +++ b/scripts/templates/func.gotxt @@ -1,7 +1,10 @@ {{- $name := snakecase .Name -}} -{{ $arity := add (countSubstr "," .Signature) -2 }} +{{- $arity := sub (countSubstr "," .Signature) 2 -}} +{{- $predicate := print $name "/" $arity -}} +{{- $_ := set globalCtx "funcName" .Name -}} +{{- $_ := set globalCtx "predicate" $predicate -}} -## {{ $name -}}/{{- $arity -}} +## {{ $predicate -}} {{- spacer -}} {{- template "doc" .Doc -}} diff --git a/scripts/templates/text.gotxt b/scripts/templates/text.gotxt index 1b93a197..8c2c1851 100644 --- a/scripts/templates/text.gotxt +++ b/scripts/templates/text.gotxt @@ -1,10 +1,13 @@ +{{- $funcName := get globalCtx "funcName" -}} +{{- $predicate := get globalCtx "predicate" -}} + {{- range . -}} {{- if eq .Kind "text" -}} - {{- escape .Text -}} + {{- escape .Text | replace $funcName $predicate -}} {{- else if eq .Kind "rawText" -}} - {{- .Text -}} + {{- .Text | replace $funcName $predicate -}} {{- else if eq .Kind "autolink" -}} - {{- .Text -}} + {{- .Text | replace $funcName $predicate -}} {{- else if eq .Kind "link" -}} {{- link (escape .Text) .URL -}} {{- end -}}