forked from saint11/DeadWeight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
427 lines (356 loc) · 14 KB
/
index.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
var uslug = require("uslug");
const moment = require('moment');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const path = require('path');
const http = require('http');
const express = require('express');
const app = express();
const fs = require('fs-extra')
const chokidar = require('chokidar');
const dataJsonPath = './data/data.json';
const { getFromId, getSheet } = require('./util/data.js');
const data = JSON.parse(fs.readFileSync(dataJsonPath, 'utf8'));
const data_monsters = getSheet(data, "monsters");
const data_items = getSheet(data, "items");
const data_weapons = getSheet(data, "weapons");
const data_armor = getSheet(data, "armor");
const data_shield = getSheet(data, "shield");
const data_actions = getSheet(data, "actions");
const { makeMonsterTable, makeTable } = require('./util/tables.js');
const { title } = require('process');
const { SlowBuffer } = require('buffer');
var shortcodes = {
monster: {
render: function (attrs, env) {
const monster = getFromId(data_monsters, attrs.id);
console.assert(monster, "Cannot find monster " + attrs.id);
return makeMonsterTable(monster, data_actions);
}
},
itemTable: {
render: function (attrs, env) {
const options = {
exclude: ["rarity"],
bold: [0],
filter: el => el["rarity"] == attrs.id,
caption: attrs.caption,
tags: { tags: ["Quick", "Slow", "Camp", "Equipment"] }
};
return makeTable(data_items, options);
}
},
weaponTable: {
render: function (attrs, env) {
const options = {
bold: [0],
caption: attrs.caption,
punctuation: { stats: true },
enums: { rarity: ["free", "common", "uncommon", "valuable", "rare", "exotic"] },
styles: { stats: "width: 100px;" }
};
console.assert(data_weapons, "Wepons data not found");
return makeTable(data_weapons, options);
}
},
armorTable: {
render: function (attrs, env) {
const options = {
bold: [0],
caption: attrs.caption,
punctuation: { stats: true },
enums: { rarity: ["free", "common", "uncommon", "valuable", "rare", "exotic"], type: ["Cloth", "Leather", "Heavy"] },
styles: { stats: "width: 80px;" }
};
console.assert(data_armor, "Armor data not found");
return makeTable(data_armor, options);
}
},
shieldTable: {
render: function (attrs, env) {
const options = {
bold: [0],
caption: attrs.caption,
punctuation: { stats: true },
enums: { rarity: ["free", "common", "uncommon", "valuable", "rare", "exotic"], type: ["Cloth", "Leather", "Heavy"] },
styles: { stats: "width: 130px;" }
};
console.assert(data_shield, "shield data not found");
return makeTable(data_shield, options);
}
},
break: {
render: function (attrs, env) {
return `<div style="clear: both;"></div>`;
}
},
now: {
render: function (attrs, enc) {
return moment().format('h:mm a, Do MMMM YYYY');
}
},
pagebreak: {
render: function (attrs, env) {
pageCount++;
return '';
}
},
d6: {
render: function (attrs, env) {
return `<img src="images/dice-${attrs.v}.svg" class="dice"/>`
}
}
}
const headingOptions = {
linkIcon: '<i class="fas fa-link"></i>',
prefix: ''
};
var mdWeb = require('markdown-it')({ html: true, breaks: false });
const toc_options = {
level: 1,
listType: 'ul',
slugify: s => {
return uslug(s);
}
};
mdWeb.use(require("markdown-it-attrs"));
mdWeb.use(require("markdown-it-task-lists"));
mdWeb.use(require('markdown-it-container-pandoc'))
mdWeb.use(require("markdown-it-github-headings"), headingOptions);
mdWeb.use(require("./util/markdown-it-external-toc.js"), toc_options);
mdWeb.use(require('markdown-it-header-sections'))
mdWeb.use(require('markdown-it-bracketed-spans'))
mdWeb.use(require('markdown-it-meta'))
mdWeb.use(require('markdown-it-multimd-table'))
mdWeb.use(require('markdown-it-shortcode-tag'), shortcodes);
var mdPrint = require('markdown-it')({ html: true, breaks: true });
mdPrint.use(require("markdown-it-attrs"));
mdPrint.use(require('markdown-it-container-pandoc'))
mdPrint.use(require("markdown-it-anchor"));
mdPrint.use(require("markdown-it-toc-done-right"));
mdPrint.use(require('markdown-it-header-sections'))
mdPrint.use(require('markdown-it-bracketed-spans'))
mdPrint.use(require('markdown-it-meta'))
mdPrint.use(require('markdown-it-multimd-table'))
mdPrint.use(require('./util/markdown-it-print.js'))
mdPrint.use(require('markdown-it-shortcode-tag'), shortcodes);
var pageCount = -1;
const publicPath = 'public/';
const outputPath = 'output/';
const sourcePath = 'source/';
const dataPath = 'data/';
const templatesPath = 'templates/';
const staticPath = 'static/';
// Start the server for testing
const PORT = process.env.PORT || 3000;
const server = http.createServer(app)
app.use(express.static(path.join(__dirname, publicPath)));
server.listen(PORT, () => console.log(`======== Server started [${moment().format('h:mm a')}] ========`));
processSourcesFolder();
var lastProcessedTime = Date.now();
chokidar.watch(sourcePath).on('all', (event, path) => {
processSourcesFolder();
});
chokidar.watch(dataPath).on('all', (event, path) => {
processSourcesFolder();
});
function throughDirectory(directory) {
files = [];
function cycleDirectory(directory_path) {
fs.readdirSync(directory_path).forEach(file => {
const absolute = path.join(directory_path, file);
if (fs.statSync(absolute).isDirectory()) return cycleDirectory(absolute);
else return files.push(absolute);
});
}
cycleDirectory(directory);
return files;
}
function processSourcesFolder() {
if (Date.now() - lastProcessedTime < 8000) return;
lastProcessedTime = Date.now();
fs.emptyDirSync(outputPath);
console.log("======== updating sources folder ========")
fs.readdir(publicPath, (err, files) => {
files.forEach(file => {
if (path.extname(file) == ".html") {
fs.unlinkSync(path.join(publicPath, file));
console.log("removed " + file);
}
});
var files = throughDirectory(sourcePath);
const renderedFiles = [];
var bookMd = [];
files.forEach(file => {
if (path.extname(file) == ".md") {
const fileName = file.substr(0, file.lastIndexOf("."));
processMarkDown(fileName, bookMd);
renderedFiles.push(fileName);
}
});
// Make print version
var bookRaw = "";
bookMd.forEach(el => bookRaw += el + "\n\n");
var print = getHtmlFromMarkdown(bookRaw, mdPrint, 'print');
document = print.dom.window.document;
// Make print A4 file
print.linkElement.setAttribute('href', 'print-a4.css');
saveFile(print.dom.serialize(), path.join(publicPath, "book-a4"));
saveFile(print.dom.serialize(), path.join(outputPath, "book-a4"));
// Make print letter file
print.linkElement.setAttribute('href', 'print-letter.css');
saveFile(print.dom.serialize(), path.join(publicPath, "book-letter"));
saveFile(print.dom.serialize(), path.join(outputPath, "book-letter"));
console.log("Rendered book (A4 and Letter)");
// Copy static folder
fs.copySync(staticPath, publicPath);
fs.copySync(staticPath, outputPath);
fs.copySync(dataJsonPath, publicPath + "data.json");
fs.copySync(dataJsonPath, outputPath + "data.json");
// Copy all images to the output folder
requireDir(path.join(publicPath, "images/"));
fs.copySync(path.join(publicPath, "images/"), path.join(outputPath, "images/"));
// Copy all extra files to the output folder
fs.readdir(publicPath, (err, files) => {
files.forEach(file => {
if (path.extname(file) != ".html") {
fs.copySync(path.join(publicPath, file), path.join(outputPath, file))
// console.log(`${path.join(publicPath, file)} to ${path.join(outputPath, file)}`)
}
});
});
console.log("======== job done ========")
});
}
function processMarkDown(fileName, mdPrint) {
const file = fileName.substr(sourcePath.length);
const depth = fileName.split(/\/.+?/g).length;
var raw = readFile(path.join(sourcePath, file + ".md"));
console.assert(raw, "Couldn't find markdown source");
var { dom, linkElement } = getHtmlFromMarkdown(raw, mdWeb);
var document = dom.window.document;
// Make draft file
linkElement.setAttribute('href', '../'.repeat(depth) + (mdWeb.meta.css ? mdWeb.meta.css : 'draft.css'));
saveFile(dom.serialize(), path.join(publicPath, file));
// Let's make the output now!
// Make web file
linkElement.setAttribute('href', '../'.repeat(depth - 1) + (mdWeb.meta.css ? mdWeb.meta.css : 'publish.css'));
saveFile(dom.serialize(), path.join(outputPath, file));
if (mdWeb.meta.print) {
var clear = raw;
if (mdWeb.meta.print > 0) {
console.log("*** header found, cleaning ***");
clear = raw.substr(nthIndex(raw, "---", 2) + 3);
}
mdPrint[mdWeb.meta.print] = clear;
console.log("Rendered " + file);
}
else
console.log("Rendered " + file + " (web only)");
}
function saveFile(html, filePath) {
var fileName = `${filePath}.html`;
// console.log("Writing " + fileName);
requireDir(fileName);
fs.writeFileSync(fileName, html, err => {
console.log(err);
})
}
function requireDir(filePath) {
if (!fs.existsSync(path.dirname(filePath))) {
fs.mkdirSync(path.dirname(filePath));
}
}
function readFile(file_path) {
try {
var data = fs.readFileSync(file_path, 'utf8');
return data;
} catch (e) {
console.log('Error:', e.stack);
}
}
function getHtmlFromMarkdown(str, strategy, templateSuffix) {
var result = strategy.render(str);
const templatePath = `${templatesPath}template${strategy.meta.template ? '-' + strategy.meta.template : ''}${templateSuffix ? '-' + templateSuffix : ''}.html`;
var template = readFile(templatePath);
console.assert(template, "Couldn't find template source");
Object.entries(strategy.meta).forEach(attr => {
template = template.replace("$" + attr[0], attr[1]);
});
const dom = new JSDOM(template);
const document = dom.window.document;
if (strategy.meta.title) {
document.title = strategy.meta.title;
if (!strategy.meta["hide-title"]) {
const title = document.createElement('h1');
title.innerHTML = strategy.meta.title;
document.getElementById("main-document").append(title);
}
}
const toc = document.getElementById("toc");
if (toc) {
if (strategy.meta["toc"]) {
if (strategy.meta["toc-title"]) {
const title = document.createElement('h2');
title.innerHTML = strategy.meta["toc-title"];
title.id = "toc-title"
toc.appendChild(title)
}
toc.innerHTML += strategy.tocBody;
if (strategy.meta["links"]) {
const link = document.createElement('div');
link.innerHTML += `<h2 style="margin-bottom:2px; margin-top:30px">${strategy.meta["links-title"]}</h2><ul>`;
strategy.meta["links"].split(',').forEach((el) => {
const split = el.split("\|");
if (split[0].endsWith("*")) {
link.innerHTML += `<li class="selected">${split[0].trim().slice(0, -1)}</li>`;
}
else {
link.innerHTML += `<li><a href="./${split[1] ?? split[0].trim().toLowerCase()}.html">${split[0].trim()}</a></li>`;
}
toc.appendChild(link)
})
link.innerHTML += `</ul>`;
}
}
else {
toc.parentElement.remove();
}
}
document.getElementById("main-document").innerHTML += result;
// Monster tooltips
// var monster_tooltips = document.querySelectorAll(".m");
// monster_tooltips.forEach(t => {
// t.setAttribute("onmouseover", 'position_tooltip(this)');
// t.setAttribute("onmouseout", 'hide_tooltip(this)');
// });
// monster_tooltips.forEach(monsterSpan => {
// var id = monsterSpan.getAttribute("id");
// if (!id)
// id = monsterSpan.innerHTML.toLowerCase().replaceAll(" ", "_");
// const monster = getFromId(data_monsters, id);
// console.assert(monster, "Can't find monster with id " + id);
// var tableDiv = document.createElement('div');
// tableDiv.innerHTML= makeMonsterTable(monster, data_actions);
// tableDiv.classList.add ("tooltiptext");
// monsterSpan.classList.add("tooltip");
// monsterSpan.appendChild(tableDiv);
// });
var linkElement = document.createElement('link');
linkElement.setAttribute('rel', 'stylesheet');
linkElement.setAttribute('type', 'text/css');
document.head.append(linkElement);
var faAwesome = document.createElement('script');
faAwesome.setAttribute('src', 'https://kit.fontawesome.com/ba93eb1f54.js');
faAwesome.setAttribute('crossorigin', 'anonymous');
document.head.append(faAwesome);
return { dom, linkElement };
}
function nthIndex(str, pat, n) {
var L = str.length, i = -1;
while (n-- && i++ < L) {
i = str.indexOf(pat, i);
if (i < 0) break;
}
return i;
}