Created
April 24, 2015 19:40
-
-
Save pkra/7cc7a68cb6f43afd27ca to your computer and use it in GitHub Desktop.
Testing cheerio for MathJax-node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env node | |
var mjAPI = require("MathJax-node/lib/mj-single"); | |
var typeset = mjAPI.typeset; | |
var fs = require('fs'); | |
var path = require('path'); | |
var async = require('async'); | |
var cheerio = require('cheerio'); | |
var argv = require("yargs") | |
.strict() | |
.usage("Usage: jats -i input.html -o output.html", { | |
i: { | |
describe: 'specify input file', | |
alias: ('i', 'input') | |
}, | |
o: { | |
describe: 'specify output file', | |
alias: ('o', 'output') | |
} | |
}) | |
.demand(['i', 'o']) | |
.argv; | |
var inputFile = fs.readFileSync(path.join(__dirname, argv.i)); | |
var $ = cheerio.load(inputFile); | |
mjAPI.config({ | |
MathJax: { | |
menuSettings: { | |
semantics: true, | |
}, | |
"displayAlign": "left" | |
} | |
}); | |
mjAPI.start(); | |
function processMath(mathNode, callback) { | |
var thisMathNode = mathNode; | |
var mmlString = $.html(mathNode); | |
console.log(mmlString); | |
typeset({ | |
format: "MathML", | |
math: mmlString, | |
svg: true, | |
}, function(data) { | |
if (!data.errors) { | |
if (data.svg) { | |
var svgNode = data.svg; | |
$(thisMathNode).parent().append(svgNode); | |
} | |
} | |
callback(data.errors); | |
}); | |
} | |
var mathNodes = $('mml\\:math, math').get(); | |
async.each(mathNodes, processMath, function(err) { | |
if (err) { | |
throw err; | |
} | |
fs.writeFile(argv.o, $.html(), function(err) { | |
if (err) { | |
throw err; | |
} | |
}); | |
console.log("It\'s saved!"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment