mirror of
https://github.com/thegeeklab/wp-plugin-go.git
synced 2024-11-23 16:50:39 +00:00
feat: add template func ToSentence (#49)
This commit is contained in:
parent
9196a966c5
commit
1a6a376eb1
@ -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,
|
||||
|
4
docs/templates/markdown.md.tmpl
vendored
4
docs/templates/markdown.md.tmpl
vendored
@ -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 -}}
|
||||
|
10
docs/testdata/expected-doc-full.md
vendored
10
docs/testdata/expected-doc-full.md
vendored
@ -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.
|
||||
|
@ -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:]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user