var fs = require("fs"), md = require("./lib/markdown");
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, 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[0]) : Date.now();
var modified = created;
if (history.length > 1) modified = takeTime(history[history.length-1]);
c(created, modified);
});
}
function head(title) {
return "
"
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 += '- ' + title + "
";
var nav = 'Section ' + (i + 1) + ' of the Rust language tutorial.
';
if (i > 0) nav += '« Section ' + i + " | ";
nav += 'Index';
if (i + 1 < sections.length) nav += ' | Section ' + (i + 2) + " »";
nav += "
";
fs.writeFileSync("web/" + html, head(title) + nav + '' + markdown(s.text) + "
" +
nav + foot(s.ctime, s.mtime) + "");
}
index += "
" + foot(null, max_modified) + "