Skip to content
This repository was archived by the owner on Jul 29, 2021. It is now read-only.

Commit 5e613de

Browse files
committed
Add support for examples.
Fixes #2
1 parent deb306d commit 5e613de

File tree

9 files changed

+741
-54
lines changed

9 files changed

+741
-54
lines changed

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ readme:
44
godoc2md github.com/davecheney/godoc2md > README.md
55

66
examples:
7-
godoc2md github.com/kr/fs > examples/fs/README.md
8-
godoc2md github.com/codegangsta/martini > examples/martini/README.md
9-
godoc2md github.com/gorilla/sessions > examples/sessions/README.md
10-
godoc2md go/build > examples/build/README.md
7+
godoc2md -ex github.com/kr/fs > examples/fs/README.md
8+
godoc2md -ex github.com/codegangsta/martini > examples/martini/README.md
9+
godoc2md -ex github.com/gorilla/sessions > examples/sessions/README.md
10+
godoc2md -ex github.com/pkg/errors > examples/errors/README.md
11+
godoc2md -ex go/build > examples/build/README.md
1112

1213
.PHONY: examples readme all

example.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"go/doc"
6+
"go/printer"
7+
"regexp"
8+
"sort"
9+
"strings"
10+
"unicode"
11+
"unicode/utf8"
12+
13+
"golang.org/x/tools/godoc"
14+
)
15+
16+
type example struct {
17+
Name string
18+
Doc string
19+
Code string
20+
Output string
21+
}
22+
23+
func examplesFunc(info *godoc.PageInfo, name string) []*example {
24+
var egs []*example
25+
for _, eg := range info.Examples {
26+
if name != "*" && stripExampleSuffix(eg.Name) != name {
27+
continue
28+
}
29+
doc := eg.Doc
30+
out := eg.Output
31+
code, wholeFile := exampleCode(info, eg)
32+
if wholeFile {
33+
doc = ""
34+
out = ""
35+
}
36+
egs = append(egs, &example{
37+
Name: eg.Name,
38+
Doc: doc,
39+
Code: code,
40+
Output: out,
41+
})
42+
}
43+
sort.Slice(egs, func(i int, j int) bool {
44+
ni, si := splitExampleName(egs[i].Name)
45+
nj, sj := splitExampleName(egs[j].Name)
46+
if ni == nj {
47+
return si < sj
48+
}
49+
return ni < nj
50+
})
51+
return egs
52+
}
53+
54+
var exampleOutputRx = regexp.MustCompile(`(?i)//[[:space:]]*(unordered )?output:`)
55+
56+
func exampleCode(info *godoc.PageInfo, eg *doc.Example) (code string, wholeFile bool) {
57+
// Print code
58+
var buf bytes.Buffer
59+
cnode := &printer.CommentedNode{Node: eg.Code, Comments: eg.Comments}
60+
config := &printer.Config{Mode: printer.UseSpaces, Tabwidth: *tabWidth}
61+
config.Fprint(&buf, info.FSet, cnode)
62+
code = strings.Trim(buf.String(), "\n")
63+
wholeFile = true
64+
65+
if n := len(code); n >= 2 && code[0] == '{' && code[n-1] == '}' {
66+
wholeFile = false
67+
// Remove surrounding braces.
68+
code = strings.Trim(code[1:n-1], "\n")
69+
// Remove output from code.
70+
if loc := exampleOutputRx.FindStringIndex(code); loc != nil {
71+
code = strings.TrimRightFunc(code[:loc[0]], unicode.IsSpace)
72+
}
73+
// Unindent code.
74+
lines := strings.Split(code, "\n")
75+
unindent(lines)
76+
code = strings.Join(lines, "\n")
77+
}
78+
79+
return code, wholeFile
80+
}
81+
82+
func splitExampleName(s string) (name, suffix string) {
83+
i := strings.LastIndex(s, "_")
84+
if 0 <= i && i < len(s)-1 && !startsWithUppercase(s[i+1:]) {
85+
name = s[:i]
86+
suffix = " (" + strings.Title(s[i+1:]) + ")"
87+
return
88+
}
89+
name = s
90+
return
91+
}
92+
93+
// stripExampleSuffix strips lowercase braz in Foo_braz or Foo_Bar_braz from name
94+
// while keeping uppercase Braz in Foo_Braz.
95+
func stripExampleSuffix(name string) string {
96+
if i := strings.LastIndex(name, "_"); i != -1 {
97+
if i < len(name)-1 && !startsWithUppercase(name[i+1:]) {
98+
name = name[:i]
99+
}
100+
}
101+
return name
102+
}
103+
104+
func startsWithUppercase(s string) bool {
105+
r, _ := utf8.DecodeRuneInString(s)
106+
return unicode.IsUpper(r)
107+
}

