source: sasview/docs/sphinx-docs/convertKaTex.js @ 593b536d

ESS_GUIESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_sync_sascalc
Last change on this file since 593b536d was a686617, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

change symlink for convertKaTex.js into a regular file

  • Property mode set to 100755
File size: 2.7 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");  // standard node
17const jsdom = require("jsdom");  // npm install jsdom
18const { JSDOM } = jsdom;
19
20// Need a fake document in order to initialize katex, and it needs to be strict
21const DTD = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
22const empty_dom = new JSDOM(DTD+'\n<html><body></body></html>');
23setGlobal('document', empty_dom.window.document);
24
25const path = require("path");
26setGlobal('katex', require(path.resolve("./build/html/_static/katex/katex.min")));
27const 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
31function 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 */
42function* 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
59function 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
71function processTree(dir) {
72    //console.log(process.argv.slice(2));
73    for (const path of walkit(dir, /\.html$/)) {
74        processHtml(path);
75    }
76}
77
78if (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}
Note: See TracBrowser for help on using the repository browser.