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"); |
---|
17 | //const await = require("await"); |
---|
18 | const mjpage = require('mathjax-node-page').mjpage; |
---|
19 | |
---|
20 | function isDir(path) { |
---|
21 | const stat = fs.statSync(path); |
---|
22 | return (stat && stat.isDirectory()); |
---|
23 | } |
---|
24 | |
---|
25 | /** |
---|
26 | * Walk a directory tree, yielding a list of matching filenames |
---|
27 | * |
---|
28 | * @param {path} dir - root of the tree which is walked |
---|
29 | * @param {regex} [pattern] - regular expression match for the full path |
---|
30 | */ |
---|
31 | function* walkit(dir, pattern = /./) { |
---|
32 | const _walk = function* (dir) { |
---|
33 | const entries = fs.readdirSync(dir); |
---|
34 | for (const entry of entries) { |
---|
35 | const path = dir + "/" + entry; |
---|
36 | if (isDir(path)) { |
---|
37 | //console.log('walking '+path); |
---|
38 | yield* _walk(path); |
---|
39 | } else if (pattern.test(path)) { |
---|
40 | //console.log('yielding '+path); |
---|
41 | yield path; |
---|
42 | } |
---|
43 | } |
---|
44 | }; |
---|
45 | yield* _walk(dir); |
---|
46 | } |
---|
47 | |
---|
48 | function readFileAsync(path) { |
---|
49 | return new Promise((resolve, reject) => |
---|
50 | fs.readFile(path, (err, data) => { |
---|
51 | if (err) { reject(err); } else { resolve(data); } |
---|
52 | })); |
---|
53 | } |
---|
54 | |
---|
55 | function writeFileAsync(path, data) { |
---|
56 | return new Promise((resolve, reject) => |
---|
57 | fs.writeFile(path, data, (err) => { |
---|
58 | if (err) { reject(err); } else { resolve(data); } |
---|
59 | })); |
---|
60 | } |
---|
61 | |
---|
62 | const pageConfig = { |
---|
63 | format: ["TeX"], |
---|
64 | //output: 'html', |
---|
65 | output: 'svg', |
---|
66 | fontURL: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js/fonts/HTML-CSS', |
---|
67 | MathJax: { |
---|
68 | //config: ["MMLorHTML.js"], |
---|
69 | //jax: ["input/TeX","input/MathML","output/HTML-CSS", |
---|
70 | // "output/NativeMML", "output/PreviewHTML"], |
---|
71 | jax: ["input/TeX", "output/HTML-CSS"], |
---|
72 | //extensions: ["tex2jax.js","mml2jax.js","MathMenu.js", |
---|
73 | // "MathZoom.js", "fast-preview.js", |
---|
74 | // "AssistiveMML.js", "a11y/accessibility-menu.js" |
---|
75 | // ], |
---|
76 | extensions: ["tex2jax.js"], |
---|
77 | TeX: { |
---|
78 | extensions: ["AMSmath.js","AMSsymbols.js", |
---|
79 | "noErrors.js","noUndefined.js"] |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | const mjnodeConfig = { |
---|
84 | ex: 6, // ex-size in pixels |
---|
85 | width: 100, // width of math container (in ex) for linebreaking and tags |
---|
86 | fragment: false, |
---|
87 | useFontCache: true, // use <defs> and <use> in svg output? |
---|
88 | useGlobalCache: false, // use common <defs> for all equations? |
---|
89 | //state: mjstate, // track global state |
---|
90 | linebreaks: false, // do linebreaking? |
---|
91 | equationNumbers: "none", // or "AMS" or "all" |
---|
92 | //math: "", // the math to typeset |
---|
93 | html: true, // generate HTML output? |
---|
94 | css: true, // generate CSS for HTML output? |
---|
95 | //mml: false, // generate mml output? |
---|
96 | //svg: false, // generate svg output? |
---|
97 | //speakText: true, // add spoken annotations to output? |
---|
98 | //timeout: 10 * 1000, // 10 second timeout before restarting MathJax |
---|
99 | } |
---|
100 | function mjpageAsync(input, pageConfig, mjnodeConfig) { |
---|
101 | return new Promise((resolve, reject) => |
---|
102 | mjpage(input, pageConfig, mjnodeConfig, output => resolve(output)) |
---|
103 | ); |
---|
104 | } |
---|
105 | async function updateHtmlAsync(path) { |
---|
106 | try { |
---|
107 | console.log("====> processing", path); |
---|
108 | const input = await readFileAsync(path); |
---|
109 | const output = await mjpageAsync(input, pageConfig, mjnodeConfig); |
---|
110 | return await writeFileAsync(path, output); |
---|
111 | } catch (err) { |
---|
112 | console.log(err); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | async function processTreeAsync(dir) { |
---|
117 | //console.log(dir); |
---|
118 | for (const path of walkit(dir, /\.html$/)) { |
---|
119 | await updateHtmlAsync(path); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | async function mainAsync(args) { |
---|
124 | //console.log(process.argv.slice(2)); |
---|
125 | for (const arg of args) { |
---|
126 | if (isDir(arg)) { |
---|
127 | processTreeAsync(arg); |
---|
128 | } else if (/\.html$/.test(arg)) { |
---|
129 | updateHtmlAsync(arg); |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | function updateHtmlSortOfSync(path) { |
---|
136 | console.log("====> loading", path); |
---|
137 | const input = fs.readFileSync(path); |
---|
138 | //const pageConfig = {format: ['TeX']} |
---|
139 | //const mjnodeConfig = {html: true} |
---|
140 | mjpage(input, pageConfig, mjnodeConfig, |
---|
141 | //output => console.log(output)); |
---|
142 | output => { |
---|
143 | console.log("====> processed", path); |
---|
144 | fs.writeFileSync(path, output) |
---|
145 | }); |
---|
146 | } |
---|
147 | |
---|
148 | function processTreeSortOfSync(dir) { |
---|
149 | //console.log(dir); |
---|
150 | for (const path of walkit(dir, /\.html$/)) { |
---|
151 | updateHtmlSortOfSync(path); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | function mainSortOfSync(args) { |
---|
156 | //console.log(process.argv.slice(2)); |
---|
157 | for (const arg of args) { |
---|
158 | if (isDir(arg)) { |
---|
159 | processTreeSortOfSync(arg); |
---|
160 | } else if (/\.html$/.test(arg)) { |
---|
161 | updateHtmlSortOfSync(arg); |
---|
162 | } |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | if (typeof require !== 'undefined' && require.main === module) { |
---|
167 | console.log(mainAsync(process.argv.slice(2))); |
---|
168 | } |
---|