forked from Proskomma/proskomma-render-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_actions.js
71 lines (70 loc) · 2.66 KB
/
shared_actions.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
const sharedActions = {
// Character markup - open or close an element
characterScope: [
'scope',
(context, data) => data.payload.startsWith("span") && ["bd", "bk", "dc", "em", "ft", "fq", "fqa", "fr", "fv", "it", "k", "ord", "pn", "qs", "sls", "tl", "wj", "xt"].includes(data.payload.split("/")[1]),
(renderer, context, data) => {
if (data.subType === "start") {
renderer.pushStackRow();
} else {
const spanContent = renderer.topStackRow().join("");
renderer.popStackRow();
renderer.topStackRow().push(`<span class="${data.payload.split("/")[1]}">${spanContent}</span>`);
}
}
],
// ignore w
w: [
'scope',
(context, data) => data.payload.split("/")[1] === 'w',
(renderer, context, data) => {}
],
startBlock: [
'startBlock',
() => true,
renderer => renderer.pushStackRow(),
],
unhandledScope: [
'scope',
(context, data) => data.payload.startsWith("span"),
(renderer, context, data) => {
if (data.subType === "start") {
renderer.docSetModel.writeLogEntry(
'Warning',
`Unhandled span '${data.payload}'`,
renderer.key,
)
}
}
],
token: [
'token',
() => true,
(renderer, context, data) => {
let tokenString;
if (["lineSpace", "eol"].includes(data.subType)) {
tokenString = " ";
} else {
if ([";", "!", "?"].includes(data.payload)) {
if (renderer.topStackRow().length > 0) {
let lastPushed = renderer.topStackRow().pop();
lastPushed = lastPushed.replace(/ $/, " ");
renderer.appendToTopStackRow(lastPushed);
}
tokenString = data.payload;
} else if ([":", "»"].includes(data.payload)) {
if (renderer.topStackRow().length > 0) {
let lastPushed = renderer.topStackRow().pop();
lastPushed = lastPushed.replace(/ $/, " ");
renderer.appendToTopStackRow(lastPushed);
}
tokenString = data.payload;
} else {
tokenString = data.payload.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
}
return renderer.appendToTopStackRow(tokenString);
}
],
};
module.exports = sharedActions;