forked from davecheney/godoc2md
/
main.go
130 lines (107 loc) · 3.02 KB
/
main.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// godoc2md converts godoc formatted package documentation into Markdown format.
//
//
// Usage
//
// godoc2md $PACKAGE > $GOPATH/src/$PACKAGE/README.md
package main
import (
"bytes"
"flag"
"fmt"
"go/build"
"log"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"text/template"
"golang.org/x/tools/godoc"
"golang.org/x/tools/godoc/vfs"
)
var (
verbose = flag.Bool("v", false, "verbose mode")
// file system roots
// TODO(gri) consider the invariant that goroot always end in '/'
goroot = flag.String("goroot", runtime.GOROOT(), "Go root directory")
// layout control
tabWidth = flag.Int("tabwidth", 4, "tab width")
showTimestamps = flag.Bool("timestamps", false, "show timestamps with directory listings")
templateDir = flag.String("templates", "", "directory containing alternate template files")
showPlayground = flag.Bool("play", false, "enable playground in web interface")
showExamples = flag.Bool("ex", false, "show examples in command line mode")
declLinks = flag.Bool("links", true, "link identifiers to their declarations")
)
func usage() {
fmt.Fprintf(os.Stderr,
"usage: godoc2md package [name ...]\n")
flag.PrintDefaults()
os.Exit(2)
}
var (
pres *godoc.Presentation
fs = vfs.NameSpace{}
funcs = map[string]interface{}{
"comment_md": comment_mdFunc,
"base": path.Base,
"md": mdFunc,
"pre": preFunc,
}
)
const punchCardWidth = 80
func comment_mdFunc(comment string) string {
var buf bytes.Buffer
ToMD(&buf, comment, nil)
return buf.String()
}
func mdFunc(text string) string {
text = strings.Replace(text, "*", "\\*", -1)
text = strings.Replace(text, "_", "\\_", -1)
return text
}
func preFunc(text string) string {
return "``` go\n" + text + "\n```"
}
func readTemplate(name, data string) *template.Template {
// be explicit with errors (for app engine use)
t, err := template.New(name).Funcs(pres.FuncMap()).Funcs(funcs).Parse(string(data))
if err != nil {
log.Fatal("readTemplate: ", err)
}
return t
}
func readTemplates(p *godoc.Presentation, html bool) {
p.PackageText = readTemplate("package.txt", pkgTemplate)
}
func main() {
flag.Usage = usage
flag.Parse()
// Check usage
if flag.NArg() == 0 {
usage()
}
// use file system of underlying OS
fs.Bind("/", vfs.OS(*goroot), "/", vfs.BindReplace)
// Bind $GOPATH trees into Go root.
for _, p := range filepath.SplitList(build.Default.GOPATH) {
fs.Bind("/src/pkg", vfs.OS(p), "/src", vfs.BindAfter)
}
corpus := godoc.NewCorpus(fs)
corpus.Verbose = *verbose
pres = godoc.NewPresentation(corpus)
pres.TabWidth = *tabWidth
pres.ShowTimestamps = *showTimestamps
pres.ShowPlayground = *showPlayground
pres.ShowExamples = *showExamples
pres.DeclLinks = *declLinks
pres.SrcMode = false
pres.HTMLMode = false
readTemplates(pres, false)
if err := godoc.CommandLine(os.Stdout, fs, pres, flag.Args()); err != nil {
log.Print(err)
}
}