-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgen_pdf_wk.js
354 lines (320 loc) · 9.46 KB
/
gen_pdf_wk.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
#!/usr/bin/env node
const childProcess = require("child_process");
const cmdOptions = require("commander");
const dom = require("xmldom").DOMParser;
const fs = require("fs-extra");
const fsp = require("fs-promise");
const fileUrl = require("file-url");
const htmltidy = require("htmltidy2");
const less = require("less");
const path = require("path");
const selectn = require("selectn");
const streamToPromise = require("stream-to-promise");
const xpath = require("xpath");
const configLoader = require("./gen_pdf_wk_loadConfig.js");
// Module level variables.
let config;
// Convert string representing integers to a number. Throws if failed.
function filterInt(value) {
if (/^(-|\+)?([0-9]+|Infinity)$/.test(value)) {
return Number(value);
}
throw new Error(`Unable to parse "${value}" as an integer.`);
}
// Wrapper around 'file-url' module. It converts Windows drive letters to lower
// case so QT can understand them.
function getQTCompatibleFileUrl(path) {
return fileUrl(path).replace(new RegExp("^file:///[A-Z]:/"), match =>
match.toLowerCase()
);
}
// Convenience function to get the source path of the asset.
function getAssetSrcPath(assetName) {
return path.join(config.root, selectn(assetName, config));
}
class GitbookToWkhtmltopdf {
constructor(assetMap) {
this._docList = [];
this._assetMap = assetMap;
}
_getAssetDescPath(assetName) {
return this._assetMap.get(getAssetSrcPath(assetName));
}
_getStdinArgs(args) {
let ret = "";
for (const arg of args) {
ret += arg.toString().replace(/[ \t\n\r\\]/g, "\\$&");
ret += " ";
}
return ret;
}
_processNodes(select, nodes, level) {
for (const node of nodes) {
let url;
try {
url = path.join(
cmdOptions.ebookDirectory,
decodeURIComponent(select("./html:span/html:a/@href", node)[0].value)
);
} catch (e) {
// Multipart titles - skip this item until we have a better handling method.
continue;
}
const title = select(
"normalize-space(./html:span/html:a/text())",
node
).toString();
this._docList.push({ url: url, title: title, level: level });
const subNodes = select("./html:ol/html:li", node);
this._processNodes(select, subNodes, level + 1);
}
}
_doCommand() {
const args = [];
args.push(
"-T",
config.margin.top,
"-B",
config.margin.bottom,
"-L",
config.margin.left,
"-R",
config.margin.right
);
if (config.title) {
args.push("--title", config.title);
}
const argsForAllPages = [
"--zoom",
cmdOptions.zoom,
"--debug-javascript",
"--javascript-delay",
cmdOptions.javascriptDelay
];
if (config.cover) {
args.push(
"cover",
getQTCompatibleFileUrl(this._getAssetDescPath("cover")),
"--exclude-from-outline",
...argsForAllPages
);
}
// Can't use file:// URL in XSL stylesheet. The reason is unknown.
args.push("toc", ...argsForAllPages);
if (config.tocXsl) {
args.push(
"--xsl-style-sheet",
path.relative(
cmdOptions.ebookDirectory,
this._getAssetDescPath("tocXsl")
)
);
}
const argsForNormalPages = [...argsForAllPages];
for (const section of ["header", "footer"]) {
const sectionConfig = config[section];
if (!sectionConfig.contentHtml) {
continue;
}
argsForNormalPages.push(
`--${section}-html`,
getQTCompatibleFileUrl(
this._getAssetDescPath(`${section}.contentHtml`)
),
`--${section}-spacing`,
sectionConfig.spacing
);
}
Object.freeze(argsForNormalPages);
for (const doc of this._docList) {
args.push(getQTCompatibleFileUrl(doc.url), ...argsForNormalPages);
}
args.push(path.relative(cmdOptions.ebookDirectory, cmdOptions.outputFile));
const childProcessOptions = {
cwd: cmdOptions.ebookDirectory,
input: this._getStdinArgs(args),
stdio: ["pipe", process.stdout, process.stderr],
timeout: 200000 // wkhtmltopdf can infinitely loop in some cases.
};
console.log("Launching wkhtmltopdf:");
if (cmdOptions.debug) {
console.log("[debug] Options for wkhtmltopdf: %O", childProcessOptions);
}
childProcess.spawnSync(
"wkhtmltopdf",
["--read-args-from-stdin"],
childProcessOptions
);
}
process(xml) {
const doc = new dom().parseFromString(xml);
const select = xpath.useNamespaces({
html: "http://www.w3.org/1999/xhtml"
});
const nodes = select(
'//html:div[contains(@class,"toc")]/html:ol/html:li',
doc
);
this._processNodes(select, nodes, 1);
this._doCommand();
}
}
// Build with GitBook if needed.
function maybeBuildBook() {
if (
!cmdOptions.build &&
fs.existsSync(cmdOptions.ebookDirectory) &&
fs.statSync(cmdOptions.ebookDirectory).isDirectory()
) {
return;
}
console.log("Running GitBook:");
const args = ["build", ".", cmdOptions.ebookDirectory, "--format", "ebook"];
childProcess.spawnSync("gitbook", args, { shell: true, stdio: "inherit" });
}
// Process the assets. Returns the mapping from source to destination.
function processAssets() {
const assets = new Set(
[
...config.assets,
config.cover,
selectn("header.contentHtml", config),
selectn("footer.contentHtml", config)
]
.filter(v => v)
.map(v => path.join(config.root, v))
);
const assetMap = new Map();
const assetReverseMap = new Map();
for (const assetSrc of assets) {
const assetDestBase = path.join(
cmdOptions.ebookDirectory,
path.basename(assetSrc)
);
let assetDest;
if (assetReverseMap.has(assetDestBase)) {
for (let i = 1; ; ++i) {
assetDest = assetDestBase + "_" + i;
if (!assetReverseMap.has(assetDest)) {
break;
}
}
} else {
assetDest = assetDestBase;
}
assetMap.set(assetSrc, assetDest);
assetReverseMap.set(assetDest, assetSrc);
}
return new Promise(resolve => {
Promise.all(
[...assetMap].map(([src, dest]) => {
switch (path.extname(src).toLowerCase()) {
case ".less": {
console.log("Processing LESS asset: %s => %s", src, dest);
const cssDest = dest.replace(/\.less$/, ".css");
return fsp
.readFile(src, "utf8")
.then(data => less.render(data, { filename: src }))
.then(output => fsp.writeFile(cssDest, output.css, "utf8"));
}
case ".xsl": {
console.log("Processing XSL asset: %s => %s", src, dest);
return fsp.readFile(src, "utf8").then(data => {
// Replace parts of URL for QT XSL.
const url = getQTCompatibleFileUrl(
cmdOptions.ebookDirectory
).replace("%20", " ");
const processedData = data.replace("%url_current_dir%", url);
return fsp.writeFile(dest, processedData, "utf8");
});
}
default: {
console.log("Copying asset: %s => %s", src, dest);
return fsp.copy(src, dest, { clobber: true });
}
}
})
).then(() => resolve(assetMap));
});
}
// Process the summary. Return the processed summary as xhtml.
function processSummary() {
return streamToPromise(
fs
.createReadStream(path.join(cmdOptions.ebookDirectory, "SUMMARY.html"))
.pipe(
htmltidy.createWorker({
"input-encoding": "utf8",
"output-encoding": "utf8",
"output-xhtml": true
})
)
).then(buffer =>
fsp
.writeFile(
path.join(cmdOptions.ebookDirectory, "SUMMARY.xhtml"),
buffer,
"utf8"
)
.then(() => buffer.toString())
);
}
// Constants for command line options.
const ebookDir = "_ebook";
const outputFile = "book_wk.pdf";
const qtSmartShrinkDefault = 1.2;
const jsDelayDefault = 1000;
cmdOptions
.version("0.0.1")
.option(
"-D, --ebook-directory <path>",
`Set the temp directory of the generated ebook. [default: ${
ebookDir
}] Caution: errors will happen if a delimeter (space, comma, semicolon, equal) is present in this option.`,
ebookDir
)
.option(
"-o, --output-file <file>",
`Set the output file. [default: ${outputFile}]`,
outputFile
)
.option(
"-z, --zoom <float>",
`Set the zoom level. The default is meant to offset QT smart shrink. [default: ${
qtSmartShrinkDefault
}]`,
parseFloat,
qtSmartShrinkDefault
)
.option(
"-j, --javascript-delay <msec>",
`Allow the specified number of milliseconds for the javascript to finish. [default: ${
jsDelayDefault
}]`,
filterInt,
jsDelayDefault
)
.option("-n, --no-build", "Do not rebuild tempororary files.")
.option("--debug", "Print debugging information.")
.parse(process.argv);
// Load config file.
configLoader
.loadConfig()
.then(rawConfig => {
config = rawConfig.wkhtmltopdf;
// Some config in other sections are copied to the wkhtmltopdf config.
config.root = rawConfig.root;
if (!config.title) {
config.title = rawConfig.title;
}
config.summary = rawConfig.structure.summary;
Object.freeze(config);
maybeBuildBook();
return Promise.all([processSummary(), processAssets()]);
})
.then(([xml, assetMap]) => {
new GitbookToWkhtmltopdf(assetMap).process(xml);
})
.catch(err => {
console.error(err.stack);
});