-
Notifications
You must be signed in to change notification settings - Fork 20
/
doc_generator.go
498 lines (401 loc) · 12.4 KB
/
doc_generator.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
* Cadence docgen - The Cadence documentation generator
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package docgen
import (
"bytes"
"fmt"
"io"
"os"
"path"
"strings"
"text/template"
"github.com/onflow/cadence/runtime/ast"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/parser"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"github.com/onflow/cadence-tools/docgen/templates"
)
const nameSeparator = "_"
const newline = "\n"
const mdFileExt = ".md"
const paramPrefix = "@param "
const returnPrefix = "@return "
const baseTemplate = "base-template"
const compositeFullTemplate = "composite-full-template"
var templateFiles = []string{
baseTemplate,
compositeFullTemplate,
"composite-members-template",
"function-template",
"composite-template",
"field-template",
"enum-template",
"enum-case-template",
"initializer-template",
"event-template",
}
type DocGenerator struct {
entryPageGen *template.Template
compositePageGen *template.Template
typeNames []string
outputDir string
files InMemoryFiles
}
type InMemoryFiles map[string][]byte
type InMemoryFileWriter struct {
fileName string
buf *bytes.Buffer
files InMemoryFiles
}
func NewInMemoryFileWriter(files InMemoryFiles, fileName string) *InMemoryFileWriter {
return &InMemoryFileWriter{
fileName: fileName,
files: files,
buf: &bytes.Buffer{},
}
}
func (w *InMemoryFileWriter) Write(bytes []byte) (n int, err error) {
return w.buf.Write(bytes)
}
func (w *InMemoryFileWriter) Close() error {
w.files[w.fileName] = w.buf.Bytes()
w.buf = nil
return nil
}
func NewDocGenerator() *DocGenerator {
gen := &DocGenerator{}
functions := newTemplateFunctions[ast.Declaration](ASTDeclarationTemplateFunctions{})
functions["fileName"] = func(decl ast.Declaration) string {
fileNamePrefix := gen.currentFileName()
if len(fileNamePrefix) == 0 {
return fmt.Sprint(decl.DeclarationIdentifier().String(), mdFileExt)
}
return fmt.Sprint(fileNamePrefix, nameSeparator, decl.DeclarationIdentifier().String(), mdFileExt)
}
templateProvider := templates.NewMarkdownTemplateProvider()
gen.entryPageGen = newTemplate(baseTemplate, templateProvider, functions)
gen.compositePageGen = newTemplate(compositeFullTemplate, templateProvider, functions)
return gen
}
func newTemplate(name string, templateProvider templates.TemplateProvider, functions template.FuncMap) *template.Template {
rootTemplate := template.New(name).Funcs(functions)
for _, templateFile := range templateFiles {
content, err := templateProvider.Get(templateFile)
if err != nil {
panic(err)
}
var tmpl *template.Template
if templateFile == name {
tmpl = rootTemplate
} else {
tmpl = rootTemplate.New(name)
}
_, err = tmpl.Parse(content)
if err != nil {
panic(err)
}
}
return rootTemplate
}
func (gen *DocGenerator) Generate(source string, outputDir string) error {
gen.outputDir = outputDir
gen.typeNames = nil
program, err := parser.ParseProgram([]byte(source), nil)
if err != nil {
return err
}
return gen.genProgram(program)
}
func (gen *DocGenerator) GenerateInMemory(source string) (InMemoryFiles, error) {
gen.files = InMemoryFiles{}
gen.typeNames = nil
program, err := parser.ParseProgram([]byte(source), nil)
if err != nil {
return nil, err
}
err = gen.genProgram(program)
if err != nil {
return nil, err
}
return gen.files, nil
}
func (gen *DocGenerator) genProgram(program *ast.Program) error {
// If the program does not have a sole declaration,
// i.e. it has multiple top level declarations,
// then generate an entry page.
if program.SoleContractDeclaration() == nil &&
program.SoleContractInterfaceDeclaration() == nil {
// Generate entry page
// TODO: file name 'index' can conflict with struct names, resulting an overwrite.
f, err := gen.fileWriter("index.md")
if err != nil {
return err
}
defer f.Close()
err = gen.entryPageGen.Execute(f, program)
if err != nil {
return err
}
}
// Generate dedicated pages for all the nested composite declarations
return gen.genDeclarations(program.Declarations())
}
func (gen *DocGenerator) genDeclarations(decls []ast.Declaration) error {
var err error
for _, decl := range decls {
switch astDecl := decl.(type) {
case *ast.CompositeDeclaration:
err = gen.genCompositeDeclaration(astDecl)
case *ast.InterfaceDeclaration:
err = gen.genInterfaceDeclaration(astDecl)
default:
// do nothing
}
if err != nil {
return err
}
}
return nil
}
func (gen *DocGenerator) genCompositeDeclaration(declaration *ast.CompositeDeclaration) error {
if declaration.DeclarationKind() == common.DeclarationKindEvent {
return nil
}
declName := declaration.DeclarationIdentifier().String()
return gen.genCompositeDecl(declName, declaration.Members, declaration)
}
func (gen *DocGenerator) genInterfaceDeclaration(declaration *ast.InterfaceDeclaration) error {
declName := declaration.DeclarationIdentifier().String()
return gen.genCompositeDecl(declName, declaration.Members, declaration)
}
func (gen *DocGenerator) genCompositeDecl(name string, members *ast.Members, decl ast.Declaration) error {
gen.typeNames = append(gen.typeNames, name)
defer func() {
lastIndex := len(gen.typeNames) - 1
gen.typeNames = gen.typeNames[:lastIndex]
}()
fileName := fmt.Sprint(gen.currentFileName(), mdFileExt)
f, err := gen.fileWriter(fileName)
if err != nil {
return err
}
defer f.Close()
err = gen.compositePageGen.Execute(f, decl)
if err != nil {
return err
}
return gen.genDeclarations(members.Declarations())
}
func (gen *DocGenerator) fileWriter(fileName string) (io.WriteCloser, error) {
if gen.files == nil {
return os.Create(path.Join(gen.outputDir, fileName))
}
return NewInMemoryFileWriter(gen.files, fileName), nil
}
func (gen *DocGenerator) currentFileName() string {
return strings.Join(gen.typeNames, nameSeparator)
}
type ElementTemplateFunctions[T any] interface {
HasConformance(T) bool
IsEnum(T) bool
DeclKeywords(T) string
DeclTypeTitle(T) string
GenInitializer(T) bool
Enums(declarations []T) []T
StructsAndResources([]T) []T
Events([]T) []T
}
type ASTDeclarationTemplateFunctions struct{}
var _ ElementTemplateFunctions[ast.Declaration] = ASTDeclarationTemplateFunctions{}
func (ASTDeclarationTemplateFunctions) HasConformance(declaration ast.Declaration) bool {
switch declaration.DeclarationKind() {
case common.DeclarationKindStructure,
common.DeclarationKindResource,
common.DeclarationKindContract,
common.DeclarationKindEnum:
return true
default:
return false
}
}
func (ASTDeclarationTemplateFunctions) IsEnum(declaration ast.Declaration) bool {
return declaration.DeclarationKind() == common.DeclarationKindEnum
}
func (ASTDeclarationTemplateFunctions) DeclKeywords(declaration ast.Declaration) string {
var parts []string
accessKeyword := declaration.DeclarationAccess().Keyword()
if len(accessKeyword) > 0 {
parts = append(parts, accessKeyword)
}
var kindKeyword string
kind := declaration.DeclarationKind()
if kind == common.DeclarationKindField {
kindKeyword = declaration.(*ast.FieldDeclaration).VariableKind.Keyword()
} else {
kindKeyword = kind.Keywords()
}
if len(kindKeyword) > 0 {
parts = append(parts, kindKeyword)
}
return strings.Join(parts, " ")
}
func (ASTDeclarationTemplateFunctions) DeclTypeTitle(declaration ast.Declaration) string {
return cases.Title(language.English).
String(declaration.DeclarationKind().Keywords())
}
func (ASTDeclarationTemplateFunctions) GenInitializer(declaration ast.Declaration) bool {
switch declaration.DeclarationKind() {
case common.DeclarationKindStructure,
common.DeclarationKindResource:
return true
default:
return false
}
}
func (ASTDeclarationTemplateFunctions) Enums(declarations []ast.Declaration) []ast.Declaration {
var enums []ast.Declaration
for _, decl := range declarations {
if decl.DeclarationKind() == common.DeclarationKindEnum {
enums = append(enums, decl)
}
}
return enums
}
func (ASTDeclarationTemplateFunctions) StructsAndResources(declarations []ast.Declaration) []ast.Declaration {
var structsAndResources []ast.Declaration
for _, declaration := range declarations {
switch declaration.DeclarationKind() {
case common.DeclarationKindStructure,
common.DeclarationKindResource:
structsAndResources = append(structsAndResources, declaration)
}
}
return structsAndResources
}
func (ASTDeclarationTemplateFunctions) Events(declarations []ast.Declaration) []ast.Declaration {
var eventDeclarations []ast.Declaration
for _, decl := range declarations {
if decl.DeclarationKind() == common.DeclarationKindEvent {
eventDeclarations = append(eventDeclarations, decl)
}
}
return eventDeclarations
}
func newTemplateFunctions[T any](
elementFunctions ElementTemplateFunctions[T],
) template.FuncMap {
return template.FuncMap{
"hasConformance": elementFunctions.HasConformance,
"isEnum": elementFunctions.IsEnum,
"declKeywords": elementFunctions.DeclKeywords,
"declTypeTitle": elementFunctions.DeclTypeTitle,
"genInitializer": elementFunctions.GenInitializer,
"enums": elementFunctions.Enums,
"structsAndResources": elementFunctions.StructsAndResources,
"events": elementFunctions.Events,
"formatDoc": formatDoc,
"formatFuncDoc": formatFuncDoc,
}
}
func formatDoc(docString string) string {
var builder strings.Builder
// Trim leading and trailing empty lines
docString = strings.TrimSpace(docString)
lines := strings.Split(docString, newline)
for i, line := range lines {
formattedLine := strings.TrimSpace(line)
if i > 0 {
builder.WriteString(newline)
}
builder.WriteString(formattedLine)
}
return builder.String()
}
func formatFuncDoc(docString string, genReturnType bool) string {
var builder strings.Builder
var params []string
var isPrevLineEmpty bool
var docLines int
var returnDoc string
// Trim leading and trailing empty lines
docString = strings.TrimSpace(docString)
lines := strings.Split(docString, newline)
for _, line := range lines {
formattedLine := strings.TrimSpace(line)
if strings.HasPrefix(formattedLine, paramPrefix) {
paramInfo := strings.TrimPrefix(formattedLine, paramPrefix)
colonIndex := strings.IndexByte(paramInfo, ':')
// If colon isn't there, cannot determine the param name.
// Hence treat as a normal doc line.
if colonIndex >= 0 {
paramName := strings.TrimSpace(paramInfo[0:colonIndex])
// If param name is empty, treat as a normal doc line.
if len(paramName) > 0 {
paramDoc := strings.TrimSpace(paramInfo[colonIndex+1:])
var formattedParam string
if len(paramDoc) > 0 {
formattedParam = fmt.Sprintf(" - %s : _%s_", paramName, paramDoc)
} else {
formattedParam = fmt.Sprintf(" - %s", paramName)
}
params = append(params, formattedParam)
continue
}
}
} else if genReturnType && strings.HasPrefix(formattedLine, returnPrefix) {
returnDoc = formattedLine
continue
}
// Ignore the line if its a consecutive blank line.
isLineEmpty := len(formattedLine) == 0
if isPrevLineEmpty && isLineEmpty {
continue
}
if docLines > 0 {
builder.WriteString(newline)
}
builder.WriteString(formattedLine)
isPrevLineEmpty = isLineEmpty
docLines++
}
// Print the parameters
if len(params) > 0 {
if !isPrevLineEmpty {
builder.WriteString(newline)
}
builder.WriteString(newline)
builder.WriteString("Parameters:")
for _, param := range params {
builder.WriteString(newline)
builder.WriteString(param)
}
isPrevLineEmpty = false
}
// Print the return type info
if len(returnDoc) > 0 {
if !isPrevLineEmpty {
builder.WriteString(newline)
}
builder.WriteString(newline)
returnInfo := strings.TrimPrefix(returnDoc, returnPrefix)
returnInfo = strings.TrimSpace(returnInfo)
builder.WriteString(fmt.Sprintf("Returns: %s", returnInfo))
}
return builder.String()
}