Merge pull request #109 from bep/markdowncustom
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Bjørn Erik Pedersen · committed by Bjørn Erik Pedersen · parents be18870, 1d99c8f · Browse files · 2 files·+65 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- genmarkdown: add optional frontmatter- and linkadjustment-funcs
Diff stats
2 files+65 added−6 removed
- other:+65 added−6 removed
Provenance
- Commit
e659faf- Tree
419efbc- Author
Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>- Committer
Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>- Parent 1
be18870- Parent 2
1d99c8f
2 files changed+65 added−6 removed
modified:md_docs.go+18 added-5 removed
45 unmodified lines46 func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }47 func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }48 49 func GenMarkdown(cmd *Command, out *bytes.Buffer) {50+ GenMarkdownCustom(cmd, out, func(s string) string { return s })51+}52+53+func GenMarkdownCustom(cmd *Command, out *bytes.Buffer, linkHandler func(string) string) {54 name := cmd.CommandPath()55 56 short := cmd.Short57 long := cmd.Long23 unmodified lines81 parent := cmd.Parent()82 pname := parent.CommandPath()83 link := pname + ".md"84 link = strings.Replace(link, " ", "_", -1)81- fmt.Fprintf(out, "* [%s](%s)\t - %s\n", pname, link, parent.Short)85+ fmt.Fprintf(out, "* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short)86 }87 88 children := cmd.Commands()89 sort.Sort(byName(children))4 unmodified lines94 }95 cname := name + " " + child.Name()96 link := cname + ".md"97 link = strings.Replace(link, " ", "_", -1)94- fmt.Fprintf(out, "* [%s](%s)\t - %s\n", cname, link, child.Short)98+ fmt.Fprintf(out, "* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short)99 }100 fmt.Fprintf(out, "\n")101 }102 103 fmt.Fprintf(out, "###### Auto generated by spf13/cobra at %s\n", time.Now().UTC())104 }105 106 func GenMarkdownTree(cmd *Command, dir string) {107+ noOp := func(s string) string { return s }108+ GenMarkdownTreeCustom(cmd, dir, noOp, noOp)109+}110+111+func GenMarkdownTreeCustom(cmd *Command, dir string, filePrepender func(string) string, linkHandler func(string) string) {112 for _, c := range cmd.Commands() {104- GenMarkdownTree(c, dir)113+ GenMarkdownTreeCustom(c, dir, filePrepender, linkHandler)114 }106-115 out := new(bytes.Buffer)116 109- GenMarkdown(cmd, out)117+ GenMarkdownCustom(cmd, out, linkHandler)118 119 filename := cmd.CommandPath()120 filename = dir + strings.Replace(filename, " ", "_", -1) + ".md"121 outFile, err := os.Create(filename)1 unmodified line123 fmt.Println(err)124 os.Exit(1)125 }126 defer outFile.Close()127+ _, err = outFile.WriteString(filePrepender(filename))128+ if err != nil {129+ fmt.Println(err)130+ os.Exit(1)131+ }132 _, err = outFile.Write(out.Bytes())133 if err != nil {134 fmt.Println(err)135 os.Exit(1)2 unmodified linesmodified:md_docs.md+47 added-1 removed
23 unmodified lines242526272828293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808123 unmodified linesThis will generate a whole series of files, one for each command in the tree, in the directory specified (in this case "./")
## Generate markdown docs for a single command
You may wish to have more control over the output, or only generate for a single command, instead of the entire command tree. If this is the case you may prefer to `GenMarkdown()` instead of `GenMarkdownTree`You may wish to have more control over the output, or only generate for a single command, instead of the entire command tree. If this is the case you may prefer to `GenMarkdown` instead of `GenMarkdownTree`
```go out := new(bytes.Buffer) cobra.GenMarkdown(cmd, out)```
This will write the markdown doc for ONLY "cmd" into the out, buffer.
## Customize the output
Both `GenMarkdown` and `GenMarkdownTree` has alternate versions with callbacks to get some control of the output:
```gofunc GenMarkdownTreeCustom(cmd *Command, dir string, filePrepender func(string) string, linkHandler func(string) string) { //...}```
```gofunc GenMarkdownCustom(cmd *Command, out *bytes.Buffer, linkHandler func(string) string) { //...}```
The `filePrepender` will prepend the return value given the full filepath to the rendered Markdown file. A common use case is to add front matter to use the generated documentation with [Hugo](http://gohugo.io/):
```goconst fmTemplate = `---date: %stitle: "%s"slug: %surl: %s---`
filePrepender := func(filename string) string { now := time.Now().Format(time.RFC3339) name := filepath.Base(filename) base := strings.TrimSuffix(name, path.Ext(name)) url := "/commands/" + strings.ToLower(base) + "/" return fmt.Sprintf(fmTemplate, now, strings.Replace(base, "_", " ", -1), base, url)}```
The `linkHandler` can be used to customize the rendered internal links to the commands, given a filename:
```golinkHandler := func(name string) string { base := strings.TrimSuffix(name, path.Ext(name)) return "/commands/" + strings.ToLower(base) + "/"}``` No newline at end of file