-
Notifications
You must be signed in to change notification settings - Fork 0
/
docgen.go
311 lines (273 loc) · 8.21 KB
/
docgen.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package decoder
import (
"bytes"
"encoding/json"
"fmt"
"html"
"io"
"regexp"
"strings"
)
type DocgenFormat string
const (
DocgenFormatMarkdown DocgenFormat = "markdown"
DocgenFormatHTML = "html"
DocgenFormatJSON = "json"
)
func Docgen(format DocgenFormat) ([]byte, error) {
var buf bytes.Buffer
err := WriteDocgen(&buf, format)
return buf.Bytes(), err
}
func WriteDocgen(w io.Writer, format DocgenFormat) error {
switch format {
case DocgenFormatMarkdown:
return writeDocgenMarkdown(w)
case DocgenFormatHTML:
return writeDocgenHTML(w)
case DocgenFormatJSON:
return writeDocgenJSON(w)
}
return fmt.Errorf("unknown format: %s", format)
}
func writeDocgenMarkdown(w io.Writer) error {
_, _ = w.Write([]byte("# API\n\n"))
_, _ = w.Write([]byte("## Modifiers\n\n"))
for i := 0; i < len(modBuf); i++ {
tuple := &modBuf[i]
_ = tuple.write(w, DocgenFormatMarkdown, false)
}
_, _ = w.Write([]byte("## Getters\n\n"))
for i := 0; i < len(getterBuf); i++ {
tuple := &getterBuf[i]
_ = tuple.write(w, DocgenFormatMarkdown, false)
}
_, _ = w.Write([]byte("## Callbacks\n\n"))
for i := 0; i < len(callbackBuf); i++ {
tuple := &callbackBuf[i]
_ = tuple.write(w, DocgenFormatMarkdown, false)
}
return nil
}
func writeDocgenHTML(w io.Writer) error {
_, _ = w.Write([]byte("<html><head><meta charset=\"utf-8\">"))
_, _ = w.Write([]byte(`<style>`))
_, _ = w.Write([]byte(`*{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Noto Sans,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji;font-size:16px;line-height:1.5;word-wrap:break-word}body{padding:1em}blockquote,code{margin:0}blockquote{background-color:#e7f3fe;border-left:6px solid #2196F3;padding:10px 15px;}code,pre{border-radius:6px;font-family:monospace;font-size:12px}li,pre{margin-top:.5rem}h1{font-size:var(--h1-size,32px)}h1,h2,h3,h4{font-weight:var(--base-text-weight-semibold,600)}h2{font-size:var(--h2-size,24px)}h3{font-size:var(--h3-size,20px)}p{margin-top:0;margin-bottom:10px}code{padding:.2em .4em;white-space:break-spaces;background-color:rgba(99,110,123,.2)}pre{padding:10px 9px;background-color:rgb(45,51,59,.2)}`))
_, _ = w.Write([]byte(`</style>`))
_, _ = w.Write([]byte("</head><body><h1>API</h1>"))
_, _ = w.Write([]byte(`<a name="mod"></a><h2>Modifiers</h2>`))
for i := 0; i < len(modBuf); i++ {
tuple := &modBuf[i]
_ = tuple.write(w, DocgenFormatHTML, false)
}
_, _ = w.Write([]byte(`<a name="cond"></a><h2>Getters</h2>`))
for i := 0; i < len(getterBuf); i++ {
tuple := &getterBuf[i]
_ = tuple.write(w, DocgenFormatHTML, false)
}
_, _ = w.Write([]byte(`<a name="condOK"></a><h2>Callbacks</h2>`))
for i := 0; i < len(callbackBuf); i++ {
tuple := &callbackBuf[i]
_ = tuple.write(w, DocgenFormatHTML, false)
}
_, _ = w.Write([]byte("</body></html>"))
return nil
}
func writeDocgenJSON(w io.Writer) error {
cpy := func(t *docgen) (r docgenJSON) {
r.Name = t.name
r.Alias = t.alias
r.Type = t.typ
r.Desc = t.desc
r.Note = t.note
r.Example = t.example
r.Inspector = t.ins
for i := 0; i < len(t.params); i++ {
p := &t.params[i]
r.Params = append(r.Params, docgenParamJSON{
Name: p.name,
Desc: p.desc,
})
}
return
}
var x docgenContainerJSON
for i := 0; i < len(modBuf); i++ {
x.Modifiers = append(x.Modifiers, cpy(&modBuf[i].docgen))
}
for i := 0; i < len(getterBuf); i++ {
x.Getters = append(x.Getters, cpy(&getterBuf[i].docgen))
}
for i := 0; i < len(callbackBuf); i++ {
x.Callbacks = append(x.Callbacks, cpy(&callbackBuf[i].docgen))
}
b, err := json.Marshal(&x)
_, _ = w.Write(b)
return err
}
type docgenParam struct {
name, desc string
}
type docgen struct {
name, alias, typ, desc, note, example, ins string
params []docgenParam
}
func (t *docgen) WithDescription(desc string) *docgen {
t.desc = desc
return t
}
func (t *docgen) WithType(typ string) *docgen {
t.typ = typ
return t
}
func (t *docgen) WithParam(param, desc string) *docgen {
t.params = append(t.params, docgenParam{
name: param,
desc: desc,
})
return t
}
func (t *docgen) WithNote(note string) *docgen {
t.note = note
return t
}
func (t *docgen) WithExample(example string) *docgen {
t.example = example
return t
}
func (t *docgen) write(w io.Writer, format DocgenFormat, compact bool) error {
htmlEscape := func(s string, nl2br bool) string {
s = html.EscapeString(s)
s = reDGHTMLCode.ReplaceAllString(s, "<code>$0</code>")
if nl2br {
s = strings.ReplaceAll(s, "\n", "<br/>")
}
s = strings.ReplaceAll(s, "`", "")
return s
}
switch {
case format == DocgenFormatMarkdown && compact == true:
_, _ = w.Write([]byte("* `"))
_, _ = w.Write([]byte(t.name))
if len(t.typ) > 0 {
_, _ = w.Write([]byte(" "))
_, _ = w.Write([]byte(t.typ))
}
if len(t.ins) > 0 {
_, _ = w.Write([]byte(" "))
_, _ = w.Write([]byte(t.ins))
}
_, _ = w.Write([]byte("`"))
if len(t.desc) > 0 {
_, _ = w.Write([]byte(" "))
_, _ = w.Write([]byte(t.desc))
}
_, _ = w.Write([]byte("\n"))
case format == DocgenFormatMarkdown && compact == false:
_, _ = w.Write([]byte("### "))
_, _ = w.Write([]byte(t.name))
_, _ = w.Write([]byte("\n"))
if len(t.alias) > 0 {
_, _ = w.Write([]byte("Alias: `" + t.alias + "`\n"))
}
_, _ = w.Write([]byte("\n"))
if len(t.params) > 0 {
_, _ = w.Write([]byte("Params:\n"))
for j := 0; j < len(t.params); j++ {
param := &t.params[j]
_, _ = w.Write([]byte("* `" + param.name + "`"))
if len(param.desc) > 0 {
_, _ = w.Write([]byte(" " + param.desc))
}
_, _ = w.Write([]byte("\n"))
}
_, _ = w.Write([]byte("\n"))
}
if len(t.desc) > 0 {
_, _ = w.Write([]byte(t.desc))
_, _ = w.Write([]byte("\n\n"))
}
if len(t.note) > 0 {
_, _ = w.Write([]byte("> **_NOTE:_** "))
_, _ = w.Write([]byte(t.note))
_, _ = w.Write([]byte("\n\n"))
}
if len(t.example) > 0 {
_, _ = w.Write([]byte("Example:\n```\n"))
_, _ = w.Write([]byte(t.example))
_, _ = w.Write([]byte("\n```\n\n"))
}
case format == DocgenFormatHTML && compact == true:
_, _ = w.Write([]byte("<li><code>"))
_, _ = w.Write([]byte(t.name))
if len(t.typ) > 0 {
_, _ = w.Write([]byte(" "))
_, _ = w.Write([]byte(t.typ))
}
if len(t.ins) > 0 {
_, _ = w.Write([]byte(" "))
_, _ = w.Write([]byte(t.ins))
}
_, _ = w.Write([]byte("</code>"))
if len(t.desc) > 0 {
_, _ = w.Write([]byte(" "))
_, _ = w.Write([]byte(htmlEscape(t.desc, false)))
}
_, _ = w.Write([]byte("</li>"))
case format == DocgenFormatHTML && compact == false:
_, _ = w.Write([]byte("<h3>" + t.name + "</h3>"))
if len(t.alias) > 0 {
_, _ = w.Write([]byte("<block>Alias: <code>" + t.alias + "</code></block>"))
}
if len(t.params) > 0 {
_, _ = w.Write([]byte("<div>Params:<ul>"))
for j := 0; j < len(t.params); j++ {
param := &t.params[j]
_, _ = w.Write([]byte("<li><code>" + param.name + "</code>"))
if len(param.desc) > 0 {
_, _ = w.Write([]byte(" " + htmlEscape(param.desc, true)))
}
_, _ = w.Write([]byte("</li>"))
}
_, _ = w.Write([]byte("</ul></div>"))
}
if len(t.desc) > 0 {
_, _ = w.Write([]byte("<p>"))
_, _ = w.Write([]byte(htmlEscape(t.desc, true)))
_, _ = w.Write([]byte("</p>"))
}
if len(t.note) > 0 {
_, _ = w.Write([]byte("<blockquote>"))
_, _ = w.Write([]byte(htmlEscape(t.note, false)))
_, _ = w.Write([]byte("</blockquote>"))
}
if len(t.example) > 0 {
_, _ = w.Write([]byte("<div>Example:<pre>"))
_, _ = w.Write([]byte(htmlEscape(t.example, false)))
_, _ = w.Write([]byte("</pre></div>"))
}
}
return nil
}
var reDGHTMLCode = regexp.MustCompile("`([^`]+)`")
type (
docgenJSON struct {
Name string `json:"name,omitempty"`
Alias string `json:"alias,omitempty"`
Type string `json:"type,omitempty"`
Desc string `json:"desc,omitempty"`
Note string `json:"note,omitempty"`
Example string `json:"example,omitempty"`
Inspector string `json:"inspector,omitempty"`
Params []docgenParamJSON `json:"params,omitempty"`
}
docgenParamJSON struct {
Name string `json:"name,omitempty"`
Desc string `json:"desc,omitempty"`
}
docgenContainerJSON struct {
Modifiers []docgenJSON `json:"modifiers,omitempty"`
Getters []docgenJSON `json:"conditionHelpers,omitempty"`
Callbacks []docgenJSON `json:"conditionOKHelpers,omitempty"`
}
)