/* report-to-html.phjs: $distribution_URI Copyright © 2012 World Wide Web Consortium, (Massachusetts Institute of Technology, European Research Consortium for Informatics and Mathematics, Keio University). All Rights Reserved. This work is distributed under the W3C® Software License [1] in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231 This program is intended to be executed using phantomjs [2]. syntax: phantomjs report-to-html.phjs [2] http://phantomjs.org/ */ var system = require('system'); var fs = require('fs'); if (system.args.length < 2) { console.error('[error] phantomjs : report-to-html.phjs: Missing arguments:' + ' use phantomjs report-to-html.phjs '); phantom.exit(1); // ensure it crashes since the exit doesn't seem to do what's expected foo.bar(); } var arg_index = 1; var reportname = system.args[arg_index]; var show_anchors = false; var MAX_LEVEL = 3; while (arg_index < system.args.length && system.args[arg_index].indexOf("--") === 0) { var option = system.args[arg_index].substring(2); switch(option) { case "help": console.log("You're on your own. Good luck."); phantom.exit(1);foo.bar(); break; case "version": console.log("1.0"); phantom.exit(1);foo.bar(); break; case "anchors": show_anchors = true; break; case "report": arg_index++; reportname = system.args[arg_index]; break; case "max-level": arg_index++; MAX_LEVEL = parseInt(system.args[arg_index]); break; } arg_index++; } var report = null; { var file = fs.read(reportname); try { report = JSON.parse(file); } catch (e) { console.error(e); phantom.exit(1); } } console.log("Report: " + reportname); console.log("MAX LEVEL: " + MAX_LEVEL); function getSection(caniuse) { var browsers = "
\n
Desktop
\n
"; var index = 0; for (var browser in caniuse.stats) { var y = false; var p = false; var n = false; var u = false; browsers += "(" + browser; var r = ""; for (var version in caniuse.stats[browser]) { switch (caniuse.stats[browser][version]) { case "y": y = true; break; case "p": p = true; break; case "n": n = true; break; case "u": u = true; break; default: r=caniuse.stats[browser][version]; } } if (y) r="y"; else if (p) r="p"; else if (n) r="n"; else if (u) r="u"; browsers += ":" + r + ")"; if (index++===5){ browsers+="
\n
Mobile
\n
" } } browsers += "
\n
\n"; return "" + caniuse.title + "\n" + browsers; } function generateSection(output, section, level, initial, subsections) { var title = (typeof section.title === "string")? section.title : '#' + section.id; var total = (typeof section.total === "number")? section.total : 0; var caniuse = (typeof section.caniuse === "object")? getSection(section.caniuse) : ""; if (!show_anchors && typeof section.title !== "string") return; output.write("\n"); output.write(""); var needsClosing = false; if (level > MAX_LEVEL) { output.write(""); needsClosing = true; } else if (level == MAX_LEVEL && !initial && typeof section.sections === "object") { output.write(""); generate(section, level, true); needsClosing = true; } output.write(title); if (needsClosing) { output.write(""); } output.write("\n"); output.write("" + total + "\n"); output.write("" + caniuse + "\n"); output.write("\n"); if ((typeof section.sections === "object") && (subsections || level < MAX_LEVEL)) { section.sections.forEach(function(x) { generateSection(output, x, level + 1, false, subsections); }); } } function generate(section, level, subsections) { var outputname = "html/" + section.title.replace(/[&\s\.\/\+]/g, '_') + ".html"; var output = fs.open(outputname, "w"); console.debug("[debug] outputname is " + outputname); output.write("\n"); output.write("\n"); output.write("\n"); output.write("" + section.title + " report\n"); output.write("\n"); output.write("\n"); output.write("\n"); output.write("

" + section.title + " report

\n"); output.write("\n"); output.write("\n"); output.write("\n"); output.write("\n"); output.write("\n"); output.write("\n"); output.write("\n"); output.write("\n"); output.write("\n"); generateSection(output, section, level, true, subsections); output.write("\n"); output.write("
Table of ContentsTotal # of testsCan I use…
\n"); output.write("\n"); output.write("\n"); output.close(); } generate(report, 0, false); phantom.exit(0);