1 | #!/usr/bin/env node |
---|
2 | // Usage: |
---|
3 | // npm install jsdom |
---|
4 | // node convertTree.js build/html |
---|
5 | /* eslint no-console:0 */ |
---|
6 | /* global katex */ |
---|
7 | "use strict"; |
---|
8 | |
---|
9 | /** |
---|
10 | * Set a global variables name to value. |
---|
11 | */ |
---|
12 | function setGlobal(name, value) { |
---|
13 | (function(globals) { globals[name] = value; }((1, eval)('this'))); |
---|
14 | } |
---|
15 | |
---|
16 | const fs = require("fs"); // standard node |
---|
17 | const jsdom = require("jsdom"); // npm install jsdom |
---|
18 | const { JSDOM } = jsdom; |
---|
19 | |
---|
20 | // Need a fake document in order to initialize katex, and it needs to be strict |
---|
21 | const DTD = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'; |
---|
22 | const empty_dom = new JSDOM(DTD+'\n<html><body></body></html>'); |
---|
23 | setGlobal('document', empty_dom.window.document); |
---|
24 | |
---|
25 | const path = require("path"); |
---|
26 | setGlobal('katex', require(path.resolve("./build/html/_static/katex/katex.min"))); |
---|
27 | const renderMathInElement = require(path.resolve("./build/html/_static/katex/contrib/auto-render.min")); |
---|
28 | //setGlobal('katex', require("../../katex.js")); |
---|
29 | //const renderMathInElement = require("../auto-render/auto-render"); |
---|
30 | |
---|
31 | function isDir(path) { |
---|
32 | const stat = fs.statSync(path); |
---|
33 | return (stat && stat.isDirectory()); |
---|
34 | } |
---|
35 | |
---|
36 | /** |
---|
37 | * Walk a directory tree, yielding a list of matching filenames |
---|
38 | * |
---|
39 | * @param {path} dir - root of the tree which is walked |
---|
40 | * @param {regex} [pattern] - regular expression match for the full path |
---|
41 | */ |
---|
42 | function* walkit(dir, pattern = /./) { |
---|
43 | const _walk = function* (dir) { |
---|
44 | const entries = fs.readdirSync(dir); |
---|
45 | for (const entry of entries) { |
---|
46 | const path = dir + "/" + entry; |
---|
47 | if (isDir(path)) { |
---|
48 | //console.log('walking '+path); |
---|
49 | yield* _walk(path); |
---|
50 | } else if (pattern.test(path)) { |
---|
51 | //console.log('yielding '+path); |
---|
52 | yield path; |
---|
53 | } |
---|
54 | } |
---|
55 | }; |
---|
56 | yield* _walk(dir); |
---|
57 | } |
---|
58 | |
---|
59 | function processHtml(path) { |
---|
60 | JSDOM.fromFile(path).then(dom => { |
---|
61 | console.log("====> processing", path); |
---|
62 | //console.log(dom.serialize()); |
---|
63 | setGlobal('document', dom.window.document); |
---|
64 | //console.log(document.compatMode); |
---|
65 | renderMathInElement(dom.window.document.body); |
---|
66 | //console.log(dom.serialize()); |
---|
67 | fs.writeFileSync(path, dom.serialize()); |
---|
68 | }).catch(console.log.bind(console)); |
---|
69 | } |
---|
70 | |
---|
71 | function processTree(dir) { |
---|
72 | //console.log(process.argv.slice(2)); |
---|
73 | for (const path of walkit(dir, /\.html$/)) { |
---|
74 | processHtml(path); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | if (typeof require !== 'undefined' && require.main === module) { |
---|
79 | //console.log(process.argv.slice(2)); |
---|
80 | for (const arg of process.argv.slice(2)) { |
---|
81 | if (isDir(arg)) { |
---|
82 | processTree(arg); |
---|
83 | } else if (/\.html$/.test(arg)) { |
---|
84 | processHtml(arg); |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|