Skip to content

Commit

Permalink
[add] function support
Browse files Browse the repository at this point in the history
  • Loading branch information
igtm committed Jan 14, 2022
1 parent 2eca824 commit d146423
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ Available template params:
{{.Params}} can access given parameters by '-p' option, which must be defined on 'Params' field of markdown header.
```

Available template functions:

`AnyKind of_string`

| Function | Result |
|--------------------|----------------------|
| `ToUpper` | `ANY KIND OF_STRING` |
| `ToLower` | `anykind of_string` |
| `ToSnake` | `any_kind_of_string` |
| `ToScreamingSnake` | `ANY_KIND_OF_STRING` |
| `ToKebab` | `any-kind-of-string` |
| `ToScreamingKebab` | `ANY-KIND-OF-STRING` |
| `ToCamel` | `AnyKindOfString` |
| `ToLowerCamel` | `anyKindOfString` |

This library uses [Go Template](https://pkg.go.dev/text/template).

so you can use any Go Template syntax like '{{if foo}} .. {{end}}' and like that.

# references

- [example code](./example)
2 changes: 1 addition & 1 deletion example/CODEGEN.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ package foo
import "fmt"

func Bar() {
fmt.Println("bar")
fmt.Println("{{ .Params.fuga | ToUpper }}")
}
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/igtm/manaita
go 1.17

require (
github.com/iancoleman/strcase v0.2.0
github.com/yuin/goldmark v1.4.4
github.com/yuin/goldmark-meta v1.0.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.4 h1:zNWRjYUW32G9KirMXYHQHVNFkXvMI7LpgNW2AgYAoIs=
github.com/yuin/goldmark v1.4.4/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg=
Expand Down
16 changes: 14 additions & 2 deletions manaita.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"text/template"

"github.com/iancoleman/strcase"
"github.com/yuin/goldmark"
meta "github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/parser"
Expand All @@ -28,6 +29,17 @@ const (
var (
c = flag.String("c", DefaultCodeGenFileName, "template markdown file path")
p = flag.String("p", "", "template params")

funcMap = template.FuncMap{
"ToUpper": strings.ToUpper,
"ToLower": strings.ToLower,
"ToSnake": strcase.ToSnake,
"ToScreamingSnake": strcase.ToScreamingSnake,
"ToKebab": strcase.ToKebab,
"ToScreamingKebab": strcase.ToScreamingKebab,
"ToCamel": strcase.ToCamel,
"ToLowerCamel": strcase.ToLowerCamel,
}
)

func main() {
Expand Down Expand Up @@ -111,7 +123,7 @@ func main() {
// dest filename
destFileName := strings.Replace(loc, DestFileNameStartCode, "", -1)
// compile filename
tmpl := template.Must(template.New("").Parse(destFileName))
tmpl := template.Must(template.New("").Funcs(funcMap).Parse(destFileName))
var compiledDest bytes.Buffer
err = tmpl.Execute(&compiledDest, map[string]interface{}{
"Env": envMap,
Expand All @@ -137,7 +149,7 @@ func main() {
code = GoTemplateHeader + code
}

tmpl := template.Must(template.New("").Parse(code))
tmpl := template.Must(template.New("").Funcs(funcMap).Parse(code))
err = tmpl.Execute(destFile, map[string]interface{}{
"Env": envMap,
"Params": paramMap,
Expand Down

0 comments on commit d146423

Please sign in to comment.