This repository was archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown.go
130 lines (120 loc) · 3.33 KB
/
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// markdown.go - this is a part of the cli package. This code focuses on
// generating Markdown docs from the internal help information.
package cli
import (
"fmt"
"io"
"sort"
"strings"
)
// GenerateMarkdown writes a Markdown page to io.Writer provided.
// Documentation is based on the application's metadata like app name,
// version, options, actions, etc.
func (c *Cli) GenerateMarkdown(w io.Writer) {
var parts []string
parts = append(parts, c.appName)
if len(c.options) > 0 {
parts = append(parts, "[OPTIONS]")
}
// NOTE: setup explicit parameter documentation
if len(c.params) > 0 {
parts = append(parts, c.params...)
}
if len(c.verbs) > 0 && len(c.params) == 0 {
if c.VerbsRequired {
parts = append(parts, "VERB")
} else {
parts = append(parts, "[VERB]")
}
// Check for verb options...
for _, verb := range c.verbs {
if len(verb.options) > 0 {
parts = append(parts, "[VERB OPTIONS]")
break
}
}
// Check for verb params
for _, verb := range c.verbs {
if len(verb.params) > 0 {
parts = append(parts, "[VERB PARAMETERS...]")
break
}
}
}
fmt.Fprintf(w, "\nUSAGE\n=====\n\n %s\n\n", strings.Join(parts, " "))
if section, ok := c.Documentation["synopsis"]; ok == true {
fmt.Fprintf(w, "SYNOPSIS\n--------\n\n%s\n\n", section)
}
if section, ok := c.Documentation["description"]; ok == true {
fmt.Fprintf(w, "DESCRIPTION\n-----------\n\n%s\n\n", section)
}
if len(c.env) > 0 {
fmt.Fprintf(w, "ENVIRONMENT\n-----------\n\n")
if len(c.options) > 0 {
fmt.Fprintf(w, "Environment variables can be overridden by corresponding options\n\n")
}
keys := []string{}
padding := 0
for k, _ := range c.env {
keys = append(keys, k)
if len(k) > padding {
padding = len(k) + 1
}
}
// Sort the keys alphabetically and display output
sort.Strings(keys)
fmt.Fprintf(w, "```\n")
for _, k := range keys {
fmt.Fprintf(w, " %s # %s\n", padRight(k, " ", padding), c.env[k].Usage)
}
fmt.Fprintf(w, "```\n\n")
}
if len(c.options) > 0 {
fmt.Fprintf(w, "OPTIONS\n-------\n\n")
parts := []string{}
parts = append(parts, "Below are a set of options available.")
if len(c.env) > 0 {
parts = append(parts, "Options will override any corresponding environment settings.")
}
if len(parts) > 0 {
fmt.Fprintf(w, "%s\n\n", strings.Join(parts, " "))
}
keys := []string{}
padding := 0
for k, _ := range c.options {
keys = append(keys, k)
if len(k) > padding {
padding = len(k) + 1
}
}
// Sort the keys alphabetically and display output
sort.Strings(keys)
fmt.Fprintf(w, "```\n")
for _, k := range keys {
fmt.Fprintf(w, " %s %s\n", padRight(k, " ", padding), c.options[k])
}
fmt.Fprintf(w, "```\n")
fmt.Fprintf(w, "\n\n")
}
if section, ok := c.Documentation["examples"]; ok == true {
fmt.Fprintf(w, "EXAMPLES\n--------\n\n%s\n\n", section)
}
if len(c.Documentation) > 0 {
keys := []string{}
for k, _ := range c.verbs {
if _, hasDocs := c.Documentation[k]; hasDocs == true {
keys = append(keys, k)
}
}
if len(keys) > 0 {
// Sort the keys alphabetically and display output
sort.Strings(keys)
links := []string{}
for _, key := range keys {
links = append(links, fmt.Sprintf("[%s](%s.html)", key, key))
}
fmt.Fprintf(w, "Related: %s\n\n", strings.Join(links, ", "))
}
}
fmt.Fprintf(w, "%s\n", c.version)
}