2015-07-30 22:32:17 +00:00
|
|
|
/*global require,process,GLOBAL*/
|
|
|
|
/*jslint nomen: true*/
|
|
|
|
|
|
|
|
|
|
|
|
var CONSTANTS = {
|
|
|
|
DIAGRAM_WIDTH: 800,
|
|
|
|
DIAGRAM_HEIGHT: 500
|
|
|
|
};
|
|
|
|
|
2015-07-30 23:16:33 +00:00
|
|
|
GLOBAL.window = GLOBAL.window || GLOBAL; // nomnoml expects window to be defined
|
2015-07-30 22:32:17 +00:00
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var fs = require("fs"),
|
|
|
|
mkdirp = require("mkdirp"),
|
|
|
|
path = require("path"),
|
|
|
|
glob = require("glob"),
|
|
|
|
showdown = require("github-flavored-markdown"),
|
|
|
|
split = require("split"),
|
|
|
|
stream = require("stream"),
|
|
|
|
nomnoml = require('nomnoml'),
|
|
|
|
Canvas = require('canvas'),
|
|
|
|
options = require("minimist")(process.argv.slice(2));
|
|
|
|
|
|
|
|
function renderNomnoml(source, target) {
|
|
|
|
var canvas =
|
|
|
|
new Canvas(CONSTANTS.DIAGRAM_WIDTH, CONSTANTS.DIAGRAM_HEIGHT);
|
|
|
|
nomnoml.draw(canvas, source, 1.0);
|
|
|
|
canvas.pngStream().pipe(fs.createWriteStream(target));
|
|
|
|
}
|
|
|
|
|
|
|
|
function nomnomlifier(outputDirectory, prefix) {
|
|
|
|
var transform = new stream.Transform({ objectMode: true }),
|
|
|
|
isBuilding = false,
|
|
|
|
counter = 1,
|
|
|
|
outputPath,
|
|
|
|
source = "";
|
|
|
|
|
|
|
|
transform._transform = function (chunk, encoding, done) {
|
2015-07-30 23:16:33 +00:00
|
|
|
if (!isBuilding) {
|
2015-07-30 22:32:17 +00:00
|
|
|
if (chunk.trim().indexOf("```nomnoml") === 0) {
|
|
|
|
var outputFilename = prefix + '-' + counter + '.png';
|
|
|
|
outputPath = path.join(outputDirectory, outputFilename);
|
|
|
|
this.push([
|
|
|
|
"\n![Diagram ",
|
|
|
|
counter,
|
|
|
|
"](",
|
|
|
|
outputFilename,
|
|
|
|
")\n\n"
|
|
|
|
].join(""));
|
|
|
|
isBuilding = true;
|
|
|
|
source = "";
|
2015-07-30 23:16:33 +00:00
|
|
|
counter += 1;
|
2015-07-30 22:32:17 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, pass through
|
2015-07-30 23:16:33 +00:00
|
|
|
this.push(chunk + '\n');
|
2015-07-30 22:32:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (chunk.trim() === "```") {
|
|
|
|
// End nomnoml
|
|
|
|
renderNomnoml(source, outputPath);
|
|
|
|
isBuilding = false;
|
|
|
|
} else {
|
2015-07-30 23:16:33 +00:00
|
|
|
source += chunk + '\n';
|
2015-07-30 22:32:17 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-30 23:16:33 +00:00
|
|
|
done();
|
2015-07-30 22:32:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
function gfmifier() {
|
|
|
|
var transform = new stream.Transform({ objectMode: true }),
|
|
|
|
markdown = "";
|
|
|
|
transform._transform = function (chunk, encoding, done) {
|
|
|
|
markdown += chunk;
|
2015-07-30 23:16:33 +00:00
|
|
|
done();
|
2015-07-30 22:32:17 +00:00
|
|
|
};
|
2015-07-30 23:16:33 +00:00
|
|
|
transform._flush = function (done) {
|
|
|
|
this.push("<html><body>\n");
|
|
|
|
this.push(showdown.parse(markdown));
|
|
|
|
this.push("\n</body></html>\n");
|
|
|
|
done();
|
2015-07-30 22:32:17 +00:00
|
|
|
};
|
|
|
|
return transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
options['in'] = options['in'] || options.i;
|
|
|
|
options.out = options.out || options.o;
|
|
|
|
|
|
|
|
glob(options['in'] + "/**/*.md", {}, function (err, files) {
|
|
|
|
files.forEach(function (file) {
|
|
|
|
var destination = file.replace(options['in'], options.out)
|
|
|
|
.replace(/md$/, "html"),
|
|
|
|
destPath = path.dirname(destination),
|
|
|
|
prefix = path.basename(destination).replace(/\.html$/, "");
|
|
|
|
|
|
|
|
mkdirp(destPath, function (err) {
|
|
|
|
fs.createReadStream(file, { encoding: 'utf8' })
|
|
|
|
.pipe(split())
|
|
|
|
.pipe(nomnomlifier(destPath, prefix))
|
|
|
|
.pipe(gfmifier())
|
|
|
|
.pipe(fs.createWriteStream(destination, {
|
|
|
|
encoding: 'utf8'
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-08-03 19:49:35 +00:00
|
|
|
// Also copy over all HTML, CSS, or PNG files
|
|
|
|
glob(options['in'] + "/**/*.@(html|css|png)", {}, function (err, files) {
|
2015-07-30 23:36:17 +00:00
|
|
|
files.forEach(function (file) {
|
|
|
|
var destination = file.replace(options['in'], options.out),
|
|
|
|
destPath = path.dirname(destination);
|
|
|
|
|
|
|
|
mkdirp(destPath, function (err) {
|
|
|
|
fs.createReadStream(file, { encoding: 'utf8' })
|
|
|
|
.pipe(fs.createWriteStream(destination, {
|
|
|
|
encoding: 'utf8'
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-30 22:32:17 +00:00
|
|
|
}());
|