-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtemplate.go
More file actions
42 lines (39 loc) · 870 Bytes
/
template.go
File metadata and controls
42 lines (39 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package template
import (
"bytes"
"strings"
"text/template"
)
func CustomExample(example string, kwargs map[string]string) string {
tpl, err := template.New("template").Funcs(template.FuncMap{
"rpad": func(str string, length int) string {
padding := length - len(str)
if padding > 0 {
return str + strings.Repeat(" ", padding)
}
return str
},
}).Parse(`{{.Example}}
Args:
{{- range $key, $value := .Kwargs}}
{{rpad $key ($.Padding)}}{{$value}}
{{- end}}`)
if err != nil {
return example
}
padding, miniPadding := 0, 3
for key := range kwargs {
if padding < len(key)+miniPadding {
padding = len(key) + miniPadding
}
}
var buf bytes.Buffer
if err = tpl.Execute(&buf, struct {
Example string
Kwargs map[string]string
Padding int
}{example, kwargs, padding}); err != nil {
return example
}
return buf.String()
}