var fs = require("fs"), md = require("./lib/markdown");
CodeMirror = require("./lib/codemirror-node");
require("./lib/codemirror-rust");
md.Markdown.dialects.Maruku.block.code = function code(block, next) {
if (block.match(/^ /)) {
var text = block.replace(/(^|\n) /g, "$1"), accum = [], curstr = "", curstyle = null;
function add(str, style) {
if (style != curstyle) {
if (curstyle) accum.push(["span", {"class": "cm-" + curstyle}, curstr]);
else if (curstr) accum.push(curstr);
curstr = str; curstyle = style;
} else curstr += str;
}
CodeMirror.runMode(text, "rust", add);
add("", "bogus"); // Flush pending string.
return [["pre", {"class": "cm-s-default"}].concat(accum)];
}
};
function markdown(str) { return md.toHTML(str, "Maruku"); }
function fileDates(file, c) {
function takeTime(str) {
return Number(str.match(/^(\S+)\s/)[1]) * 1000;
}
require("child_process").exec("git rev-list --timestamp HEAD -- " + file + ".md", function(err, stdout) {
if (err != null) { console.log("Failed to run git rev-list"); return; }
var history = stdout.split("\n");
if (history.length && history[history.length-1] == "") history.pop();
var created = history.length ? takeTime(history[history.length-1]) : Date.now();
var modified = created;
if (history.length > 1) modified = takeTime(history[0]);
c(created, modified);
});
}
function head(title) {
return "
" +
"" +
"" +
title + "\n";
}
function foot(created, modified) {
var r = "
"
var crStr = formatTime(created), modStr = formatTime(modified);
if (created) r += "Created " + crStr;
if (crStr != modStr)
r += (created ? ", l" : "L") + "ast modified on " + modStr;
return r + "
";
}
function formatTime(tm) {
var d = new Date(tm);
var months = ["", "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"];
return months[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();
}
var files = fs.readFileSync("order", "utf8").split("\n").filter(function(x) { return x; });
var max_modified = 0;
var sections = [];
// Querying git for modified dates has to be done async in node it seems...
var queried = 0;
for (var i = 0; i < files.length; ++i)
(function(i) { // Make lexical i stable
fileDates(files[i], function(ctime, mtime) {
sections[i] = {
text: fs.readFileSync(files[i] + ".md", "utf8"),
ctime: ctime, mtime: mtime,
name: files[i],
};
max_modified = Math.max(mtime, max_modified);
if (++queried == files.length) buildTutorial();
});
})(i);
function htmlName(i) { return sections[i].name + ".html"; }
function buildTutorial() {
var index = head("Rust language tutorial") + "
" +
markdown(fs.readFileSync("index.md", "utf8")) + "";
for (var i = 0; i < sections.length; ++i) {
var s = sections[i];
var html = htmlName(i);
var title = s.text.match(/^# (.*)\n/)[1];
index += '