- Timestamp:
- Sep 11, 2017 6:05:40 PM (7 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 1659f54
- Parents:
- bf80f7e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
docs/sphinx-docs/convertKaTex.js
- Property mode changed from 120000 to 100755
r3194371 ra686617 1 /Users/pkienzle/Source/KaTeX/contrib/preprocess/convertTree.js 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 }
Note: See TracChangeset
for help on using the changeset viewer.