forked from GandalfUK/godoc2ghmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
242 lines (204 loc) · 6.27 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// 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.
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/build"
"go/doc"
"go/token"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"regexp"
"runtime"
"strconv"
"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")
importAs = flag.String("import_as", "", "import path to display")
importLinks = flag.Bool("import_links", true, "link imports to their relative path or godoc.org page otherwise")
verifyImportLinks = flag.Bool("verify_import_links", true, "verify godoc.org links are accessible")
importLinksFile = flag.String("import_links_file", "", "file location to read and write state for godoc.org verifications")
vendorPath = flag.String("vendor", "", "path to vendor directory to determine if imports are vendored")
filePath = flag.String("file", "", "If specified, write output to the given file-name instead of stdout.")
fmtProtobuf = flag.Bool("fmt_protobuf", true, "enable formatting for generated Protobuf docs")
protobufPreludeMatcher = flag.String("protobuf_prelude_matcher", "generated protocol buffer package", "string from which to match generate Protobuf prelude")
protobufFilesMatcher = flag.String("protobuf_files_matcher", "generated from these files", "string from which to match .proto files list")
protobufMessagesMatcher = flag.String("protobuf_messages_matcher", "these top-level messages", "string from which to match Protobuf messages list")
stdLib = getStdLib()
importLinksState = make(map[string]string)
)
const (
validImportKey = "valid"
invalidImportKey = "invalid"
)
func init() {
flag.Usage = usage
flag.Parse()
if !*verifyImportLinks || *importLinksFile == "" {
return
}
if _, err := os.Stat(*importLinksFile); err != nil && os.IsNotExist(err) {
return
}
b, err := ioutil.ReadFile(*importLinksFile)
if err != nil {
usage()
}
for _, line := range strings.Split(string(b), "\n") {
if line == "" {
continue
}
fields := strings.Fields(line)
if len(fields) < 2 || fields[0] == "" || fields[1] == "" {
continue
}
importLinksState[fields[0]] = fields[1]
}
}
func usage() {
fmt.Fprintf(os.Stderr, "usage: godoc2gh package [name ...]\n")
flag.PrintDefaults()
os.Exit(2)
}
var (
pres *godoc.Presentation
fs = vfs.NameSpace{}
funcs map[string]interface{}
)
const punchCardWidth = 80
func pkgDoc_mdFunc(comment string) string {
var buf bytes.Buffer
ToMD(&buf, comment, nil)
s := buf.String()
if *fmtProtobuf && strings.Contains(s, *protobufPreludeMatcher) {
s = fmtProtobufDoc(s)
}
return s
}
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 ghUrlFunc(info *godoc.PageInfo, n interface{}) string {
var pos, end token.Pos
switch an := n.(type) {
case ast.Node:
pos = an.Pos()
end = an.End()
case *doc.Note:
pos = an.Pos
end = an.End
default:
panic(fmt.Sprintf("wrong type for gh_url template formatter: %T", an))
}
var posLine int
var filePath string
var linesFragment string
if pos.IsValid() {
p := info.FSet.Position(pos)
posLine = p.Line
filePath = p.Filename
if strings.HasPrefix(filePath, "/target/") {
filePath = filePath[len("/target/"):]
}
linesFragment = "#L" + strconv.Itoa(posLine)
}
if end.IsValid() {
endPos := info.FSet.Position(end)
if endPos.Line > posLine {
linesFragment += "-L" + strconv.Itoa(endPos.Line)
}
}
return "./" + filePath + linesFragment
}
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() {
// 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
funcs = map[string]interface{}{
"pkgdoc_md": pkgDoc_mdFunc,
"comment_md": comment_mdFunc,
"example_md": (*myPres)(pres).exampleMDFunc,
"base": path.Base,
"md": mdFunc,
"pre": preFunc,
"gh_url": ghUrlFunc,
"import_as": importAsFunc,
"list_imports": listImportsFunc,
}
readTemplates(pres, false)
var buf bytes.Buffer
if err := godoc.CommandLine(&buf, fs, pres, flag.Args()); err != nil {
log.Fatal(err)
}
replaced := bytes.TrimSpace(regexp.MustCompile("[\n]{3,}").ReplaceAllLiteral(buf.Bytes(), []byte("\n\n")))
if *filePath == "" {
_, err := os.Stdout.Write(replaced)
if err != nil {
log.Fatal(err)
}
} else {
err := ioutil.WriteFile(*filePath, replaced, 0644)
if err != nil {
log.Fatal(err)
}
}
}