public
Last active

Make the HTML TS structure

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
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);
});
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.