0
0
mirror of https://github.com/thegeeklab/wp-plugin-go.git synced 2024-11-24 13:10:39 +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{ &cli.StringFlag{
Name: "dummy-flag", Name: "dummy-flag",
Usage: "dummy flag desc", Usage: "Dummy flag desc.",
EnvVars: []string{"PLUGIN_DUMMY_FLAG"}, EnvVars: []string{"PLUGIN_DUMMY_FLAG"},
Value: "test", Value: "test",
Required: true, Required: true,
@ -104,7 +104,7 @@ func TestToData(t *testing.T) {
GlobalArgs: []*PluginArg{ GlobalArgs: []*PluginArg{
{ {
Name: "dummy_flag", Name: "dummy_flag",
Description: "dummy flag desc", Description: "Dummy flag desc.",
Default: "\"test\"", Default: "\"test\"",
Type: "string", Type: "string",
Required: true, Required: true,

View File

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

View File

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

View File

@ -1,14 +1,19 @@
package template package template
import ( import (
"fmt"
"text/template" "text/template"
"unicode"
"unicode/utf8"
"github.com/Masterminds/sprig/v3" "github.com/Masterminds/sprig/v3"
) )
func LoadFuncMap() template.FuncMap { func LoadFuncMap() template.FuncMap {
sprigFuncs := sprig.GenericFuncMap() sprigFuncs := sprig.GenericFuncMap()
customFuncs := template.FuncMap{} customFuncs := template.FuncMap{
"ToSentence": ToSentence,
}
for name, f := range customFuncs { for name, f := range customFuncs {
if _, ok := sprigFuncs[name]; ok { if _, ok := sprigFuncs[name]; ok {
@ -20,3 +25,28 @@ func LoadFuncMap() template.FuncMap {
return sprigFuncs 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:]
}