Skip to content

Commit

Permalink
build(scripts): replace go function name with predicate name
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Oct 23, 2023
1 parent ecbe016 commit 4cacc2d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
62 changes: 43 additions & 19 deletions scripts/generate_predicates_doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,19 @@ 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"
)

//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
}
Expand All @@ -56,6 +43,7 @@ func GeneratePredicateDocumentation() error {
if err != nil {
return err
}

content, err := out.Package(pkg)
if err != nil {
return err
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions scripts/templates/func.gotxt
Original file line number Diff line number Diff line change
@@ -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 -}}
Expand Down
9 changes: 6 additions & 3 deletions scripts/templates/text.gotxt
Original file line number Diff line number Diff line change
@@ -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 -}}
Expand Down

0 comments on commit 4cacc2d

Please sign in to comment.