0
0
mirror of https://github.com/thegeeklab/wp-plugin-go.git synced 2024-11-21 04:00:40 +00:00

feat: add template func ToSentence (#49)

This commit is contained in:
Robert Kaussow 2024-01-02 22:21:53 +01:00 committed by GitHub
parent 9196a966c5
commit 1a6a376eb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 10 deletions

View File

@ -23,7 +23,7 @@ func testApp() *cli.App {
},
&cli.StringFlag{
Name: "dummy-flag",
Usage: "dummy flag desc",
Usage: "Dummy flag desc.",
EnvVars: []string{"PLUGIN_DUMMY_FLAG"},
Value: "test",
Required: true,
@ -104,7 +104,7 @@ func TestToData(t *testing.T) {
GlobalArgs: []*PluginArg{
{
Name: "dummy_flag",
Description: "dummy flag desc",
Description: "Dummy flag desc.",
Default: "\"test\"",
Type: "string",
Required: true,

View File

@ -1,7 +1,7 @@
{{ with .Name }}# {{ . }}{{ end }}
{{- with .Description }}
{{ . }}
{{ . | ToSentence }}
{{- end }}
{{- if .GlobalArgs }}
@ -9,7 +9,7 @@
{{ range $v := .GlobalArgs }}
**_{{ $v.Name }}_**{{ with $v.Default }}{{ if $v.Required }} _required_{{ end }} (defaut: {{ . }}){{ end }}{{ if $v.Description }}{{ "\\" }}{{ end }}
{{- with $v.Description }}
 {{ . }}
 {{ . | ToSentence }}
{{- end }}
{{ end -}}
{{ end -}}

View File

@ -1,19 +1,19 @@
# test
test description
Test description.
## Parameters
**_dummy_flag_** _required_ (defaut: "test")\
 dummy flag desc
 Dummy flag desc.
**_dummy_flag_int_** _required_ (defaut: 10)\
 dummy int flag desc
 Dummy int flag desc.
**_slice_flag_**\
 slice flag
 Slice flag.
**_x_simple_flag_**
**_z_other_flag_**\
 other flag with desc
 Other flag with desc.

View File

@ -1,14 +1,19 @@
package template
import (
"fmt"
"text/template"
"unicode"
"unicode/utf8"
"github.com/Masterminds/sprig/v3"
)
func LoadFuncMap() template.FuncMap {
sprigFuncs := sprig.GenericFuncMap()
customFuncs := template.FuncMap{}
customFuncs := template.FuncMap{
"ToSentence": ToSentence,
}
for name, f := range customFuncs {
if _, ok := sprigFuncs[name]; ok {
@ -20,3 +25,28 @@ func LoadFuncMap() template.FuncMap {
return sprigFuncs
}
func ToSentence(s string) string {
if s == "" {
return ""
}
r, n := utf8.DecodeRuneInString(s)
closer := ""
if getLastRune(s, 1) != "." {
closer = "."
}
return fmt.Sprintf("%s%s%s", string(unicode.ToUpper(r)), s[n:], closer)
}
func getLastRune(s string, c int) string {
j := len(s)
for i := 0; i < c && j > 0; i++ {
_, size := utf8.DecodeLastRuneInString(s[:j])
j -= size
}
return s[j:]
}