source: sasview/docs/sphinx-docs/convertMathJax.js @ 8f1bb6f

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalcmagnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 8f1bb6f was 8f1bb6f, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

prerender mathjax for comparison

  • Property mode set to 100755
File size: 4.9 KB
Line 
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 */
12function setGlobal(name, value) {
13    (function(globals) { globals[name] = value; }((1, eval)('this')));
14}
15
16const fs = require("fs");
17//const await = require("await");
18const mjpage = require('mathjax-node-page').mjpage;
19
20function 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 */
31function* 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
48function 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
55function 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
62const pageConfig = {
63    format: ["TeX"],
64    output: 'html',
65    fontURL: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js/fonts/HTML-CSS',
66    MathJax: {
67            //config: ["MMLorHTML.js"],
68            //jax: ["input/TeX","input/MathML","output/HTML-CSS",
69            //      "output/NativeMML", "output/PreviewHTML"],
70            jax: ["input/TeX", "output/HTML-CSS"],
71            //extensions: ["tex2jax.js","mml2jax.js","MathMenu.js",
72            //             "MathZoom.js", "fast-preview.js",
73            //             "AssistiveMML.js", "a11y/accessibility-menu.js"
74            //            ],
75            extensions: ["tex2jax.js"],
76            TeX: {
77                extensions: ["AMSmath.js","AMSsymbols.js",
78                             "noErrors.js","noUndefined.js"]
79            }
80    }
81}
82const mjnodeConfig = {
83  ex: 6, // ex-size in pixels
84  width: 100, // width of math container (in ex) for linebreaking and tags
85  fragment: false,
86  useFontCache: true, // use <defs> and <use> in svg output?
87  useGlobalCache: false, // use common <defs> for all equations?
88  //state: mjstate, // track global state
89  linebreaks: false, // do linebreaking?
90  equationNumbers: "none", // or "AMS" or "all"
91  //math: "", // the math to typeset
92  html: true, // generate HTML output?
93  css: true, // generate CSS for HTML output?
94  //mml: false, // generate mml output?
95  //svg: false, // generate svg output?
96  //speakText: true, // add spoken annotations to output?
97  //timeout: 10 * 1000, // 10 second timeout before restarting MathJax
98}
99function mjpageAsync(input, pageConfig, mjnodeConfig) {
100    return new Promise((resolve, reject) =>
101        mjpage(input, pageConfig, mjnodeConfig, output => resolve(output))
102    );
103}
104async function updateHtmlAsync(path) {
105    console.log("====> processing", path);
106    const input = await readFileAsync(path);
107    const output = await mjpageAsync(input, pageConfig, mjnodeConfig);
108    return await writeFileAsync(path, output);
109}
110
111async function processTreeAsync(dir) {
112    //console.log(dir);
113    for (const path of walkit(dir, /\.html$/)) {
114        await updateHtmlAsync(path);
115    }
116}
117
118async function mainAsync(args) {
119    //console.log(process.argv.slice(2));
120    for (const arg of args) {
121        if (isDir(arg)) {
122            processTreeAsync(arg);
123        } else if (/\.html$/.test(arg)) {
124            updateHtmlAsync(arg);
125        }
126    }
127}
128
129
130function updateHtmlSortOfSync(path) {
131    console.log("====> loading", path);
132    const input = fs.readFileSync(path);
133    //const pageConfig = {format: ['TeX']}
134    //const mjnodeConfig = {html: true}
135    mjpage(input, pageConfig, mjnodeConfig,
136            //output => console.log(output));
137            output => {
138                console.log("====> processed", path);
139                fs.writeFileSync(path, output)
140            });
141}
142
143function processTreeSortOfSync(dir) {
144    //console.log(dir);
145    for (const path of walkit(dir, /\.html$/)) {
146        updateHtmlSortOfSync(path);
147    }
148}
149
150function mainSortOfSync(args) {
151    //console.log(process.argv.slice(2));
152    for (const arg of args) {
153        if (isDir(arg)) {
154            processTreeSortOfSync(arg);
155        } else if (/\.html$/.test(arg)) {
156            updateHtmlSortOfSync(arg);
157        }
158    }
159}
160
161if (typeof require !== 'undefined' && require.main === module) {
162    console.log(mainAsync(process.argv.slice(2)));
163}
Note: See TracBrowser for help on using the repository browser.