forked from lanre-ade/godoc2md
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.go
192 lines (176 loc) · 4.65 KB
/
examples.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
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()
}