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

Fix the url in the titles and the files url + support examples generation #20

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ readme:
godoc2md github.com/davecheney/godoc2md > README.md

examples:
godoc2md github.com/kr/fs > examples/fs/README.md
godoc2md github.com/codegangsta/martini > examples/martini/README.md
godoc2md github.com/gorilla/sessions > examples/sessions/README.md
godoc2md go/build > examples/build/README.md
godoc2md -ex github.com/kr/fs > examples/fs/README.md
godoc2md -ex github.com/codegangsta/martini > examples/martini/README.md
godoc2md -ex github.com/gorilla/sessions > examples/sessions/README.md
godoc2md -ex go/build > examples/build/README.md
godoc2md -ex github.com/pkg/errors > examples/errors/README.md

.PHONY: examples readme all
192 changes: 192 additions & 0 deletions examples.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package main

import (
"bytes"
"fmt"
"go/printer"
"strings"
"unicode"
"unicode/utf8"

"golang.org/x/tools/godoc"
)

func exampleLinkFunc(funcName string) string {
i := strings.LastIndex(funcName, "_")
if 0 <= i && i < len(funcName)-1 && !startsWithUppercase(funcName[i+1:]) {
name := strings.ToLower(funcName[:i])
suffix := strings.ToLower(funcName[i+1:])
return fmt.Sprintf("%s-%s", name, suffix)
}
return strings.ToLower(funcName)
}

// Based on example_textFunc from
// https://github.com/golang/tools/blob/master/godoc/godoc.go
func exampleMdFunc(info *godoc.PageInfo, funcName string) string {
if !*showExamples {
return ""
}

var buf bytes.Buffer
first := true
for _, eg := range info.Examples {
name := stripExampleSuffix(eg.Name)
if name != funcName {
continue
}

if !first {
buf.WriteString("\n")
}
first = false

// print code
cnode := &printer.CommentedNode{Node: eg.Code, Comments: eg.Comments}
config := &printer.Config{Mode: printer.UseSpaces, Tabwidth: pres.TabWidth}
var buf1 bytes.Buffer
config.Fprint(&buf1, info.FSet, cnode)
code := buf1.String()
output := strings.Trim(eg.Output, "\n")
output = replaceLeadingIndentation(output, strings.Repeat(" ", pres.TabWidth), "")

// Additional formatting if this is a function body. Unfortunately, we
// can't print statements individually because we would lose comments
// on later statements.
if n := len(code); n >= 2 && code[0] == '{' && code[n-1] == '}' {
// remove surrounding braces
code = code[1 : n-1]
// unindent
code = replaceLeadingIndentation(code, strings.Repeat(" ", pres.TabWidth), "")
}
code = strings.Trim(code, "\n")
name, suffix := splitExampleName(eg.Name)
title := fmt.Sprintf("##### Example %s%s:\n", name, suffix)
buf.WriteString(title)
if len(eg.Doc) > 0 {
buf.WriteString(eg.Doc)
buf.WriteString("\n")
}
buf.WriteString("``` go\n")
buf.WriteString(code)
buf.WriteString("\n```\n\n")
if len(output) > 0 {
buf.WriteString("Output:\n")
buf.WriteString("\n```\n")
buf.WriteString(output)
buf.WriteString("\n```\n\n")
}
}
return buf.String()
}

// Copy/pasted from https://github.com/golang/tools/blob/master/godoc/godoc.go
func splitExampleName(s string) (name, suffix string) {
i := strings.LastIndex(s, "_")
if 0 <= i && i < len(s)-1 && !startsWithUppercase(s[i+1:]) {
name = s[:i]
suffix = " (" + strings.Title(s[i+1:]) + ")"
return
}
name = s
return
}

// Copy/pasted from https://github.com/golang/tools/blob/master/godoc/godoc.go#L786
func stripExampleSuffix(name string) string {
if i := strings.LastIndex(name, "_"); i != -1 {
if i < len(name)-1 && !startsWithUppercase(name[i+1:]) {
name = name[:i]
}
}
return name
}

// Copy/pasted from https://github.com/golang/tools/blob/master/godoc/godoc.go#L777
func startsWithUppercase(s string) bool {
r, _ := utf8.DecodeRuneInString(s)
return unicode.IsUpper(r)
}

