-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_markdown.go
110 lines (91 loc) · 2.54 KB
/
writer_markdown.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
100
101
102
103
104
105
106
107
108
109
110
package venom
import (
"fmt"
"github.com/jimschubert/stripansi"
"github.com/jimschubert/venom/internal"
"strings"
)
type functionsMarkdown struct {
stripAnsi bool
maxOptionWidth int
}
func (m functionsMarkdown) FormatOptions(input string) string {
leftAligned := trimIndent(input, 2)
width := 0
loop:
for i := 0; i < len(leftAligned); i++ {
first := rune(leftAligned[i])
// lookahead
for j := 1; j < 3 && (i+j < len(leftAligned)); j++ {
idx := i + j
if ' ' == first && ' ' == rune(leftAligned[idx]) {
last := rune(leftAligned[idx+1])
if ' ' != last && '-' != last {
width = idx + 1
break loop
}
}
}
}
columnWrapped := hangingIndent(leftAligned, width, m.maxOptionWidth)
return columnWrapped
}
func (m functionsMarkdown) FormatHeader(input string) string {
return input
}
func (m functionsMarkdown) FormatText(input string) string {
// TODO: consider cleaning HTML characters here, for now assume people do the right thing by indenting any code blocks in Long descriptions
if !m.stripAnsi {
return input
} else {
return stripansi.String(input)
}
}
func (m functionsMarkdown) FormatFlag(input Flag) string {
return strings.TrimSuffix(input.RawUsage, "\n")
}
func (m functionsMarkdown) SeeAlsoPath(input string) string {
return internal.CleanPath(input)
}
func (m functionsMarkdown) FormatExample(input string) string {
// first, handles code blocks with triple tick
replaced := strings.TrimPrefix(strings.TrimSuffix(input, "\n```"), "```\n")
// next, handles single-line code with triple tick
return fmt.Sprintf("```\n%s\n```", strings.TrimPrefix(strings.TrimSuffix(replaced, "```"), "```"))
}
func (m functionsMarkdown) FormatAutoGenTag(input string) string {
return input
}
func (m functionsMarkdown) IsLocalFlag(input Flag) bool {
return !input.Persistent && !input.Inherited
}
type writerMarkdown struct {
options TemplateOptions
}
func (w *writerMarkdown) Write(outDir string, doc Documentation) error {
fns := functionsMarkdown{
stripAnsi: w.options.StripAnsiInMarkdown,
maxOptionWidth: w.options.MaxOptionWidthInMarkdown,
}
helper := writerForTemplates{
name: Markdown.String(),
fileExtension: "md",
outDir: outDir,
doc: doc,
options: w.options,
funcs: fns,
includeIndex: true,
}
return helper.write()
}
func (w *writerMarkdown) SetTemplateOptions(options TemplateOptions) {
w.options = options
}
func init() {
registerWriter(Markdown, func() writer {
return &writerMarkdown{}
})
}
var (
_ functions = (*functionsMarkdown)(nil)
)