forked from Proskomma/proskomma-render-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PeripheralDocument.js
164 lines (159 loc) · 6.37 KB
/
PeripheralDocument.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
const { ScriptureParaDocument } = require('proskomma-render');
const sharedActions = require('./shared_actions');
class PeripheralDocument extends ScriptureParaDocument {
constructor(result, context, config) {
super(result, context, config);
this.head = [];
this.bodyHead = [];
this.body = [];
addActions(this);
}
}
const addActions = (dInstance) => {
// Initialize headers (not including title) and other state
dInstance.addAction(
'startDocument',
() => true,
(renderer, context) => {
let cssPath = "../../CSS/styles.css";
dInstance.head = [
'<meta charset=\"utf-8\"/>\n',
`<link type="text/css" rel="stylesheet" href="${cssPath}" />\n`,
`<title>${context.document.headers.h}</title>`,
];
dInstance.body = [];
dInstance.bodyHead = [];
const periphTitle = context.document.idParts[3];
dInstance.docSetModel.bookTitles[context.document.headers.bookCode] = [
periphTitle,
periphTitle,
periphTitle,
periphTitle,
];
dInstance.context.document.chapters = [];
}
);
// Follow some block grafts to secondary content
dInstance.addAction(
'blockGraft',
context => ["title", "heading", "introduction"].includes(context.sequenceStack[0].blockGraft.subType),
(renderer, context, data) => {
renderer.renderSequenceId(data.payload);
}
);
// Start new stack row for new block
dInstance.addAction(...sharedActions.startBlock);
// Render title block
dInstance.addAction(
'endBlock',
context => context.sequenceStack[0].type === "title",
(renderer, context, data) => {
const htmlClass = data.bs.payload.split('/')[1];
const tag = ["mt", "ms"].includes(htmlClass) ? "h1" : "h2";
renderer.bodyHead.push(`<${tag} class="${htmlClass}">${renderer.topStackRow().join("").trim()}</${tag}>\n`);
renderer.popStackRow();
},
);
// Render heading block
dInstance.addAction(
'endBlock',
context => context.sequenceStack[0].type === "heading",
(renderer, context, data) => {
const htmlClass = data.bs.payload.split("/")[1];
let headingTag;
switch (htmlClass) {
case "s":
case "is":
headingTag = "h3";
break;
default:
headingTag = "h4";
}
renderer.body.push(`<${headingTag} class="${htmlClass}">${renderer.topStackRow().join("").trim()}</${headingTag}>\n`);
renderer.popStackRow();
},
);
// process footnote content
dInstance.addAction(
'endBlock',
context => context.sequenceStack[0].type === "footnote",
renderer => {
const footnoteContent = renderer.topStackRow().join("").trim("");
renderer.popStackRow();
renderer.context.sequenceStack[1].renderStack[0].push(`<span class="footnote">${footnoteContent}</span>`);
},
);
// Render main or introduction block in a div with class derived from the block scope
dInstance.addAction(
'endBlock',
context => ["main", "introduction"].includes(context.sequenceStack[0].type),
(renderer, context, data) => {
const htmlClass = data.bs.payload.split("/")[1];
renderer.body.push(`<div class="${htmlClass}">${renderer.topStackRow().join("").trim()}</div>\n`);
renderer.popStackRow();
},
);
// Character markup - open or close an element
dInstance.addAction(...sharedActions.characterScope);
// Unhandled scope
dInstance.addAction(...sharedActions.w);
dInstance.addAction(...sharedActions.unhandledScope);
// Tokens, including attempt to add French spaces and half-spaces after punctuation
dInstance.addAction(
'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);
}
);
// process footnote
dInstance.addAction(
'inlineGraft',
(context, data) => data.subType === "footnote",
(renderer, context, data) => {
renderer.renderSequenceId(data.payload);
}
);
// Generate document HTML
dInstance.addAction(
'endSequence',
context => context.sequenceStack[0].type === "main",
(renderer, context) => {
let bodyHead = renderer.bodyHead.join("");
renderer.config.bookOutput[context.document.headers.bookCode] =
[
'<div class="periph">\n',
`<a id="title_${context.document.headers.bookCode}"/>\n`,
`<p class="runningHeader">${dInstance.docSetModel.bookTitles[context.document.headers.bookCode][0]}</p>\n`,
`<header>\n${bodyHead}\n</header>\n`,
'<div class="periphBody">\n',
renderer.body.join(""),
'</div>\n',
'</div>\n',
].join("");
}
);
};
module.exports = PeripheralDocument;