// Copy/pasted from https://github.com/golang/tools/blob/master/godoc/godoc.go
func replaceLeadingIndentation(body, oldIndent, newIndent string) string {
// Handle indent at the beginning of the first line. After this, we handle
// indentation only after a newline.
var buf bytes.Buffer
if strings.HasPrefix(body, oldIndent) {
buf.WriteString(newIndent)
body = body[len(oldIndent):]
}

// Use a state machine to keep track of whether we're in a string or
// rune literal while we process the rest of the code.
const (
codeState = iota
runeState
interpretedStringState
rawStringState
)
searchChars := []string{
"'\"`\n", // codeState
`\'`, // runeState
`\"`, // interpretedStringState
"`\n", // rawStringState
// newlineState does not need to search
}
state := codeState
for {
i := strings.IndexAny(body, searchChars[state])
if i < 0 {
buf.WriteString(body)
break
}
c := body[i]
buf.WriteString(body[:i+1])
body = body[i+1:]
switch state {
case codeState:
switch c {
case '\'':
state = runeState
case '"':
state = interpretedStringState
case '`':
state = rawStringState
case '\n':
if strings.HasPrefix(body, oldIndent) {
buf.WriteString(newIndent)
body = body[len(oldIndent):]
}
}

case runeState:
switch c {
case '\\':
r, size := utf8.DecodeRuneInString(body)
buf.WriteRune(r)
body = body[size:]
case '\'':
state = codeState
}

case interpretedStringState:
switch c {
case '\\':
r, size := utf8.DecodeRuneInString(body)
buf.WriteRune(r)
body = body[size:]
case '"':
state = codeState
}

case rawStringState:
switch c {
case '`':
state = codeState
case '\n':
buf.WriteString(newIndent)
}
}
}
return buf.String()
}
36 changes: 19 additions & 17 deletions examples/build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ During a particular build, the following words are satisfied:
- "go1.6", from Go version 1.6 onward
- "go1.7", from Go version 1.7 onward
- "go1.8", from Go version 1.8 onward
- "go1.9", from Go version 1.9 onward
- "go1.10", from Go version 1.10 onward
- any additional words listed in ctxt.BuildTags

If a file's name, after stripping the extension and a possible _test suffix,
Expand Down Expand Up @@ -204,7 +206,7 @@ documentation.


#### <a name="pkg-files">Package files</a>
[build.go](/src/go/build/build.go) [doc.go](/src/go/build/doc.go) [read.go](/src/go/build/read.go) [syslist.go](/src/go/build/syslist.go) [zcgo.go](/src/go/build/zcgo.go)
[build.go](https://golang.org/src/go/build/build.go) [doc.go](https://golang.org/src/go/build/doc.go) [read.go](https://golang.org/src/go/build/read.go) [syslist.go](https://golang.org/src/go/build/syslist.go) [zcgo.go](https://golang.org/src/go/build/zcgo.go)



Expand All @@ -216,7 +218,7 @@ ToolDir is the directory containing build tools.



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



## <a name="IsLocalImport">func</a> [IsLocalImport](/src/target/build.go?s=44955:44991#L1553)
## <a name="IsLocalImport">func</a> [IsLocalImport](https://golang.org/src/go/build/build.go?s=46808:46844#L1601)
``` go
func IsLocalImport(path string) bool
```
Expand All @@ -238,7 +240,7 @@ a local import path, like ".", "..", "./foo", or "../foo".



## <a name="Context">type</a> [Context](/src/target/build.go?s=450:3568#L30)
## <a name="Context">type</a> [Context](https://golang.org/src/go/build/build.go?s=450:3568#L30)
``` go
type Context struct {
GOARCH string // target architecture
Expand Down Expand Up @@ -321,7 +323,7 @@ if set, or else the compiled code's GOARCH, GOOS, and GOROOT.



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



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



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



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



## <a name="ImportMode">type</a> [ImportMode](/src/target/build.go?s=9780:9800#L325)
## <a name="ImportMode">type</a> [ImportMode](https://golang.org/src/go/build/build.go?s=9966:9986#L330)
``` go
type ImportMode uint
```
Expand Down Expand Up @@ -441,7 +443,7 @@ const (



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



### <a name="MultiplePackageError.Error">func</a> (\*MultiplePackageError) [Error](/src/target/build.go?s=15623:15668#L457)
### <a name="MultiplePackageError.Error">func</a> (\*MultiplePackageError) [Error](https://golang.org/src/go/build/build.go?s=15809:15854#L462)
``` go
func (e *MultiplePackageError) Error() string
```



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



### <a name="NoGoError.Error">func</a> (\*NoGoError) [Error](/src/target/build.go?s=15204:15238#L445)
### <a name="NoGoError.Error">func</a> (\*NoGoError) [Error](https://golang.org/src/go/build/build.go?s=15390:15424#L450)
``` go
func (e *NoGoError) Error() string
```



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



### <a name="Import">func</a> [Import](/src/target/build.go?s=33366:33433#L1117)
### <a name="Import">func</a> [Import](https://golang.org/src/go/build/build.go?s=34178:34245#L1135)
``` go
func Import(path, srcDir string, mode ImportMode) (*Package, error)
```
Import is shorthand for Default.Import.


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



### <a name="Package.IsCommand">func</a> (\*Package) [IsCommand](/src/target/build.go?s=14705:14739#L428)
### <a name="Package.IsCommand">func</a> (\*Package) [IsCommand](https://golang.org/src/go/build/build.go?s=14891:14925#L433)
``` go
func (p *Package) IsCommand() bool
```
Expand Down
Loading