forked from JohannesKaufmann/html-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_help.go
108 lines (77 loc) · 2.19 KB
/
cmd_help.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
package cmd
import (
"flag"
"fmt"
"io"
"sort"
"strings"
"text/template"
)
var usageTemplate = `
# html2markdown - convert html to markdown [version {{ .Version }}]
Convert HTML to Markdown. Even works with entire websites!
## Basics
By default the "Commonmark" Plugin will be enabled. You can customize the options,
for example changing the appearance of bold with --opt-strong-delimiter="__"
Other Plugins can also be enabled. For example "GitHub Flavored Markdown" (GFM)
extends Commonmark with more features.
## Escaping
Some characters have a special meaning in markdown. The library escapes these — if necessary.
See the documentation for more info.
## Security
Once you convert this markdown *back* to HTML you need to be careful of malicious content.
Use a HTML sanitizer before displaying the HTML in the browser!
## Examples
echo "<strong>important</strong>" | html2markdown
curl --no-progress-meter http://example.com | html2markdown
## Flags
-v, --version
show the version of html2markdown and exit
--help
{{ range .Flags }}
--{{ .Name }}{{ with .Usage }}
{{ . | indent 8 }}{{ end }}
{{ end }}
For more information visit the documentation:
https://github.com/Johanneskaufmann/html-to-markdown
`
var templateFuncs = template.FuncMap{
"indent": func(spaces int, v string) string {
pad := strings.Repeat(" ", spaces)
return pad + strings.Replace(v, "\n", "\n"+pad, -1)
},
}
func tmpl(w io.Writer, text string, data interface{}) error {
t := template.New("usage")
t.Funcs(templateFuncs)
_, err := t.Parse(text)
if err != nil {
return err
}
return t.Execute(w, data)
}
func (cli *CLI) initUsageText() error {
var flags []*flag.Flag
cli.flags.VisitAll(func(f *flag.Flag) {
if f.Name == "v" || f.Name == "version" {
// We manually mention these in the usage
return
}
flags = append(flags, f)
})
sort.Slice(flags, func(i, j int) bool {
return flags[i].Name < flags[j].Name
})
data := map[string]any{
"Version": cli.Release.Version,
"Flags": flags,
}
err := tmpl(&cli.usageText, usageTemplate, data)
if err != nil {
return err
}
return nil
}
func (cli CLI) printUsage() {
fmt.Fprint(cli.Stdout, cli.usageText.String())
}