Skip to content

Instantly share code, notes, and snippets.

@pkra
Created April 24, 2015 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkra/7cc7a68cb6f43afd27ca to your computer and use it in GitHub Desktop.
Save pkra/7cc7a68cb6f43afd27ca to your computer and use it in GitHub Desktop.
Testing cheerio for MathJax-node
#! /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