Skip to content

Instantly share code, notes, and snippets.

@darobin
Created December 11, 2012 16: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 darobin/4260155 to your computer and use it in GitHub Desktop.
Save darobin/4260155 to your computer and use it in GitHub Desktop.
Make the HTML TS structure
var fs = require("fs")
, pth = require("path")
, _ = require("underscore")
, jsdom = require("jsdom")
, wrench = require("wrench")
, mkdirp = require("mkdirp").sync
// use hg for source, gh for target
, sourceDir = pth.join(__dirname, "../html")
, targetDir = pth.join(__dirname, "../html-testsuite")
, sourceTestDir = pth.join(sourceDir, "tests")
, testDir = pth.join(targetDir, "tests")
, MAX_DEPTH = 3
;
console.log("Reset target.");
if (fs.existsSync(testDir)) wrench.rmdirSyncRecursive(testDir);
console.log("Move harness and reporting dirs out of test.");
mkdirp(testDir);
wrench.copyDirSyncRecursive(pth.join(sourceDir, "tests/harness"), pth.join(targetDir, "harness"));
wrench.copyDirSyncRecursive(pth.join(sourceDir, "tests/reporting"), pth.join(targetDir, "reporting"));
var sections = {
html5: "/Projects/htmlwg.org/drafts/output/html/CR/Overview.html"
, html51: "/Projects/htmlwg.org/drafts/output/html/master/Overview.html"
, canvas2d: "/Projects/htmlwg.org/drafts/output/2dcontext/html5_canvas_CR/Overview.html"
, canvas2d2: "/Projects/htmlwg.org/drafts/output/2dcontext/html5_canvas/Overview.html"
, microdata: "/Projects/htmlwg.org/drafts/output/microdata/CR/Overview.html"
};
function walkTree ($, $el, list) {
$el.find("> li").each(function () {
var $li = $(this)
, $a = $li.find("> a").first()
;
// skip sections that don't have a number
if (!/^\s*\d+/.test($a.text())) return;
var def = { id: $a.attr("href").replace(/^.*#/, "") }
, $ol = $li.find("> ol").first()
;
if ($ol.length) {
def.children = [];
walkTree($, $ol, def.children);
}
list.push(def);
});
}
function extractSections (sec, secDir, spec, cb) {
jsdom.defaultDocumentFeatures.FetchExternalResources = false;
jsdom.defaultDocumentFeatures.ProcessExternalResources = false;
jsdom.defaultDocumentFeatures.MutationEvents = "2.0";
jsdom.defaultDocumentFeatures.SkipExternalResources = true;
jsdom.env(
spec
, function (err, window) {
if (err) return cb(err);
jsdom.jQueryify(window, "/Projects/COMMON/jquery.min.js", function (window, $) {
if (!$) return cb("$ was not defined");
var $root = $("body > ol.toc").first()
, tree = []
;
walkTree($, $root, tree);
cb(null, tree, sec, secDir);
}
);
});
}
function makeDirs (base, tree, depth) {
console.log("Making " + base + " at depth " + depth);
for (var i = 0, n = tree.length; i < n; i++) {
var sec = tree[i]
, sane = sec.id.replace(/\//g, "_")
, path = pth.join(base, sane)
;
mkdirp(path);
fs.writeFileSync(pth.join(path, ".gitkeep"), "", "utf8");
if (sec.children && sec.children.length) {
if (depth === 3) {
fs.writeFileSync(pth.join(path, "contains.json"), JSON.stringify(sec.children, null, 4), "utf8");
}
else {
makeDirs(path, sec.children, depth + 1);
}
}
}
}
for (var sec in sections) {
var secDir = pth.join(testDir, sec);
mkdirp(secDir);
console.log("Launching extraction for " + sec);
extractSections(sec, secDir, sections[sec], function (err, toc, sec, secDir) {
if (err) console.log("ERROR: " + err);
makeDirs(secDir, toc, 1);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment