Skip to content
Myr

genmarkdown: add optional frontmatter- and linkadjustment-funcs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The automatic Markdown generator works great! But to use it to render the documentation in Hugo, we need front matter and slightly different links. This commit adds optional callback funcs to add that.

authored by bep · committed by bep · parent be18870 · 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.

Diff stats

2 files+65 added−6 removed

  • other:+65 added−6 removed

2 files+65 added−6 removed

modified:md_docs.go+18 added-5 removed
45 unmodified lines
46func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }47func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }4849func GenMarkdown(cmd *Command, out *bytes.Buffer) {50 GenMarkdownCustom(cmd, out, func(s string) string { return s })51}5253func GenMarkdownCustom(cmd *Command, out *bytes.Buffer, linkHandler func(string) string) {54 name := cmd.CommandPath()5556 short := cmd.Short57 long := cmd.Long
23 unmodified lines
81 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 }8788 children := cmd.Commands()89 sort.Sort(byName(children))
4 unmodified lines
94 }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 }102103 fmt.Fprintf(out, "###### Auto generated by spf13/cobra at %s\n", time.Now().UTC())104}105106func GenMarkdownTree(cmd *Command, dir string) {107 noOp := func(s string) string { return s }108 GenMarkdownTreeCustom(cmd, dir, noOp, noOp)109}110111func 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 }106115 out := new(bytes.Buffer)116109 GenMarkdown(cmd, out)117 GenMarkdownCustom(cmd, out, linkHandler)118119 filename := cmd.CommandPath()120 filename = dir + strings.Replace(filename, " ", "_", -1) + ".md"121 outFile, err := os.Create(filename)
1 unmodified line
123 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 lines
modified:md_docs.md+47 added-1 removed