diff --git a/docs/docs_test.go b/docs/docs_test.go index a874556..166b33c 100644 --- a/docs/docs_test.go +++ b/docs/docs_test.go @@ -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, diff --git a/docs/templates/markdown.md.tmpl b/docs/templates/markdown.md.tmpl index 2ceb661..a704b42 100644 --- a/docs/templates/markdown.md.tmpl +++ b/docs/templates/markdown.md.tmpl @@ -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 -}} diff --git a/docs/testdata/expected-doc-full.md b/docs/testdata/expected-doc-full.md index 60c14ac..7326f34 100644 --- a/docs/testdata/expected-doc-full.md +++ b/docs/testdata/expected-doc-full.md @@ -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. diff --git a/template/functions.go b/template/functions.go index 4c5b521..0aa44c9 100644 --- a/template/functions.go +++ b/template/functions.go @@ -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:] +}