docgen.go 1.2 KB
Newer Older
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
package terminal

import (
	"fmt"
	"io"
	"strings"
)

func replaceDocPath(s string) string {
	const docpath = "$GOPATH/src/github.com/derekparker/delve/"

	for {
		start := strings.Index(s, docpath)
		if start < 0 {
			return s
		}
		var end int
		for end = start + len(docpath); end < len(s); end++ {
			if s[end] == ' ' {
				break
			}
		}

		text := s[start+len(docpath) : end]
		s = s[:start] + fmt.Sprintf("[%s](//github.com/derekparker/delve/tree/master/%s)", text, text) + s[end:]
	}
}

func (commands *Commands) WriteMarkdown(w io.Writer) {
30
	fmt.Fprint(w, "# Commands\n\n")
31

32 33
	fmt.Fprint(w, "Command | Description\n")
	fmt.Fprint(w, "--------|------------\n")
34 35 36 37 38 39 40
	for _, cmd := range commands.cmds {
		h := cmd.helpMsg
		if idx := strings.Index(h, "\n"); idx >= 0 {
			h = h[:idx]
		}
		fmt.Fprintf(w, "[%s](#%s) | %s\n", cmd.aliases[0], cmd.aliases[0], h)
	}
41
	fmt.Fprint(w, "\n")
42 43 44 45

	for _, cmd := range commands.cmds {
		fmt.Fprintf(w, "## %s\n%s\n\n", cmd.aliases[0], replaceDocPath(cmd.helpMsg))
		if len(cmd.aliases) > 1 {
46
			fmt.Fprint(w, "Aliases:")
47 48 49
			for _, alias := range cmd.aliases[1:] {
				fmt.Fprintf(w, " %s", alias)
			}
50
			fmt.Fprint(w, "\n")
51
		}
52
		fmt.Fprint(w, "\n")
53 54
	}
}