-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.js
More file actions
240 lines (189 loc) · 4.52 KB
/
compile.js
File metadata and controls
240 lines (189 loc) · 4.52 KB
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
//
// Produces a single page (searchable) document with a TOC.
//
// Yeah, its a lot of messy string templating. If you have
// a better idea, feel free to make a pull request :)
//
const fs = require('fs')
const marked = require('marked')
marked.setOptions({
renderer: new marked.Renderer()
})
const escapeString = s => {
s = s.replace('&', '&')
.replace('>', '>')
.replace('<', '<')
.replace('\'', ''')
.replace('"', '"')
.replace('`', '`')
return s
}
//
// Return Types
//
const templateReturnType = type => `
|Return Type|Description|
|:---|:---|
|${type.return.TYPE}|${type.return.comment}|
`
//
// Properties
//
const templateProperties = members => {
const props = members.filter(m => m.type === 'property')
if (!props.length) {
return '' // there are no operators
}
let header = [
`### PROPERTIES\n`,
`|Type|Name|Description|`,
`|:---|:---|:----------|`
]
const rows = props.map(prop =>
`|${prop.TYPE}|${prop.name}|${prop.comment || ''}|`
)
return [...header, ...rows].join('\n')
}
//
// Operators
//
const templateOperators = members => {
const operators = members.filter(m => m.type === 'operator')
if (!operators.length) {
return '' // there are no operators
}
let header = [
`### OPERATORS\n`,
`|Operator|Description|`,
`|:-------|:----------|`
]
const rows = operators.map(operator =>
`|${operator.name}|${operator.comment || ''}|`
)
return [...header, ...rows].join('\n')
}
//
// Params
//
const templateParams = type => {
if (!type.params || !Object.keys(type.params).length) {
return ''
}
return [
``,
`|Parameter|Default|Description|`,
`|:---|:---|:---|`,
...Object.keys(type.params).map(key => {
const param = type.params[key]
return `|${key}|${param.default || ''}|${param.comment || ''}|`
})
].join('\n')
}
//
// Methods
//
const templateMethods = members => {
const methods = members.filter(m => m.type === 'method')
if (!methods.length) {
return '' // there are no operators
}
const header = [`### METHODS\n`]
const rows = methods.map(method => {
return [
`#### ${method.name}`,
`${method.comment}`,
`\`\`\``,
method.raw,
`\`\`\``,
templateReturnType(method),
templateParams(method),
``
].join('\n')
})
return [...header, ...rows].join('\n')
}
//
// Constructors
//
const templateConstructors = members => {
const ctors = members.filter(m => m.type === 'constructor')
if (!ctors.length) {
return '' // there are no operators
}
const header = [`### CONSTRUCTORS\n`]
const rows = ctors.map(ctor => {
return [
`${ctor.comment || ''}`,
`\`\`\``,
ctor.raw,
`\`\`\``,
templateParams(ctor),
``
].join('\n')
})
return [...header, ...rows].join('\n')
}
const templateClassOrStruct = (key, type) => `
## ${escapeString(key)}
${type.comment || 'Undocumented. Do not use.'}
${templateOperators(type.members)}
${templateConstructors(type.members)}
${templateMethods(type.members)}
${templateProperties(type.members)}
`
const templateFunction = (key, type) => `
## ${escapeString(key)}
${type.comment || 'Undocumented. Do not use.'}
\`\`\`
${type.raw}
\`\`\`
${templateReturnType(type)}
${templateParams(type)}
`
const createMD = ast => {
if (!ast.namespace) return ''
return `
# ${ast.namespace.join('::')}
${
Object.keys(ast.types).map(key => {
const type = ast.types[key]
return type.members
? templateClassOrStruct(key, type)
: templateFunction(key, type)
}).join('\n')
}
`
}
module.exports = buffers => {
const toc = {}
const groups = {}
buffers.forEach(ast => {
if (!ast.namespace) return
const ns = ast.namespace.join('::')
const output = marked(createMD(ast))
if (ast.types) {
toc[ns] = toc[ns] || []
toc[ns].push(...Object.keys(ast.types))
}
groups[ns] = groups[ns] || []
groups[ns].push(output)
})
const flattened = []
Object.keys(toc).forEach(key => {
const k = escapeString(key)
flattened.push(`<a href="${k}">${k}</a><ul>`)
flattened.push(...toc[key].map(type =>
`<li>${escapeString(type)}</li>`))
flattened.push(`</ul>`)
})
const output = `
<div class="toc">
${flattened.join('\n')}
</div>
<div class="docs">
${Object.values(groups).map(buf => buf.join('')).join('\n')}
</div>
`
const template = fs.readFileSync(`${__dirname}/template.txt`, 'utf8')
process.stdout.write(template.replace('DOCS', output))
}