-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
289 lines (265 loc) · 8.43 KB
/
parser.js
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
let order;
let filename;
let sections;
/**
* Entry point for parsing
* Grammar:
* page: name, blurb?, sections
* name: string
* blurb: string
* sections: section[]
* section: name, blurb?, (sections|columns|examples)?
* columns: example[]
* examples: example[]
* example: name, content, note?
* content: code,
* note: string
* @param {Object} json json to be parsed
* @returns {Object} output
*/
let entry = (projectOrder, jsons) => {
order = projectOrder;
let mainJSON = getMainJSON(jsons)
filename = mainJSON.filename || error('Page filename missing', mainJSON);
return page(jsons);
}
let page = jsons => {
let output = [];
let mainJSON = getMainJSON(jsons);
if(mainJSON.name) {
output.push({
type: 'heading',
level: 1,
text: mainJSON.name,
});
}
else {
error(`Expected ${order[0]}'s page '${filename}' to have a name.`, mainJSON);
}
if(mainJSON.blurb) {
output.push({
type: 'paragraph',
text: mainJSON.blurb,
})
}
// Each section of the page will be in this hash
let sections = {};
// This set will maintain their order
let sectionOrder = new Set();
// Link the project's sections
for(let project of order) {
// Get this project's page json
let json = jsons[project];
// If this project isn't here, skip it
if (!json) {
continue;
}
// Sections? If not we have a problem
if(json.sections) {
// For each section, either tack it on to an existing section array or make a new one
for(let sec of json.sections) {
// Sections need names. We're using them as keys, so check now.
if(!sec.name) {
error(`Expected a section of ${project}'s page '${filename}' to have a name.`, json);
}
// Have we seen this section name before?
if(sections[sec.name]) {
// Just tack it on.
sections[sec.name][project] = sec;
}
else {
// Haven't seen it, so initialize and add to our set.
sections[sec.name] = {};
sections[sec.name][project] = sec;
sectionOrder.add(sec.name);
}
}
}
else {
error(`Expected ${project}'s page '${filename}' to have sections.`, json);
}
}
// parse each section
for(let sec of sectionOrder) {
output = output.concat(section(sections[sec], 2));
}
output.push({
type: 'endpage'
});
return [{
type: 'sections',
sections: [...sectionOrder],
}, ...output];
};
let section = (jsons, level) => {
let output = [];
let mainJSON = getMainJSON(jsons);
// We know each section has a name because we check in page
output.push({
type: 'heading',
level: level,
text: mainJSON.name,
});
if(mainJSON.blurb) {
output.push({
type: 'paragraph',
text: mainJSON.blurb,
})
}
// Each section of the page will be in this hash
let sections = {};
// This set will maintain their order
let sectionsOrder = new Set();
// Each section of the page will be in this hash
let columnExamples = {};
// This set will maintain their order
let columnExamplesOrder = new Set();
// Each section of the page will be in this hash
let examples = {};
// This set will maintain their order
let examplesOrder = new Set();
// Link the project's stuff
for(let project of order) {
// Get this project's section json
let json = jsons[project];
// If this project isn't here, skip it
if (!json) {
continue;
}
if(json.sections) {
// For each section, either tack it on to an existing section array or make a new one
for(let sec of json.sections) {
// Sections need names. We're using them as keys, so check now.
if(!sec.name) {
error(`Expected the ${mainJSON.name} section of ${project}'s page '${filename}' to have a name.`, sec);
}
// Have we seen this section name before?
if(sections[sec.name]) {
// Just tack it on.
sections[sec.name][project] = sec;
}
else {
// Haven't seen it, so initialize and add to our set.
sections[sec.name] = {};
sections[sec.name][project] = sec;
sectionsOrder.add(sec.name);
}
}
}
if(json.columns) {
for(let ex of json.columns) {
// Examples need names. We're using them as keys, so check now.
if(!ex.name) {
error(`Expected an example in the ${mainJSON.name} section of ${project}'s page '${filename}' to have a name.`, ex);
}
// Have we seen this example name before?
if(columnExamples[ex.name]) {
// Just tack it on.
columnExamples[ex.name][project] = ex;
}
else {
// Haven't seen it, so initialize and add to our set.
columnExamples[ex.name] = {};
columnExamples[ex.name][project] = ex;
columnExamplesOrder.add(ex.name);
}
}
}
if(json.examples) {
for(let ex of json.examples) {
// Examples need names. We're using them as keys, so check now.
if(!ex.name) {
error(`Expected an example in the ${mainJSON.name} section of ${project}'s page '${filename}' to have a name.`, ex);
}
// Have we seen this example name before?
if(examples[ex.name]) {
// Just tack it on.
examples[ex.name][project] = ex;
}
else {
// Haven't seen it, so initialize and add to our set.
examples[ex.name] = {};
examples[ex.name][project] = ex;
examplesOrder.add(ex.name);
}
}
}
}
if(examplesOrder.size) {
for(let ex of examplesOrder) {
output.push(example(examples[ex]));
}
}
if(columnExamplesOrder.size) {
let cols = [[],[]];
let left = true;
for(let ex of columnExamplesOrder) {
if (left) {
cols[0].push(example(columnExamples[ex]));
}
else {
cols[1].push(example(columnExamples[ex]));
}
left = !left;
}
output.push({
type: 'columns',
cols: cols,
});
}
if(sectionsOrder.size) {
for(let sec of sectionsOrder) {
output = output.concat(section(sections[sec], level + 1));
}
}
return output;
};
let example = jsons => {
let mainJSON = getMainJSON(jsons);
let output = {
type: 'example',
title: mainJSON.name,
};
for(let project of order) {
// Get this project's section json
let json = jsons[project];
// If this project isn't here, skip it
if (!json) {
continue;
}
if (json.content) {
if (!output.content) {
output.content = [];
}
output.content.push({
project: project,
content: json.content,
});
}
else {
error('Expected example to have content.', json);
}
if (json.note) {
if (!output.note) {
output.note = [];
}
output.note.push({
project: project,
note: json.note,
});
}
}
return output;
};
let getMainJSON = jsons => {
for(let project of order) {
if(jsons[project]) {
return jsons[project];
}
}
error('No projects found.', jsons);
}
let error = (msg, json) => {
throw new Error(`Parse error: ${msg} Raw output: ${JSON.stringify(json)}`);
}
module.exports = entry;