forked from Proskomma/proskomma-render-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_pdf.js
executable file
·96 lines (86 loc) · 2.81 KB
/
make_pdf.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
const fse = require('fs-extra');
const path = require('path');
const {Proskomma} = require('proskomma');
const {
ScriptureParaModel,
ScriptureParaModelQuery
} = require('proskomma-render');
const MainDocSet = require('./MainDocSet');
const bookMatches = str => {
for (const book of config.bookSources) {
if (str.includes(book) || str.includes(book.toLowerCase())) {
return true;
}
}
return false;
}
const peripheralMatches = str => {
for (const periph of config.peripheralSources) {
if (str.includes(periph) || str.includes(periph.toLowerCase())) {
return true;
}
}
return false;
}
const doMainRender = (config, result) => {
ts = Date.now();
const model = new ScriptureParaModel(result, config);
model.addDocSetModel('default', new MainDocSet(result, model.context, config));
model.render();
console.log(`Main rendered in ${(Date.now() - ts) / 1000} sec`);
console.log(model.logString());
}
const doRender = async (pk, config) => {
const thenFunction = result => {
console.log(`Query processed in ${(Date.now() - ts) / 1000} sec`);
doMainRender(config, result);
return config;
}
await ScriptureParaModelQuery(pk)
.then(thenFunction)
};
if (process.argv.length !== 4) {
throw new Error("USAGE: node make_pdf.js <configPath> <htmlOutputPath>");
}
const configPath = path.resolve(__dirname, process.argv[2]);
const config = fse.readJsonSync(configPath);
config.codeRoot = __dirname;
config.configRoot = path.dirname(configPath);
config.outputPath = process.argv[3];
if (!config.outputPath) {
throw new Error("USAGE: node make_pdf.js <configPath> <htmlOutputPath>");
}
config.bookOutput = {};
let ts = Date.now();
let nBooks = 0;
let nPeriphs = 0;
const pk = new Proskomma();
const fqSourceDir = path.resolve(config.configRoot, config.sourceDir);
for (const filePath of fse.readdirSync(fqSourceDir)) {
if (bookMatches(filePath)) {
console.log(` ${filePath} (book)`);
nBooks++;
const content = fse.readFileSync(path.join(fqSourceDir, filePath));
const contentType = filePath.split('.').pop();
pk.importDocument(
{lang: "xxx", abbr: "yyy"},
contentType,
content,
{}
);
} else if (peripheralMatches(filePath)) {
console.log(` ${filePath} (peripheral)`);
nPeriphs++;
let content = fse.readFileSync(path.join(fqSourceDir, filePath));
pk.importUsfmPeriph(
{ lang: 'xxx', abbr: 'yyy' },
content,
{},
);
}
}
console.log(`${nBooks} book(s) and ${nPeriphs} peripheral(s) loaded in ${(Date.now() - ts) / 1000} sec`);
ts = Date.now();
doRender(pk, config).then((res) => {
// console.log(JSON.stringify(config, null, 2));
});