-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
411 lines (404 loc) · 14.6 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
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
// Import dependencies.
const fs = require('fs');
const path = require('path');
const renderer = require('./views/html');
// Function converting a string according to a legend.
const titleOf = (string, legend) => legend[string] || string;
// Function identifying a list of keys of properties to be rendered.
const keysOf = object =>
object.order && object.order.format !== 'hide'
? object.order.data
: Object.keys(object).filter(key => object[key].format !== 'hide');
// Function rendering a stringable.
const stringOf = stringable => {
if (typeof stringable === 'string') {
return stringable;
}
else if (Array.isArray(stringable)) {
return stringable.map(element => stringOf(element)).join('');
}
else if (typeof stringable === 'object') {
const format = stringable.format;
const data = stringable.data;
switch(format) {
case 'address': {
const {point, city, region, postalCode, countryCode} = data;
let line1Item = point;
const line2Items = [city, region, postalCode, countryCode];
let line2Item = '';
if (line2Items[0]) {
const cityRegion = line2Items.slice(0, 2).filter(
item => item.length
).join(', ');
const cityRegionCode = [cityRegion, line2Items[2]].filter(
item => item.length
).join(' ');
line2Item = [cityRegionCode, line2Items[3]].filter(
item => item.length
).join(', ');
}
else {
line1Item = line2Items[2] = '';
line2Item = line2Items.filter(item => item.length).join(', ');
}
return renderer.multilineOf(
[line1Item, line2Item].filter(item => item.length)
);
}
case 'bulletList': {
return renderer.bulletListOf(
data.map(item => renderer.bulletItemOf(stringOf(item)))
);
}
case 'code': {
return renderer.codeOf(stringOf(data));
}
case 'edWork': {
const {location, startDate, endDate} = data;
let locationItem = '', dateItem = '';
if (location) {
locationItem = data.url ? renderer.hLinkOf(
location, data.url
) : location;
}
if (startDate || endDate) {
dateItem = `${startDate || ''}–${endDate || ''}`;
}
const subjectItem = data.subject || '';
const locationDateItem = [locationItem, dateItem].filter(
item => item.length
).join(', ');
return [locationDateItem, subjectItem].filter(
item => item.length
).join(': ');
}
case 'headedString': {
return renderer.headedStringOf(
stringOf(data.head),
stringOf(data.tail),
data.hasOwnProperty('delimiter') ? data.delimiter : ''
);
}
case 'hLink': {
return renderer.hLinkOf(data.label, data.href);
}
case 'mailLink': {
return renderer.mailLinkOf(data.label, data.href);
}
}
}
};
// Function rendering a heading.
const headOf = (object, type) => {
const headObject = object.head;
return headObject
? renderer.headOf(stringOf(headObject.data), headObject.size, type)
: '';
};
// Function prepending the title as a heading to an array of content.
const headedOf = (contentArray, title, size, type) => {
const headedContentArray = contentArray;
headedContentArray.unshift(renderer.headOf(title, size, type));
return headedContentArray;
};
// Function rendering the body of an extracted section.
const extractionOf = (fromArray, intoType, headStyle, title, legend) => {
const bandClass = `band ${headStyle || 'center'}`;
switch(intoType) {
case 'basicMainHeads': {
const {head, subhead} = fromArray[0];
const content = [
renderer.paragraphOf(stringOf(head), 1, 'center'),
renderer.paragraphOf(stringOf(subhead), 2, 'center under')
].join('\n');
return content;
}
case 'centeredStrongLines': {
const content = fromArray.map(
line => renderer.paragraphOf(stringOf(line), 3, 'center tight strong')
).join('\n');
return content;
}
case 'headedWorkVolLists': {
const subsections = fromArray.map(fromObject => {
const {organization, role, location, startDate, endDate, highlights}
= fromObject;
const head = renderer.headOf(renderer.multilineOf(
[
stringOf(organization),
stringOf(role),
stringOf(location),
stringOf(`${startDate} – ${endDate}`)
]
), 5, 'section-subhead strong');
const list = renderer.bulletListOf(highlights.map(
highlight => renderer.bulletItemOf(highlight)
));
const content = [head, list].join('\n');
return renderer.sectionOf(content, organization, '');
});
return headedOf(subsections, title, 3, bandClass).join('\n');
}
case 'headedWorkVolParagraphs': {
const subsections = fromArray.map(fromObject => {
const {organization, role, website, startDate, endDate, synopsis}
= fromObject;
const head = renderer.headOf(stringOf({
format: 'hLink',
data: {
label: organization,
href: website
}
}), 5, '');
const subhead = renderer.headOf(
stringOf(`${startDate} – ${endDate}: ${role}`), 6, ''
);
const content = [
head, subhead, renderer.paragraphOf(synopsis, 7, '')
].join('\n');
return renderer.sectionOf(content, organization, '');
});
return headedOf(subsections, title, 3, bandClass).join('\n');
}
case 'headedConferenceParagraphs': {
const subsections = fromArray.map(fromObject => {
const {name, date, title} = fromObject;
const head = renderer.headOf(`${name}, ${date}`, 5, '');
const titleLine = renderer.paragraphOf(`“${title}”`, 7, '');
const content = [head, titleLine].join('\n');
return renderer.sectionOf(content, name, '');
});
return headedOf(subsections, title, 3, bandClass).join('\n');
}
case 'headedEducationParagraphs': {
const subsections = fromArray.map(fromObject => {
const {
organization,
level,
diploma,
specialties,
website,
startDate,
endDate,
synopsis,
gpa,
transcript
} = fromObject;
const dates = `${startDate} – ${endDate}`;
const orgLink = stringOf({
format: 'hLink',
data: {
label: organization,
href: website
}
});
const head = renderer.headOf(`${orgLink}, ${dates}`, 5, '');
const recordData = [
level,
diploma,
specialties.join(', '),
gpa ? `${titleOf('gpa', legend)} ${gpa}` : '',
transcript ? stringOf({
format: 'hLink',
data: {
label: titleOf('transcript', legend),
href: transcript
}
}) : ''
];
const record = recordData.filter(item => item).join('; ');
const subhead = record ? renderer.headOf(record, 6, '') : '';
const heads = subhead ? [head, subhead] : [head];
const content = heads.concat(
renderer.paragraphOf(synopsis, 7, '')
).join('\n');
return renderer.sectionOf(content, organization, '');
});
return headedOf(subsections, title, 3, bandClass).join('\n');
}
case 'headedGrantParagraphs': {
const subsections = fromArray.map(fromObject => {
const {grantor, date, title} = fromObject;
const head = renderer.headOf(`${grantor}, ${date}`, 5, '');
const content = [head, `“${title}”`].join('\n');
return renderer.sectionOf(content, grantor, '');
});
return headedOf(subsections, title, 3, bandClass).join('\n');
}
case 'headedPublicationParagraphs': {
const subsections = fromArray.map(fromObject => {
const {authors, title, date, publisher, url} = fromObject;
const authorLine = authors.join(', ');
const titleLine = url ? stringOf({
format: 'hLink',
data: {
label: title,
href: url
}
}) : title;
const pubLine = `${publisher}: ${date}`;
const content = renderer.paragraphOf(
renderer.multilineOf([authorLine, titleLine, pubLine]), 7, ''
);
return renderer.sectionOf(content, title, '');
});
return headedOf(subsections, title, 3, bandClass).join('\n');
}
case 'introLists': {
const subsections = fromArray.map(fromObject => {
const {introTopic, list} = fromObject;
const head = renderer.headOf(
stringOf(introTopic), 5, 'strong'
);
const items = renderer.bulletListOf(list.map(
item => renderer.bulletItemOf(item)
));
const content = [head, items].join('\n');
return renderer.sectionOf(content, introTopic, '');
});
return headedOf(subsections, title, 3, bandClass).join('\n');
}
case 'headedGroupList': {
const subsections = fromArray.map(fromObject => {
const {organization, roles} = fromObject;
const head = renderer.headOf(
stringOf(organization), 5, 'strong'
);
const roleList = renderer.bulletListOf(roles.map(
role => renderer.bulletItemOf(role)
));
const content = [head, roleList].join('\n');
return renderer.sectionOf(content, organization, '');
});
return headedOf(subsections, title, 3, bandClass).join('\n');
}
}
return '';
};
/*
Function returning an HTML document rendering a jsonresume-theme-a11y
source object.
*/
exports.parse = a11yObject => {
const lang = a11yObject.lang ? a11yObject.lang.data : 'en';
const pageTitle = a11yObject.title ? a11yObject.title.data : 'Résumé';
const style = fs.readFileSync(path.join(__dirname, 'style.css'), 'utf-8');
const legend = a11yObject.legend ? a11yObject.legend.data : {};
const footPrefix = titleOf('creditTo', legend);
const footLink = {
format: 'hLink',
data: {
label: 'jsonresume-theme-a11y',
href: 'https://www.npmjs.com/package/jsonresume-theme-a11y'
}
};
const footContent = [footPrefix, stringOf(footLink)].join('');
const footSection = renderer.sectionOf(
footContent, 'credit', 'theme-credit'
);
const sectionNames = keysOf(a11yObject);
const sections = sectionNames.map(sectionName => {
const sectionObject = a11yObject[sectionName];
const format = sectionObject.format;
const title = titleOf(sectionObject.title, legend);
const data = sectionObject.data;
switch(format) {
case 'boxedBulletList': {
const head = headOf(data, 'section-head band center');
const items = data.list.map(
item => renderer.bulletItemOf(stringOf(item))
);
const list = renderer.bulletListOf(items);
const content = head ? [head, list].join('\n') : list;
return renderer.sectionOf(content, title, format);
}
case 'center': {
const lines = data.filter(lineSpec => lineSpec.text.length).map(
lineSpec => renderer.paragraphOf(
stringOf(lineSpec.text), lineSpec.size, ''
)
);
return renderer.sectionOf(lines.join('\n'), title, format);
}
case 'cornerPic': {
const image = renderer.imageOf(data.src, data.alt);
return renderer.sectionOf(image, title, format);
}
case 'extraction': {
const content = extractionOf(
a11yObject[data.from].data, data.into, data.headStyle, title, legend
);
return renderer.sectionOf(content, title, `section-head${
data.into === 'basicMainHeads' ? ' center' : ''
}`);
}
case 'left': {
const lines = data.map(line => renderer.paragraphOf(line, 7, ''));
return renderer.sectionOf(lines.join('\n'), title, format);
}
case 'rowTables': {
const rows = data.map(rowArray => renderer.plainRowOf(rowArray));
const tables = rows.map(
row => renderer.tableOf([renderer.tableBodyOf([row])], 'rowTable')
);
return renderer.sectionOf(tables.join('\n'), title, 'center');
}
case 'rowTablesCircled': {
const compactSectionOf = (content, title, format) => {
const quasiTable = renderer.squeezeBoxOf(content);
return renderer.sectionOf(quasiTable, title, format);
};
const head = headOf(data, '');
const rows = data.tables.map(rowArray => renderer.plainRowOf(rowArray));
const tables = rows.map(
row => renderer.tableOf([renderer.tableBodyOf([row])], 'rowTable')
);
const items = head ? [head, ...tables] : tables;
return compactSectionOf(
items.join('\n'), title, 'rowTablesCircled'
);
}
case 'tableLeftHeads': {
const sectionHead = headOf(data, '');
const sectionTable = data.table;
const tableCaption = sectionTable.caption;
const caption = renderer.captionOf(
tableCaption.size, tableCaption.data
);
const rows = sectionTable.data.map(
rowSpec => {
rowSpec.data.unshift(titleOf(rowSpec.label, legend));
return renderer.leftHeadRowOf(
rowSpec.data.map(cellSpec => stringOf(cellSpec))
);
}
);
const body = renderer.tableBodyOf(rows);
const table = renderer.tableOf([caption, body], 'tableLH');
const content = sectionHead ? [sectionHead, table].join('\n') : table;
return renderer.sectionOf(content, title, 'section-head center');
}
case 'tableTopHead': {
const sectionHead = headOf(data, '');
const sectionTable = data.table;
const tableCaption = sectionTable.caption;
const caption = renderer.captionOf(
tableCaption.size, tableCaption.data
);
const head = renderer.headRowOf(
sectionTable.label.map(string => titleOf(string, legend))
);
const etc = renderer.tableBodyOf(sectionTable.data.map(
rowSpec => renderer.plainRowOf(
rowSpec.map(cellSpec => stringOf(cellSpec))
)
));
const table = renderer.tableOf([caption, head, etc], 'tableTH');
const content = sectionHead ? [sectionHead, table].join('\n') : table;
return renderer.sectionOf(content, title, 'section-head center');
}
}
});
const sectionContent = sections.join('\n');
return renderer.pageOf(sectionContent, footSection, lang, pageTitle, style);
};