examples/build/README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ During a particular build, the following words are satisfied:
117117
- "go1.6", from Go version 1.6 onward
118118
- "go1.7", from Go version 1.7 onward
119119
- "go1.8", from Go version 1.8 onward
120+
- "go1.9", from Go version 1.9 onward
121+
- "go1.10", from Go version 1.10 onward
120122
- any additional words listed in ctxt.BuildTags
121123

122124
If a file's name, after stripping the extension and a possible _test suffix,
@@ -216,7 +218,7 @@ ToolDir is the directory containing build tools.
216218

217219

218220

219-
## <a name="ArchChar">func</a> [ArchChar](/src/target/build.go?s=45435:45479#L1563)
221+
## <a name="ArchChar">func</a> [ArchChar](/src/target/build.go?s=47288:47332#L1611)
220222
``` go
221223
func ArchChar(goarch string) (string, error)
222224
```
@@ -228,7 +230,7 @@ no longer vary by architecture; they are compile, link, .o, and a.out, respectiv
228230

229231

230232

231-
## <a name="IsLocalImport">func</a> [IsLocalImport](/src/target/build.go?s=44955:44991#L1553)
233+
## <a name="IsLocalImport">func</a> [IsLocalImport](/src/target/build.go?s=46808:46844#L1601)
232234
``` go
233235
func IsLocalImport(path string) bool
234236
```
@@ -321,7 +323,7 @@ if set, or else the compiled code's GOARCH, GOOS, and GOROOT.
321323

322324

323325

324-
### <a name="Context.Import">func</a> (\*Context) [Import](/src/target/build.go?s=16697:16787#L486)
326+
### <a name="Context.Import">func</a> (\*Context) [Import](/src/target/build.go?s=16883:16973#L491)
325327
``` go
326328
func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Package, error)
327329
```
@@ -345,7 +347,7 @@ If an error occurs, Import returns a non-nil error and a non-nil
345347

346348

347349

348-
### <a name="Context.ImportDir">func</a> (\*Context) [ImportDir](/src/target/build.go?s=14860:14937#L434)
350+
### <a name="Context.ImportDir">func</a> (\*Context) [ImportDir](/src/target/build.go?s=15046:15123#L439)
349351
``` go
350352
func (ctxt *Context) ImportDir(dir string, mode ImportMode) (*Package, error)
351353
```
@@ -355,7 +357,7 @@ the named directory.
355357

356358

357359

358-
### <a name="Context.MatchFile">func</a> (\*Context) [MatchFile](/src/target/build.go?s=30989:31061#L1032)
360+
### <a name="Context.MatchFile">func</a> (\*Context) [MatchFile](/src/target/build.go?s=31854:31926#L1050)
359361
``` go
360362
func (ctxt *Context) MatchFile(dir, name string) (match bool, err error)
361363
```
@@ -369,7 +371,7 @@ read some or all of the file's content.
369371

370372

371373

