forked from aws/aws-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
docstring.go
156 lines (136 loc) · 3.45 KB
/
docstring.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
package api
import (
"bytes"
"encoding/json"
"fmt"
"html"
"os"
"regexp"
"strings"
)
type apiDocumentation struct {
*API
Operations map[string]string
Service string
Shapes map[string]shapeDocumentation
}
type shapeDocumentation struct {
Base string
Refs map[string]string
}
// AttachDocs attaches documentation from a JSON filename.
func (a *API) AttachDocs(filename string) {
d := apiDocumentation{API: a}
f, err := os.Open(filename)
defer f.Close()
if err != nil {
panic(err)
}
err = json.NewDecoder(f).Decode(&d)
if err != nil {
panic(err)
}
d.setup()
}
func (d *apiDocumentation) setup() {
d.API.Documentation = docstring(d.Service)
if d.Service == "" {
d.API.Documentation =
fmt.Sprintf("// %s is a client for %s.\n", d.API.StructName(), d.API.NiceName())
}
for op, doc := range d.Operations {
d.API.Operations[op].Documentation = docstring(doc)
}
for shape, info := range d.Shapes {
if sh := d.API.Shapes[shape]; sh != nil {
sh.Documentation = docstring(info.Base)
}
for ref, doc := range info.Refs {
if doc == "" {
continue
}
parts := strings.Split(ref, "$")
if sh := d.API.Shapes[parts[0]]; sh != nil {
if m := sh.MemberRefs[parts[1]]; m != nil {
m.Documentation = docstring(doc)
}
}
}
}
}
var reNewline = regexp.MustCompile(`\r?\n`)
var reMultiSpace = regexp.MustCompile(`\s+`)
var reComments = regexp.MustCompile(`<!--.*?-->`)
var reFullname = regexp.MustCompile(`\s*<fullname?>.+?<\/fullname?>\s*`)
var reExamples = regexp.MustCompile(`<examples?>.+?<\/examples?>`)
var rePara = regexp.MustCompile(`<(?:p|h\d)>(.+?)</(?:p|h\d)>`)
var reLink = regexp.MustCompile(`<a href="(.+?)">(.+?)</a>`)
var reTag = regexp.MustCompile(`<.+?>`)
var reEndNL = regexp.MustCompile(`\n+$`)
// docstring rewrites a string to insert godocs formatting.
func docstring(doc string) string {
doc = reNewline.ReplaceAllString(doc, "")
doc = reMultiSpace.ReplaceAllString(doc, " ")
doc = reComments.ReplaceAllString(doc, "")
doc = reFullname.ReplaceAllString(doc, "")
doc = reExamples.ReplaceAllString(doc, "")
doc = rePara.ReplaceAllString(doc, "$1\n\n")
doc = reLink.ReplaceAllString(doc, "$2 ($1)")
doc = reTag.ReplaceAllString(doc, "$1")
doc = reEndNL.ReplaceAllString(doc, "")
doc = strings.TrimSpace(doc)
if doc == "" {
return "\n"
}
doc = html.UnescapeString(doc)
doc = wrap(doc, 72)
return commentify(doc)
}
// commentify converts a string to a Go comment
func commentify(doc string) string {
lines := strings.Split(doc, "\n")
out := []string{}
for i, line := range lines {
if i > 0 && line == "" && lines[i-1] == "" {
continue
}
out = append(out, "// "+line)
}
return strings.Join(out, "\n") + "\n"
}
// wrap returns a rewritten version of text to have line breaks
// at approximately length characters. Line breaks will only be
// inserted into whitespace.
func wrap(text string, length int) string {
var buf bytes.Buffer
var last rune
var lastNL bool
var col int
for _, c := range text {
switch c {
case '\r': // ignore this
continue // and also don't track `last`
case '\n': // ignore this too, but reset col
if col >= length || last == '\n' {
buf.WriteString("\n\n")
}
col = 0
case ' ', '\t': // opportunity to split
if col >= length {
buf.WriteByte('\n')
col = 0
} else {
if !lastNL {
buf.WriteRune(c)
}
col++ // count column
}
default:
buf.WriteRune(c)
col++
}
lastNL = c == '\n'
last = c
}
return buf.String()
}