-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_rest.go
99 lines (80 loc) · 1.93 KB
/
writer_rest.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package venom
import (
"fmt"
"github.com/jimschubert/stripansi"
"strings"
)
type functionsRest struct {
}
func (f functionsRest) FormatHeader(input string) string {
return strings.ReplaceAll(input, " ", "_")
}
func (f functionsRest) FormatText(input string) string {
return stripansi.String(input)
}
func (f functionsRest) FormatOptions(input string) string {
return f.indented(input)
}
func (f functionsRest) FormatFlag(input Flag) string {
return f.indented(input.Usage)
}
func (f functionsRest) SeeAlsoPath(input string) string {
ref := strings.ReplaceAll(input, " ", "_")
return fmt.Sprintf("`%s <%s.rst>`_", input, ref)
}
func (f functionsRest) FormatExample(input string) string {
return f.indented(trimIndent(input, -1))
}
func (f functionsRest) FormatAutoGenTag(input string) string {
return input
}
func (f functionsRest) IsLocalFlag(input Flag) bool {
return !input.Persistent && !input.Inherited
}
func (f functionsRest) indented(input string) string {
var result []rune
addIndent := true
for _, char := range input {
switch char {
// trims space at the start of each line!
case '\t', '\v', '\f', '\r', ' ', 0x85, 0xA0:
if addIndent {
continue
}
case '\n':
if !addIndent && len(result) > 0 {
addIndent = true
}
}
if addIndent {
result = append(result, ' ', ' ')
addIndent = false
}
result = append(result, char)
}
return string(result)
}
type writerRest struct {
options TemplateOptions
}
func (w *writerRest) Write(outDir string, doc Documentation) error {
fns := functionsRest{}
helper := writerForTemplates{
name: ReST.String(),
fileExtension: "rst",
outDir: outDir,
doc: doc,
options: w.options,
funcs: fns,
includeIndex: false,
}
return helper.write()
}
func (w *writerRest) SetTemplateOptions(options TemplateOptions) {
w.options = options
}
func init() {
registerWriter(ReST, func() writer {
return &writerRest{}
})
}