/
output.go
390 lines (349 loc) · 8.97 KB
/
output.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright 2020 cloudeng llc. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"bytes"
"fmt"
"go/ast"
"go/doc"
"go/format"
"go/token"
"os"
"path"
"regexp"
"strings"
"text/template"
"unicode"
"unicode/utf8"
"golang.org/x/tools/go/packages"
)
// any line with fewer than the requested number of words and which ends in
// a colon is deemed to be a heading.
func guessHeadings(level, words int, text string) string {
out := strings.Builder{}
sc := bufio.NewScanner(bytes.NewBufferString(text))
heading := strings.Repeat("#", level) + " "
for sc.Scan() {
line := sc.Text()
if len(line) == 0 {
out.WriteString("\n")
continue
}
first, _ := utf8.DecodeRuneInString(line)
fields := strings.Fields(line)
l := len(fields)
if unicode.IsUpper(first) && l > 0 && l < words && strings.HasSuffix(fields[l-1], ":") {
out.WriteString(heading)
fields[l-1] = strings.TrimSuffix(fields[l-1], ":")
out.WriteString(strings.Join(fields, " "))
out.WriteString("\n")
continue
}
out.WriteString(line)
out.WriteString("\n")
}
return out.String()
}
func highlight(word, text string) string {
out := strings.Builder{}
sc := bufio.NewScanner(bytes.NewBufferString(text))
pre := regexp.MustCompile(`(\s+)` + word)
post := regexp.MustCompile(word + `(\s+)`)
quoted := "`" + word + "`"
for sc.Scan() {
line := pre.ReplaceAllString(sc.Text(), "$1"+quoted)
line = post.ReplaceAllString(line, quoted+"$1")
out.WriteString(line)
out.WriteString("\n")
}
return out.String()
}
func goComment(text string) string {
out := strings.Builder{}
sc := bufio.NewScanner(bytes.NewBufferString(text))
for sc.Scan() {
out.WriteString("// ")
out.WriteString(sc.Text())
out.WriteString("\n")
}
return out.String()
}
func filterGoGenerate(text string) string {
out := strings.Builder{}
sc := bufio.NewScanner(bytes.NewBufferString(text))
for sc.Scan() {
if strings.HasPrefix(sc.Text(), "go:generate") {
continue
}
out.WriteString(sc.Text())
out.WriteString("\n")
}
return out.String()
}
type outputState struct {
markdownGoCodeStart string
doc *doc.Package
pkg *packages.Package
options outputOptions
tplFuncs map[string]interface{}
}
type outputOptions struct {
markdownFlavour string
goPkgSite string
circleciProject string
goreportcard bool
}
type outputOption func(o *outputOptions)
func markdownFlavour(f string) outputOption {
return func(o *outputOptions) {
o.markdownFlavour = f
}
}
func goPkgSite(s string) outputOption {
return func(o *outputOptions) {
o.goPkgSite = s
}
}
func goreportcard(b bool) outputOption {
return func(o *outputOptions) {
o.goreportcard = b
}
}
func circleciProject(p string) outputOption {
return func(o *outputOptions) {
o.circleciProject = p
}
}
func newOutputState(doc *doc.Package, pkg *packages.Package, opts ...outputOption) *outputState {
st := &outputState{doc: doc, pkg: pkg}
for _, fn := range opts {
fn(&st.options)
}
switch st.options.markdownFlavour {
case "github":
st.markdownGoCodeStart = "```go"
default:
st.markdownGoCodeStart = "```"
}
st.tplFuncs = map[string]interface{}{
"badges": st.badges,
"codeStart": st.codeStart,
"codeEnd": st.codeEnd,
"comment": st.comment,
"filterGoGenerate": filterGoGenerate,
"func": st.funcDecl,
"gocomment": goComment,
"guessHeadings": guessHeadings,
"highlight": highlight,
"type": st.typeDecl,
"value": st.valueDecl,
"join": strings.Join,
}
switch st.options.goPkgSite {
case "pkg.go.dev":
st.tplFuncs["packageLink"] = st.pkgGoDevPackageLink
st.tplFuncs["exampleLink"] = st.pkgGoDevExampleLink
case "godoc.org":
st.tplFuncs["packageLink"] = st.godocOrgPackageLink
st.tplFuncs["exampleLink"] = st.godocOrgExampleLink
default:
panic(fmt.Sprintf("unsupported go pkg site: %v", st.options.goPkgSite))
}
return st
}
func (st *outputState) badges() string {
var badges []string
if ci := st.options.circleciProject; len(ci) > 0 {
badge := fmt.Sprintf("[![CircleCI](https://circleci.com/gh/%v.svg?style=svg)](https://circleci.com/gh/%v)", ci, ci)
badges = append(badges, badge)
}
if st.options.goreportcard {
badge := fmt.Sprintf("[![Go Report Card](https://goreportcard.com/badge/%s)](https://goreportcard.com/report/%s)", st.pkg.PkgPath, st.pkg.PkgPath)
badges = append(badges, badge)
}
return strings.Join(badges, " ")
}
func (st *outputState) valueDecl(decl *ast.GenDecl) string {
switch decl.Tok {
case token.CONST, token.VAR:
out := &strings.Builder{}
for _, spec := range decl.Specs {
if err := format.Node(out, st.pkg.Fset, spec); err != nil {
fmt.Fprintf(os.Stderr, "%v: failed to format const or var node: %v", st.pkg.PkgPath, err)
}
out.WriteString("\n")
}
return out.String()
}
return "unsupported"
}
func (st *outputState) funcDecl(decl *ast.FuncDecl) string {
out := &strings.Builder{}
if err := format.Node(out, st.pkg.Fset, decl); err != nil {
at := st.pkg.Fset.PositionFor(decl.Pos(), false)
fmt.Fprintf(os.Stderr, "%v: failed to format function declaration at %v: %v", st.pkg.PkgPath, at, err)
}
return out.String()
}
func (st *outputState) typeDecl(decl *ast.GenDecl) string {
out := &strings.Builder{}
if err := format.Node(out, st.pkg.Fset, decl); err != nil {
at := st.pkg.Fset.PositionFor(decl.Pos(), false)
fmt.Fprintf(os.Stderr, "%v: failed to format type declaration at %v: %v", st.pkg.PkgPath, at, err)
}
return out.String()
}
func (st *outputState) codeStart() string {
return st.markdownGoCodeStart
}
func (st *outputState) codeEnd() string {
return "```"
}
func (st *outputState) godocOrgPackageLink() string {
return fmt.Sprintf("[%s](https://godoc.org/%s)", st.pkg.PkgPath, st.doc.ImportPath)
}
func (st *outputState) godocOrgExampleLink(eg *doc.Example) string {
return fmt.Sprintf("[Example%s](https://godoc.org/%s#example-%s)",
eg.Name, st.doc.ImportPath, eg.Name)
}
func (st *outputState) pkgGoDevPackageLink() string {
return fmt.Sprintf("[%s](https://pkg.go.dev/%s?tab=doc)",
st.pkg.PkgPath, st.doc.ImportPath)
}
func (st *outputState) pkgGoDevExampleLink(eg *doc.Example) string {
return fmt.Sprintf("[Example%s](https://pkg.go.dev/%s?tab=doc#example-%s)",
eg.Name, st.doc.ImportPath, eg.Name)
}
func (st *outputState) comment(indent, preIndent int, text string) string {
out := &strings.Builder{}
doc.ToText(out,
text,
strings.Repeat(" ", indent),
strings.Repeat(" ", preIndent),
80-indent-preIndent)
return out.String()
}
func (st *outputState) outputPackage() (string, error) {
tpl, err := template.New("markdown").Funcs(st.tplFuncs).Parse(markdownPackageTemplate)
if err != nil {
return "", fmt.Errorf("failed to create template: %v", err)
}
out := &strings.Builder{}
err = tpl.Execute(out, st.doc)
return out.String(), err
}
func (st *outputState) outputCommand() (string, error) {
tpl, err := template.New("markdown").Funcs(st.tplFuncs).Parse(markdownCommandTemplate)
if err != nil {
return "", fmt.Errorf("failed to create template: %v", err)
}
out := &strings.Builder{}
tmp := struct {
*doc.Package
CommandName string
}{
st.doc,
path.Base(st.pkg.PkgPath),
}
err = tpl.Execute(out, tmp)
return out.String(), err
}
var markdownPackageTemplate = `# Package {{packageLink}}
{{if badges}}{{badges}}
{{end}}
{{codeStart}}
import {{.ImportPath}}
{{codeEnd}}
{{filterGoGenerate .Doc | comment 0 4}}
{{- if .Consts}}
## Constants
{{range .Consts}}### {{join .Names ", "}}
{{codeStart}}
{{value .Decl}}
{{codeEnd}}
{{comment 0 4 .Doc}}
{{end}}
{{end}}
{{- if .Vars}}
## Variables
{{range .Vars}}### {{join .Names ", "}}
{{codeStart}}
{{value .Decl}}
{{codeEnd}}
{{comment 0 4 .Doc}}
{{end}}
{{end}}
{{- if .Funcs}}
## Functions
{{range .Funcs}}### Func {{.Name}}
{{codeStart}}
{{func .Decl}}
{{codeEnd}}
{{comment 0 4 .Doc}}
{{end}}
{{end}}
{{- if .Types}}
## Types
{{range .Types}}### Type {{.Name}}
{{codeStart}}
{{type .Decl}}
{{codeEnd}}
{{comment 0 4 .Doc}}
{{- if .Consts}}
### Constants
{{range .Consts}}### {{join .Names ", "}}
{{codeStart}}
{{value .Decl}}
{{codeEnd}}
{{comment 0 4 .Doc}}
{{end}}
{{end}}
{{- if .Vars}}
### Variables
{{range .Vars}}### {{join .Names ", "}}
{{codeStart}}
{{value .Decl}}
{{codeEnd}}
{{comment 0 4 .Doc}}
{{end}}
{{end}}
{{- if .Funcs}}
### Functions
{{range .Funcs}}
{{codeStart}}
{{func .Decl}}
{{codeEnd}}
{{comment 0 4 .Doc}}
{{end}}
{{end}}
{{- if .Methods}}
### Methods
{{range .Methods}}
{{codeStart}}
{{func .Decl}}
{{codeEnd}}
{{comment 0 4 .Doc}}
{{end}}
{{end}}
{{end}}
{{end}}
{{- if .Examples}}
## Examples
{{range .Examples}}### {{exampleLink . }}
{{comment 0 4 .Doc}}
{{end}}
{{end}}
{{- if .Notes}}
{{range $marker, $notes := .Notes}}
### {{$marker}}
{{range $notes}}- {{.UID}}: {{.Body}}{{end}}
{{end}}
{{end}}
`
var markdownCommandTemplate = `# {{packageLink}}
{{if badges}}{{badges}}
{{end}}
{{comment 0 4 .Doc| guessHeadings 1 5 | highlight .CommandName }}
`