-
Notifications
You must be signed in to change notification settings - Fork 4
/
standard-docs-generator.b
209 lines (162 loc) · 5.67 KB
/
standard-docs-generator.b
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
import os
import ast
import json
import colors
class ParsedFile {
var classes = []
var functions = []
var variables = []
}
class ParsedClass {
var methods = []
var properties = []
var doc
}
def cite(str) {
return (str + '\n').
replace('/\\@note/', '> -').
replace('/\\@([a-zA-Z_]+)[ ]*([^\\n]*)/',
' <div class="cite"><span class="hint">$1</span> <span>$2</span></div>\n')
}
def get_class_docs(module, klass) {
var result = "\n\n### _class_ ${klass.name} ${klass.superclass ? ' < _${klass.superclass}_' : nil}\n---\n\n${klass.doc}\n\n"
if klass.properties {
var props = []
for prop in klass.properties {
if prop.doc {
props.append(get_var_docs(klass.name, klass.name, prop, prop.is_static))
}
}
if props {
result += '\n#### class ${klass.name} properties\n---\n\n'
result += '\n\n'.join(props) + '\n'
}
}
if klass.methods {
var methods = []
for method in klass.methods {
if method.doc {
methods.append(get_function_docs(nil, klass.name, method, method.is_static))
}
}
if methods {
result += '\n#### class ${klass.name} methods\n---\n\n'
result += '\n\n'.join(methods) + '\n'
}
}
return result
}
def get_function_docs(module, klass, function, is_static) {
var line1 = function.doc.match('/^[^)]+\)/')
if line1{
line1 = line1[0].
replace('/([a-zA-Z0-9_]+):/', '_$1_:').
replace('/([a-zA-Z0-9_]+)\(/', is_static ? '_static_ **$1**(' : '**$1**(')
}
var body = cite('\n'.join(function.doc.split('\n')[1,]).trim())
return "{:#${module}_${klass}_${function.name}} ${module ? '_${module}_.' : nil}${line1}\n: ${body}"
}
def get_var_docs(module, klass, var_data, is_static) {
return '{:#${module}_${klass}_${var_data.name}}${is_static ? " _static_" : nil} _${module ? module + '.' : nil}_**${var_data.name}**\n: ${cite(var_data.doc)}'
}
def get_docs(module, parse_list) {
var result = ParsedFile()
for parse_item in parse_list {
# we are only interested in declarations
if instance_of(parse_item, ast.Decl) {
if(parse_item.doc) {
if instance_of(parse_item, ast.VarDecl) {
if !parse_item.name.starts_with('_') and parse_item.doc
result.variables.append(get_var_docs(module, nil, parse_item))
} else if instance_of(parse_item, ast.FunctionDecl) {
if !parse_item.name.starts_with('_') and! parse_item.name.starts_with('@') and parse_item.doc
result.functions.append(get_function_docs(module, nil, parse_item))
} else if instance_of(parse_item, ast.ClassDecl) {
if !parse_item.name.starts_with('_') and parse_item.doc
result.classes.append(get_class_docs(module, parse_item))
}
}
}
}
return result
}
def get_module_desc(f) {
var lines = file(f).read().split('\n')
var result = ''
var i = 0
while lines[i].starts_with('#') {
if !lines[i].match('/^\\s*#\\s*@(module|copyright)/') {
result += lines[i].replace('/^#\\s?/', '') + '\n'
}
i++
}
return result.trim()
}
def create_module_doc(index_file, i, module_name, docs) {
var module_description = get_module_desc(index_file)
# var module_description = ''
var content = '---\n'+
'layout: default\n'+
'title: ${module_name}\n'+
'parent: Standard Library\n'+
'nav_order: ${i}\n'+
'permalink: /standard/${module_name}.html\n'+
'---\n\n# ${module_name}\n\n${module_description}\n\n'
if docs.variables {
content += '\n\n<h2>Properties</h2><hr>\n\n'
content += '\n\n^\n'.join(docs.variables.compact()) + '\n\n'
}
if docs.functions {
content += '\n\n<h2>Functions</h2><hr>\n\n'
content += '\n\n^\n'.join(docs.functions.compact()) + '\n\n'
}
if docs.classes {
content += '\n\n<h2>Classes</h2><hr>\n\n'
content += '\n\n^\n'.join(docs.classes.compact()) + '\n\n'
}
file('docs/standard/${module_name}.markdown', 'w').write(content)
}
def process_line(source) {
echo colors.text(' Processing ${source}', colors.style.italic)
}
if os.args.length() < 3 {
echo 'Please specify source path'
} else {
var source_dir = os.args[2]
var sources = os.read_dir(source_dir)
sources.sort()
echo colors.text('Started generating Blade standard library documentation...', colors.text_color.blue)
echo ''
iter var i = 0; i < sources.length(); i++ {
var source = sources[i], module_name = source.replace('/\\.b/', '')
# ignore the http2 module.
if source.starts_with('http2') or source.starts_with('zip2')
continue
if source.ends_with('.b') and !source.starts_with('_') { # it's a file
process_line('${source_dir}/${source}')
var parse_list = ast.parse(file('${source_dir}/${source}').read())
create_module_doc('${source_dir}/${source}', i + 1, module_name, get_docs(module_name, parse_list))
} else { # it's a directory
if !source.starts_with('.') and !source.starts_with('_') {
process_line('${source_dir}/${source}')
var parsed = []
for f in os.read_dir('${source_dir}/${source}') {
if !f.starts_with('.') and !f.starts_with('_') {
process_line('${source_dir}/${source}/${f}')
parsed.append(ast.parse(file('${source_dir}/${source}/${f}').read()))
}
}
var parse_list = []
for p in parsed {
for j in p {
parse_list.append(j)
}
}
create_module_doc('${source_dir}/${source}/index.b', i + 1, module_name, get_docs(module_name, parse_list))
}
}
}
echo ''
echo colors.text(colors.text('Successfully generated Blade standard library documentation!', colors.text_color.green), colors.style.bold)
echo ''
}