372-
### <a name="Context.SrcDirs">func</a> (\*Context) [SrcDirs](/src/target/build.go?s=7522:7561#L238)
374+
### <a name="Context.SrcDirs">func</a> (\*Context) [SrcDirs](/src/target/build.go?s=7602:7641#L239)
373375
``` go
374376
func (ctxt *Context) SrcDirs() []string
375377
```
@@ -380,7 +382,7 @@ that do not exist.
380382

381383

382384

383-
## <a name="ImportMode">type</a> [ImportMode](/src/target/build.go?s=9780:9800#L325)
385+
## <a name="ImportMode">type</a> [ImportMode](/src/target/build.go?s=9966:9986#L330)
384386
``` go
385387
type ImportMode uint
386388
```
@@ -441,7 +443,7 @@ const (
441443

442444

443445

444-
## <a name="MultiplePackageError">type</a> [MultiplePackageError](/src/target/build.go?s=15413:15621#L451)
446+
## <a name="MultiplePackageError">type</a> [MultiplePackageError](/src/target/build.go?s=15599:15807#L456)
445447
``` go
446448
type MultiplePackageError struct {
447449
Dir string // directory containing files
@@ -461,14 +463,14 @@ multiple buildable Go source files for multiple packages.
461463

462464

463465

464-
### <a name="MultiplePackageError.Error">func</a> (\*MultiplePackageError) [Error](/src/target/build.go?s=15623:15668#L457)
466+
### <a name="MultiplePackageError.Error">func</a> (\*MultiplePackageError) [Error](/src/target/build.go?s=15809:15854#L462)
465467
``` go
466468
func (e *MultiplePackageError) Error() string
467469
```
468470

469471

470472

471-
## <a name="NoGoError">type</a> [NoGoError](/src/target/build.go?s=15165:15202#L441)
473+
## <a name="NoGoError">type</a> [NoGoError](/src/target/build.go?s=15351:15388#L446)
472474
``` go
473475
type NoGoError struct {
474476
Dir string
@@ -487,14 +489,14 @@ test files, files hidden by build tags, and so on.)
487489

488490

489491

490-
### <a name="NoGoError.Error">func</a> (\*NoGoError) [Error](/src/target/build.go?s=15204:15238#L445)
492+
### <a name="NoGoError.Error">func</a> (\*NoGoError) [Error](/src/target/build.go?s=15390:15424#L450)
491493
``` go
492494
func (e *NoGoError) Error() string
493495
```
494496

495497

496498

497-
## <a name="Package">type</a> [Package](/src/target/build.go?s=11686:14547#L372)
499+
## <a name="Package">type</a> [Package](/src/target/build.go?s=11872:14733#L377)
498500
``` go
499501
type Package struct {
500502
Dir string // directory containing package sources
@@ -557,14 +559,14 @@ A Package describes the Go package found in a directory.
557559

558560

559561

560-
### <a name="Import">func</a> [Import](/src/target/build.go?s=33366:33433#L1117)
562+
### <a name="Import">func</a> [Import](/src/target/build.go?s=34178:34245#L1135)
561563
``` go
562564
func Import(path, srcDir string, mode ImportMode) (*Package, error)
563565
```
564566
Import is shorthand for Default.Import.
565567

566568

567-
### <a name="ImportDir">func</a> [ImportDir](/src/target/build.go?s=33531:33592#L1122)
569+
### <a name="ImportDir">func</a> [ImportDir](/src/target/build.go?s=34343:34404#L1140)
568570
``` go
569571
func ImportDir(dir string, mode ImportMode) (*Package, error)
570572
```
@@ -574,7 +576,7 @@ ImportDir is shorthand for Default.ImportDir.
574576

575577

576578

577-
### <a name="Package.IsCommand">func</a> (\*Package) [IsCommand](/src/target/build.go?s=14705:14739#L428)
579+
### <a name="Package.IsCommand">func</a> (\*Package) [IsCommand](/src/target/build.go?s=14891:14925#L433)
578580
``` go
579581
func (p *Package) IsCommand() bool
580582
```

0 commit comments

Comments
 (0)