-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
238 lines (212 loc) · 7.34 KB
/
Copy pathindex.js
File metadata and controls
238 lines (212 loc) · 7.34 KB
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
import args from "args";
import fs from "fs";
import fsp from "fs/promises";
import YAML from "yaml";
import fetch from "node-fetch"; // асинхронный fetch
import { fileURLToPath } from "url";
import { dirname, resolve } from "path";
import slugify from "@sindresorhus/slugify";
import { MarkdownRenderer } from "./md.js";
import * as docx from "./docx.js";
import * as base_pdf from "./render.js";
import path from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import zlib from "zlib";
import Prince from "prince";
import * as util from "node:util";
import crypto from "node:crypto";
// ------------------------
// Parse CLI arguments
// ------------------------
args
.option("path", "Search path", ".")
.option("input", "Input file", "(?<name>.*)\\.md")
.option("output", "Output file", "{{name}}")
.option("config", "Config document");
const cliData = args.parse(process.argv);
// ------------------------
// Load config
// ------------------------
let config = {};
if (cliData.config) {
const configContent = await fsp.readFile(cliData.config, "utf-8");
config = YAML.parse(configContent);
}
log("CONFIG", config);
config.style = "";
if (config.styles) {
config.style += `<style>\n`;
for (const url of config.styles) {
let data;
if (fs.existsSync(url)) {
data = (await fsp.readFile(url, "utf-8")).substring(1);
} else {
const res = await fetch(url);
data = await res.text();
}
config.style += `${data}\n`;
// config.style += `<link rel="stylesheet" href="${url}">\n`
}
config.style += `</style>`;
}
// ------------------------
// Init render engines
// ------------------------
const printConfig = config.print ?? { html: false, docx: false, base_pdf: true, prince_pdf: false };
const md = new MarkdownRenderer();
const sharedContext = {
zlib,
yaml: YAML,
fetch,
md,
log: (s) => log("REPLACEMENT", s),
error: (s) => error("REPLACEMENT", s),
slugify,
store: {}
};
// process.stdin.resume(); // so the program will not close instantly
// async function exitHandler(options, exitCode) {
// if (printConfig.base_pdf) await base_pdf.complete();
// if (exitCode || exitCode === 0) console.log(exitCode);
// process.exit();
// }
// // do something when app is closing
// process.on('exit', exitHandler.bind(null,{}));
// // catches ctrl+c event
// process.on('SIGINT', exitHandler.bind(null, {}));
// // catches "kill pid" (for example: nodemon restart)
// process.on('SIGUSR1', exitHandler.bind(null, {}));
// process.on('SIGUSR2', exitHandler.bind(null, {}));
// // catches uncaught exceptions
// process.on('uncaughtException', exitHandler.bind(null, {}));
await md.init(config, sharedContext, error);
if (printConfig.docx) await docx.init(config);
if (printConfig.base_pdf) await base_pdf.init(config);
await processDirectory(path.resolve(__dirname, cliData.path || '.'));
// ------------------------
// Render documents
// ------------------------
async function processDirectory(dirPath) {
const files = await fsp.readdir(dirPath, {recursive: config.recursive || false});
let tasks = 0;
let limiterStrike = false;
for (const fileIn of files) {
const match = fileIn.match(cliData.input);
if (!match) continue;
let fileOut = path.resolve(dirPath, cliData.output);
if (match.groups) {
for (const key in match.groups) {
fileOut = fileOut.replace(`{{${key}}}`, match.groups[key]);
}
}
const filename = resolve(dirPath, fileIn);
while (config.task_limiter && tasks >= config.task_limiter) {
await sleep(1000)
if (limiterStrike) continue;
limiterStrike = true;
log("LIMITER", "Task rate limited! Waiting for free resources!")
}
limiterStrike = false;
tasks++
markdownToFiles(filename, fileOut, dirPath, config.style).then(r => {
tasks--
log("PROCESSOR", `File ${filename} converted!`)
});
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function hash(str) {
return crypto.createHash("sha256").update(str).digest("hex");
}
// ------------------------
// Markdown -> Files
// ------------------------
async function markdownToFiles(fileIn, fileOut, rootDir, style) {
try {
const rawData = await fsp.readFile(fileIn, "utf-8");
let html = await md.render(rawData.toString());
html = applyReplacements(html, config.replacements, sharedContext);
html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>${fileIn}</title>
${style}
<script>window.wait = 0</script>
${md.inject()}
</head>
<body>
${html}
</body>
</html>`;
if (printConfig.html) {
log("HTML", `Render ${fileIn} to ${fileOut}.html`);
await fsp.writeFile(`${fileOut}.html`, html);
}
if (printConfig.docx) {
log("DOCX", `Render ${fileIn} to ${fileOut}.docx`);
const blob = await docx.render(html).arrayBuffer();
const injected = await docx.inject(blob);
await fsp.writeFile(`${fileOut}.docx`, Buffer.from(injected));
}
if (printConfig.base_pdf) {
let name = `${fileOut}${printConfig.prince_pdf ? "_base" : ""}.pdf`
log("BASE_PDF", `Render ${fileIn} to ${name}`);
const blob = await base_pdf.renderPdf(html);
const injected = await base_pdf.inject(blob, fileIn, rootDir);
await fsp.writeFile(name, injected);
}
if (printConfig.prince_pdf) {
let name = `${fileOut}${printConfig.base_pdf ? "_prince" : ""}.pdf`
log("PRINCE_PDF", `Render ${fileIn} to ${name}`)
let prince_config = config.prince_pdf ?? {};
const renderedHtml = await base_pdf.renderHtml(html);
const temp = `${fileOut}_prince.html`;
await fsp.writeFile(temp, renderedHtml);
await new Prince()
.cwd(rootDir)
.timeout(prince_config.timeout)
.inputs(temp)
.output(name)
.option("javascript", true)
.execute()
await fsp.unlink(temp)
const blob = await fsp.readFile(name);
const injected = await base_pdf.inject(blob, fileIn, rootDir);
await fsp.writeFile(name, injected);
}
} catch (err) {
error("LOAD", util.inspect(err));
}
}
// ------------------------
// Apply replacements
// ------------------------
function applyReplacements(html, replacements = [], context = {}) {
for (const replacement of replacements) {
if (!replacement.regex) continue;
const regex = new RegExp(replacement.regex, "g");
if (replacement.string) {
html = html.replaceAll(regex, replacement.string);
}
else if (replacement.code) {
const fn = eval(`(${replacement.code})`);
html = html.replaceAll(regex, (...args) =>
fn(context, ...args)
);
}
}
return html;
}
// ------------------------
// Logging helpers
// ------------------------
function log(type, message) {
console.log(`[${type}]`, message);
}
function error(type, message) {
console.error(`[${type}]`, message);
}