forked from aws/aws-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docstring.go
90 lines (81 loc) · 2.13 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
package api
import (
"bytes"
"html"
"regexp"
"strings"
)
var reNewline = regexp.MustCompile(`\r?\n`)
var reMultiSpace = regexp.MustCompile(`\s+`)
var reComments = regexp.MustCompile(`<!--.*?-->`)
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 = 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()
}