-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ab8756
commit 2568f54
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |