diff --git a/README.md b/README.md index e0c59ee..9adaee0 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/example/CODEGEN.md b/example/CODEGEN.md index d629230..eaaa698 100644 --- a/example/CODEGEN.md +++ b/example/CODEGEN.md @@ -40,6 +40,6 @@ package foo import "fmt" func Bar() { - fmt.Println("bar") + fmt.Println("{{ .Params.fuga | ToUpper }}") } ``` diff --git a/go.mod b/go.mod index 50ecfbf..3762a8c 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index d520cc1..9881311 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/manaita.go b/manaita.go index c82f18c..95f0b80 100644 --- a/manaita.go +++ b/manaita.go @@ -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" @@ -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() { @@ -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, @@ -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,