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
|
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.
## Relative / Absolute Links
Use --domain="https://example.com" to convert *relative* links to *absolute* links.
The same also works for images.
## 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
html2markdown --input file.html --output file.md
html2markdown --input "src/*.html" --output "dist/"
## Flags
-v, --version
show the version of html2markdown and exit
--help
--input PATH
Input file, directory, or glob pattern (instead of stdin)
--output PATH
Output file or directory (instead of stdout)
--output-overwrite
Replace existing files
If --input is a directory or glob pattern, --output must be a directory.
{{ 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" || f.Name == "input" || f.Name == "output" || f.Name == "output-overwrite" {
// 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())
}
|