Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: split and organize files #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 5 additions & 6 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 @@ -130,14 +129,14 @@ issues:
- linters:
- dupl
- scopelint
path: "_test\\.go"
path: _test\.go
- linters:
- forbidigo
- gosec
path: "internal/"
path: internal/
- linters:
- goerr113
- err113
text: do not define dynamic errors, use wrapped static errors instead
- linters:
- gochecknoinits
path: "docs/docs.go"
path: docs\.go
26 changes: 26 additions & 0 deletions booleanfuncs/booleanfuncs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package booleanfuncs

import (
"strings"
"text/template"
)

func NewFuncMap() template.FuncMap {
return template.FuncMap{
"eqFold": eqFoldTemplateFunc,
}
}

// eqFoldTemplateFunc is the core implementation of the `eqFold` template
// function.
func eqFoldTemplateFunc(first, second string, more ...string) bool {
if strings.EqualFold(first, second) {
return true
}
for _, s := range more {
if strings.EqualFold(first, s) {
return true
}
}
return false
}
39 changes: 39 additions & 0 deletions conversionfuncs/conversionfuncs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package conversionfuncs

import (
"fmt"
"strconv"
"text/template"
)

func NewFuncMap() template.FuncMap {
return template.FuncMap{
"toString": toStringTemplateFunc,
}
}

// toStringTemplateFunc is the core implementation of the `toString` template
// function.
func toStringTemplateFunc(arg any) string {
// FIXME add more types
switch arg := arg.(type) {
case string:
return arg
case []byte:
return string(arg)
case bool:
return strconv.FormatBool(arg)
case float32:
return strconv.FormatFloat(float64(arg), 'f', -1, 32)
case float64:
return strconv.FormatFloat(arg, 'f', -1, 64)
case int:
return strconv.Itoa(arg)
case int32:
return strconv.FormatInt(int64(arg), 10)
case int64:
return strconv.FormatInt(arg, 10)
default:
panic(fmt.Sprintf("%T: unsupported type", arg))
}
}
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
```
9 changes: 9 additions & 0 deletions docs/conversionfuncs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Conversion Functions

## `toString` *input*

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

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

import (
_ "embed"
"embed"
"log"
"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 {
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 {
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 := pageTitleRx.FindStringSubmatch(lines[0])[1]
lines = lines[1:]

for _, line := range lines {
switch {
case strings.HasPrefix(line, "## "):
if reference.Title != "" {
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 @@ -65,4 +92,16 @@ func init() {
if reference.Title != "" {
References[funcName] = reference
}

return References
}

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

files := readFiles()

for _, file := range files {
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"])
}
39 changes: 39 additions & 0 deletions docs/encodingfuncs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Encoding 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"]
```
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
```
Loading
Loading