Skip to content

Commit

Permalink
chore: split and organize files
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenhilton committed May 14, 2024
1 parent e3205f8 commit 14dc9e2
Show file tree
Hide file tree
Showing 16 changed files with 633 additions and 517 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491
- uses: golangci/golangci-lint-action@9d1e0624a798bb64f6c3cea93db47765312263dc
with:
version: v1.55.2
version: v1.58.1
- uses: DavidAnson/markdownlint-cli2-action@b4c9feab76d8025d1e83c653fa3990936df0e6c8
with:
globs: '**/*.md'
Expand Down
5 changes: 2 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ linters:
- dupl
- dupword
- durationcheck
- err113
- errcheck
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
- exportloopref
- forbidigo
Expand All @@ -30,7 +30,6 @@ linters:
- gocritic
- gocyclo
- godot
- goerr113
- gofmt
- gofumpt
- goheader
Expand Down Expand Up @@ -136,7 +135,7 @@ issues:
- gosec
path: "internal/"
- linters:
- goerr113
- err113
text: do not define dynamic errors, use wrapped static errors instead
- linters:
- gochecknoinits
Expand Down
12 changes: 12 additions & 0 deletions docs/booleanfuncs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Boolean Functions

## `eqFold` *string1* *string2* [*extraStrings*...]

`eqFold` returns the boolean truth of comparing *string1* with *string2*
and any number of *extraStrings* under Unicode case-folding.

```text
{{ eqFold "föö" "FOO" }}
true
```
47 changes: 47 additions & 0 deletions docs/conversionfuncs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Conversion Functions

## `fromJSON` *jsontext*

`fromJSON` parses *jsontext* as JSON and returns the parsed value.

```text
{{ `{ "foo": "bar" }` | fromJSON }}
```

## `hexDecode` *hextext*

`hexDecode` returns the bytes represented by *hextext*.

```text
{{ hexDecode "666f6f626172" }}
foobar
```

## `hexEncode` *string*

`hexEncode` returns the hexadecimal encoding of *string*.

```text
{{ hexEncode "foobar" }}
666f6f626172
```

## `toJSON` *input*

`toJSON` returns a JSON string representation of *input*.

```text
{{ list "foo" "bar" "baz" }}
["foo","bar","baz"]
```

## `toString` *input*

`toString` returns the string representation of *input*.

```text
{{ toString 10 }}
```
67 changes: 55 additions & 12 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,75 @@
package docs

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

//go:embed templatefuncs.md
var templateFuncsStr string

type Reference struct {
Type string
Title string
Body string
Example string
}

var References map[string]Reference
//go:embed *.md
var f embed.FS

func init() {
newlineRx := regexp.MustCompile(`\r?\n`)
var (
References map[string]Reference

newlineRx = regexp.MustCompile(`\r?\n`)
pageTitleRx = regexp.MustCompile(`^#\s+(\S+)`)
// 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*)` + "`")
funcNameRx = regexp.MustCompile("`" + `([a-zA-Z_]\w*)` + "`")
)

References = make(map[string]Reference)
func readFiles() []string {
fileContents := []string{}

fileInfos, err := f.ReadDir(".")
if err != nil {
log.Fatal(err)
}

for _, fileInfo := range fileInfos {
if fileInfo.IsDir() || !strings.HasSuffix(fileInfo.Name(), ".md") {
continue
}
content, err := f.ReadFile(fileInfo.Name())
if err != nil {
log.Fatal(err)
}
fileContents = append(fileContents, string(content))
}

return fileContents
}

func parseFile(file string) map[string]Reference {
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) {
lines := newlineRx.Split(file, -1)
funcType, lines := pageTitleRx.FindStringSubmatch(lines[0])[1], lines[1:]

for _, line := range lines {
switch {
case strings.HasPrefix(line, "## "):
if reference.Title != "" {
References[funcName] = reference
references[funcName] = reference
}
funcName = funcNameRx.FindStringSubmatch(line)[1]
reference = Reference{}
reference.Type = funcType
reference.Title = strings.TrimPrefix(line, "## ")
case strings.HasPrefix(line, "```"):
if !inExample {
Expand All @@ -63,6 +94,18 @@ func init() {
}

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

return references
}

func init() {
References = make(map[string]Reference)

files := readFiles()

for _, file := range files {
maps.Copy(References, parseFile(file))
}
}
17 changes: 11 additions & 6 deletions docs/docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

func TestReferences(t *testing.T) {
assert.Equal(t, docs.Reference{
Type: "String",
Title: "`contains` *substring* *string*",
Body: "`contains` returns whether *substring* is in *string*.",
Example: "" +
Expand All @@ -20,12 +21,16 @@ func TestReferences(t *testing.T) {
"```",
}, docs.References["contains"])
assert.Equal(t, docs.Reference{
Title: "`trimSpace` *string*",
Body: "`trimSpace` returns *string* with all spaces removed.",
Example: "```text\n" +
"{{ \" foobar \" | trimSpace }}\n" +
Type: "Boolean",
Title: "`eqFold` *string1* *string2* [*extraStrings*...]",
Body: "" +
"`eqFold` returns the boolean truth of comparing *string1* with *string2*\n" +
"and any number of *extraStrings* under Unicode case-folding.",
Example: "" +
"```text\n" +
"{{ eqFold \"föö\" \"FOO\" }}\n" +
"\n" +
"foobar\n" +
"true\n" +
"```",
}, docs.References["trimSpace"])
}, docs.References["eqFold"])
}
32 changes: 32 additions & 0 deletions docs/listfuncs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# List Functions

## `join` *delimiter* *list*

`join` returns a string containing each item in *list* joined with *delimiter*.

```text
{{ list "foo" "bar" "baz" | join "," }}
foo,bar,baz
```

## `list` *items*...

`list` creates a new list containing *items*.

```text
{{ list "foo" "bar" "baz" }}
```

## `prefixLines` *prefix* *list*

`prefixLines` returns a string consisting of each item in *list*
with the prefix *prefix*.

```text
{{ list "this is" "a multi-line" "comment" | prefixLines "# " }}
# this is
# a multi-line
# comment
```
83 changes: 83 additions & 0 deletions docs/stringfuncs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# String Functions

## `contains` *substring* *string*

`contains` returns whether *substring* is in *string*.

```text
{{ "abc" | contains "ab" }}
true
```

## `hasPrefix` *prefix* *string*

`hasPrefix` returns whether *string* begins with *prefix*.

```text
{{ "foobar" | hasPrefix "foo" }}
true
```

## `hasSuffix` *suffix* *string*

`hasSuffix` returns whether *string* ends with *suffix*.

```text
{{ "foobar" | hasSuffix "bar" }}
true
```

## `quote` *input*

`quote` returns a double-quoted string literal containing *input*.
*input* can be a string or list of strings.

```text
{{ "foobar" | quote }}
"foobar"
```

## `regexpReplaceAll` *pattern* *replacement* *string*

`regexpReplaceAll` replaces all instances of *pattern*
with *replacement* in *string*.

```text
{{ "foobar" | regexpReplaceAll "o*b" "" }}
far
```

## `toLower` *string*

`toLower` returns *string* with all letters converted to lower case.

```text
{{ toLower "FOOBAR" }}
foobar
```

## `toUpper` *string*

`toUpper` returns *string* with all letters converted to upper case.

```text
{{ toUpper "foobar" }}
FOOBAR
```

## `trimSpace` *string*

`trimSpace` returns *string* with all spaces removed.

```text
{{ " foobar " | trimSpace }}
foobar
```
Loading

0 comments on commit 14dc9e2

Please sign in to comment.