-
Notifications
You must be signed in to change notification settings - Fork 2
/
cards.js
457 lines (385 loc) · 12.6 KB
/
cards.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
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
const extractUrls = require("extract-urls");
const qrCode = require("qrcode");
const escape = require("escape-html");
const { textOnlyLength, cleanParagraphTags, trimmedMean } = require("./tools");
async function getSplitCards(chunks, headline, formatting) {
var splitCards = [];
if (!formatting) {
throw new Error("no formatting supplied");
}
// get formattings (split for default and additional pages)
const defaultPagesFormatting = getFormatting(formatting, false);
const additionalPagesFormatting = getFormatting(formatting, true);
// iterate all chunks
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
// check that chunk always begins with list-item-card
if (chunk[0].type !== "list-item-card") {
throw new Error("first chunk element should be of type list-item-card");
}
// get child nodes of first chunk element
const chunkElementOneChildNodes = chunk[0].content.childNodes;
var number = chunk[0].number;
// handle patterns of the beginning of a chunk
const isPandOl = isExpectedTags(chunkElementOneChildNodes, ["p", "ol"]); // TODO name better
const isP = isExpectedTags(chunkElementOneChildNodes, ["p"]);
if (isPandOl) {
splitCards = splitCards.concat(
handleParagraphAndOneList(
chunkElementOneChildNodes,
headline,
number,
null,
defaultPagesFormatting
)
);
} else if (isP) {
splitCards = splitCards.concat(
handleParagraphs(
chunkElementOneChildNodes,
headline,
number,
defaultPagesFormatting
)
);
}
// handle chunk content after initial element
const afterListItemCard = chunk.slice(1);
if (afterListItemCard.length) {
number += "+";
const afterListItemcardElements = afterListItemCard.map((c) => c.content);
if (isExpectedTags(afterListItemcardElements, ["p", "ol"])) {
checkParagraphPlusListStructure(afterListItemCard); // TODO maybe delete!
splitCards = splitCards.concat(
handleParagraphAndOneList(
afterListItemcardElements,
headline,
number,
"extraList",
additionalPagesFormatting
)
);
} else if (isOnlyParagraphs(afterListItemCard)) {
splitCards = splitCards.concat(
handleParagraphs(
afterListItemcardElements,
headline,
number,
additionalPagesFormatting
)
);
}
}
}
// clean and enhance cards
for (card of splitCards) {
await addBarcodes(card);
await prepareContent(card);
}
return splitCards;
}
function getFormatting(formatting, isAdditionalContent) {
if (!formatting.fonts.pages) {
return formatting;
}
const newFormatting = JSON.parse(JSON.stringify(formatting));
const pages = Object.keys(formatting.fonts.pages);
pages.forEach((page) => {
if (isAdditionalContent && newFormatting.fonts.pages[page][1]) {
// formatting for additional pages if set
newFormatting.fonts.pages[page] = newFormatting.fonts.pages[page][1];
} else {
// formatting for default pages (and additional pages if no extra formatting set)
newFormatting.fonts.pages[page] = newFormatting.fonts.pages[page][0];
}
});
return newFormatting;
}
function isExpectedTags(nodes, expectedTags) {
if (!nodes || !expectedTags) {
throw new Error("nodes or expected tags is wrong");
}
const tags = getTagsFromNodes(nodes);
return JSON.stringify(tags) === JSON.stringify(expectedTags);
}
function getTagsFromNodes(nodes) {
return nodes.map((t) => t.rawTagName);
}
function checkParagraphPlusListStructure(items) {
items.forEach((item, index) => {
if (items.length !== 2) {
throw new Error("2 items were expected");
}
if (index === 0 && item.type !== "paragraph") {
throw new Error("paragraph type was expected, got ", item.type);
} else if (index == 1 && item.type !== "list") {
throw new Error("list type was expected, got ", item.type);
}
if (item.type === "list") {
item.content.childNodes.forEach((li) => {
li.childNodes.forEach((c) => {
if (c.rawTagName !== "p") {
throw new Error("paragraph tag was expected, got " + c.rawTagName);
}
});
});
}
});
}
function isOnlyParagraphs(items) {
items.forEach((item) => {
if (item.type !== "paragraph") {
return false;
}
});
return true;
}
function handleParagraphs(nodes, headline, number, formatting) {
var content = nodes.map((n) => n.toString()).join("");
content = cleanParagraphTags(content);
// NOTE: Links count equals barcodes count
const barcodesCount = getLinks(content).length;
const { splitLength, clazz } = getClazzAndSplitLength(
content,
[],
barcodesCount,
number,
formatting
);
const splitCardContent = [];
const words = content.split(/\s+/);
var index = 0;
words.forEach((word) => {
splitCardContent[index] = splitCardContent[index] || "";
if (
textOnlyLength(splitCardContent[index]) + textOnlyLength(word) >
splitLength
) {
splitCardContent[index] += "…</p>";
splitCardContent[++index] = "<p>…";
}
splitCardContent[index] += splitCardContent[index].length ? " " : "";
splitCardContent[index] += word;
});
return getCardsForSplitCardContent(splitCardContent, clazz, number, headline);
}
function handleParagraphAndOneList(
nodes,
headline,
number,
listClazz,
formatting
) {
const paragraph = nodes[0];
const list = nodes[1];
if (
nodes.length !== 2 ||
paragraph.rawTagName !== "p" ||
list.rawTagName !== "ol"
) {
throw new Error("Chunk for paragraph with single list has wrong stucture!");
}
// get the full content
const fullContent = nodes.toString();
// NOTE: Links count equals barcodes count
const barcodesCount = getLinks(fullContent).length;
// get the clazz based on full content length
const { splitLength, clazz } = getClazzAndSplitLength(
fullContent,
[list],
barcodesCount,
number,
formatting
);
// get the content of the paragraph
const paragraphContent = paragraph.toString();
// get all list items
const listItems = list.childNodes;
// build 2 groups of list items
const listItemGroups = [[]];
listItems.forEach((item) => {
// Get current item content
const itemContent = item.toString();
item = escape(itemContent);
// Calculate the length of current content + content to add (current item)
const newLength =
textOnlyLength(paragraphContent) +
textOnlyLength(listItemGroups[0].join("")) +
textOnlyLength(itemContent);
// Only add to second list if split length is exceeded
if (newLength <= splitLength) {
listItemGroups[0].push(itemContent);
} else {
listItemGroups[1] = listItemGroups[1] || [];
listItemGroups[1].push(itemContent);
}
});
// build 2 card sides
const splitCardContent = [];
splitCardContent[0] = `${paragraphContent}`;
splitCardContent[0] += `<ol class="${listClazz || ""}">`;
splitCardContent[0] += listItemGroups[0].join("");
splitCardContent[0] += "</ol>";
if (listItemGroups[1]) {
splitCardContent[1] = `<ol class="${listClazz || ""}" start="${
listItemGroups[0].length + 1
}">`; // TODO: Sometimes is incorrect!
splitCardContent[1] += listItemGroups[1].join("");
splitCardContent[1] += "</ol>";
}
return getCardsForSplitCardContent(splitCardContent, clazz, number, headline);
}
////////////////////////// CLEAN / ENHANCE CARDS //////////////////////////
async function addBarcodes(card) {
card.barcodes = await getBarcodes(card.content);
}
function prepareContent(card) {
var content = card.content;
// clean card content
content = cleanUpcardContent(content);
// enhance card content
content = enhanceCardContent(content, card.xOfNX);
// split very long lines that would push beyond card with
content = content.replace("encodeWithSignature(", "encodeWithSignature( ");
content = content.replace("encodeWithSelector(", "encodeWithSelector( ");
card.content = content;
return card;
}
function cleanUpcardContent(content) {
// add whitespaces to make long expressions break (would overflow layout otherwise)
content = content.replace("encodeWithSignature(", "encodeWithSignature( ");
content = content.replace("encodeWithSelector(", "encodeWithSelector( ");
content = content.replace(
// TODO make general or move into document specific input
"CALLDATASIZE/CALLDATALOAD/CALLDATACOPY",
"CALLDATASIZE / CALLDATALOAD / CALLDATACOPY"
);
// remove any leading/trailing whitespace
return content.trim();
}
function enhanceCardContent(content, xOfN) {
// add highlight to beginning of content on first card page
if ([null, 1].includes(xOfN)) {
const maxColonPosition = 200;
const colonPosition = content.indexOf(":");
const isHeadline = colonPosition !== -1 && colonPosition < maxColonPosition; // NOTE: Heuristic to estimate if it is a headline
if (colonPosition && isHeadline) {
const emPosition = content.indexOf("<em>");
const emContained = emPosition !== -1 && emPosition < colonPosition;
if (emContained) {
// if em is contained
content = content.replace(
/(\S*<em>)((?:(?!<\/em>).)*)(.*)/gm,
"$1<strong>$2</strong>$3"
);
} else {
// if no em contained
content = content.replace(
/(\S+>)([^<:]+:)(.*)/gm,
"$1<strong>$2</strong>$3"
);
}
}
}
return content;
}
async function getBarcodes(content) {
const links = getLinks(content);
const barcodes = [];
/* NOTE: We ignore barcodes where there would be too many
like in card 30 of https://secureum.substack.com/p/audit-techniques-and-tools-101 */
// TODO: maybe solve differently
if (links.length > 8) {
return ["(QR codes omitted)"];
}
if (links) {
for (const link of links) {
barcodes.push(
await qrCode.toString(link, {
type: "svg",
width: 40,
margin: 0,
})
);
}
}
return barcodes;
}
function getLinks(content) {
return extractUrls(content) || [];
}
/////////////////////////////// SPLIT CARDS ///////////////////////////////
function getCardsForSplitCardContent(
splitCardContent,
clazz,
number,
headline
) {
return splitCardContent.map((spc, index) => {
return {
headline,
number,
xOfNX: splitCardContent.length > 1 ? index + 1 : null,
xOfNN: splitCardContent.length > 1 ? splitCardContent.length : null,
contentLength: textOnlyLength(spc),
clazz,
barcodes: [],
content: spc,
};
});
}
function getClazzAndSplitLength(
content,
lists,
barcodesCount,
number,
formatting
) {
const length = textOnlyLength(content); // NOTE: Only use content length without HTML tags
const listCount = lists.length;
const { standard, pages } = formatting.fonts;
// get font from page specific formatting (standard overwrites to fix edge cases)
var font = pages && pages[("" + number).replace("+", "")]; // TODO solve differently
// get font from standard formatting for content length
if (!font) {
font = standard.find((font) => length >= font[0]);
}
const clazzAndLength = {
clazz: font[3],
splitLength: listCount > 0 ? font[1] : font[2],
barcodesFactor: font[4],
};
// handle barcodes
if (barcodesCount) {
clazzAndLength.splitLength = Math.floor(
clazzAndLength.splitLength * (1 - clazzAndLength.barcodesFactor)
);
}
// handle lists with many short items (they stack high and take lots of vertical space)
if (listCount > 0) {
if (listCount > 1) {
throw new Error("handling more tahn one list is not yet implemented!");
}
// inspect the list for hints of occupying lots of vertical space
const childNodes = lists[0].childNodes;
if (childNodes.length > 5) {
const itemLengths = childNodes.map((item) => {
return textOnlyLength(item.toString());
});
const trimFromBithSides = childNodes.length <= 5 ? 0.25 : 0.1;
const mean = trimmedMean(itemLengths, trimFromBithSides);
// reduce split length for small means
const threshold = 30;
const reduction = 0.15;
if (mean < threshold) {
clazzAndLength.splitLength =
clazzAndLength.splitLength * (1 - reduction);
}
}
// TODO fix! (seems to belong to 1 particular card!)
// clazzAndLength.splitLength =
// number === 90 ? 250 : clazzAndLength.splitLength;
}
return clazzAndLength;
}
module.exports = { getSplitCards };