Skip to content

Commit

Permalink
docs: Embed docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenhilton committed Mar 4, 2024
1 parent 5ab8756 commit 2568f54
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,6 @@ issues:
- linters:
- goerr113
text: do not define dynamic errors, use wrapped static errors instead
- linters:
- gochecknoinits
path: "docs/docs.go"
68 changes: 68 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package docs

import (
_ "embed"
"regexp"
"strings"
)

//go:embed templatefuncs.md
var templateFuncsStr string

type Reference struct {
Title string
Body string
Example string
}

var References map[string]Reference

func init() {
newlineRx := regexp.MustCompile(`\r?\n`)

// Template function names must start with a letter or underscore
// and can subsequently contain letters, underscores and digits.
funcNameRx := regexp.MustCompile("`" + `([a-zA-Z_]\w*)` + "`")

References = make(map[string]Reference)
var reference Reference
var funcName string
var b strings.Builder
var e strings.Builder
inExample := false

for _, line := range newlineRx.Split(templateFuncsStr, -1) {
switch {
case strings.HasPrefix(line, "## "):
if reference.Title != "" {
References[funcName] = reference
}
funcName = funcNameRx.FindStringSubmatch(line)[1]
reference = Reference{}
reference.Title = strings.TrimPrefix(line, "## ")
case strings.HasPrefix(line, "```"):
if !inExample {
reference.Body = strings.TrimSpace(b.String())
b.Reset()
}
e.WriteString(line + "\n")
if inExample {
reference.Example = strings.TrimSpace(e.String())
e.Reset()
}
inExample = !inExample
case inExample:
if _, err := e.WriteString(line + "\n"); err != nil {
panic(err)
}
case reference.Title != "":
if _, err := b.WriteString(line + "\n"); err != nil {
panic(err)
}
}
}

if reference.Title != "" {
References[funcName] = reference
}
}

0 comments on commit 2568f54

Please sign in to comment.