Skip to content

Instantly share code, notes, and snippets.

@ssylvan
Last active January 12, 2016 03:24
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 ssylvan/8b19fb016983a1ae38b7 to your computer and use it in GitHub Desktop.
Save ssylvan/8b19fb016983a1ae38b7 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const jsdom = require("jsdom").jsdom;
const cwd = path.resolve(process.argv[2]);
console.log(cwd);
var files = glob.sync("*.html", {cwd: path.resolve(process.argv[2]), matchBase:true, follow: true, realpath:true});
var fileLen = files.length;
var mjAPI = require("./lib/mj-page.js");
mjAPI.start();
// This all works with just one file, but trying to do it on all files in a directory fails in weird ways.
for(var fileIx = 0; fileIx < files.length; ++fileIx)
{
var file = files[fileIx];
const data = fs.readFile(file, 'utf8', function (err, data){
if (err)
{
console.log("Error reading file" + this.file);
}
var document = jsdom(data);
console.log("Processing: " + this.file);
//mjAPI.start() here? Doesn't work either...
mjAPI.typeset({
html: document.body.innerHTML,
}, function(result) {
console.log("Writing output " + this.file)
"use strict";
this.document.body.innerHTML = result.html;
var HTML = "<!DOCTYPE html>\n" + this.document.documentElement.outerHTML.replace(/^(\n|\s)*/, "");
fs.writeFileSync(this.file, HTML, 'utf8');
}.bind({file: this.file, document:document})
);
}.bind({fileIx:fileIx, file:file}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment