Skip to content

Commit

Permalink
use proper methods for respec2html
Browse files Browse the repository at this point in the history
  • Loading branch information
darobin committed Jan 23, 2014
1 parent 1258dea commit 38e61f1
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions tools/respec2html.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ var page = require("webpage").create()
, source = args[1]
, output = args[2]
, timeout = isNaN(args[3]) ? 10: parseInt(args[3], 10)
, timer
;

if (args.length !== 3 && args.length !== 4) {
var usage = "Usage:\n phantomjs --ssl-protocol=any respec2html.js respec-source html-output [timeout]\n" +
if (args.length < 2 || args.length > 4) {
var usage = "Usage:\n phantomjs --ssl-protocol=any respec2html.js respec-source [html-output] [timeout]\n" +
" respec-source ReSpec source file, or an URL to the file" +
" [html-output] Name for the HTML file to be generated, defaults to stdout" +
" [timeout] An optional timeout in seconds, default is 10\n";
Expand All @@ -26,38 +27,34 @@ page.open(source, function (status) {
phantom.exit(1);
}
else {
console.error("Loading " + source);
var timer = setInterval(function () {
// Poll document.respecDone for doneness. A proper way would be to listen
// for the end-all message on respecEvents.
var done = page.evaluate(function () { return document && document.respecDone; });
if (done) {
clearInterval(timer);
console.log("Serializing the DOM into HTML...");
page.evaluateAsync(function () {
require(["core/ui", "ui/save-html"], function (ui, saver) {
saver.show(ui, respecConfig, document, respecEvents);
window.callPhantom({ html: saver.toString() });
});
if (output) console.error("Loading " + source);
page.evaluateAsync(function () {
respecEvents.sub("end-all", function () {
require(["core/ui", "ui/save-html"], function (ui, saver) {
saver.show(ui, respecConfig, document, respecEvents);
window.callPhantom({ html: saver.toString() });
});
} else {
if (timeout === 0) {
clearInterval(timer);
console.error("Timeout loading " + source + ".\n" +
" Is it a valid ReSpec source file?\n" +
" Did you forget --ssl-protocol=any?");
phantom.exit(1);
} else {
console.error("Timing out in " + --timeout + "s...");
}
});
});
timer = setInterval(function () {
if (timeout === 0) {
clearInterval(timer);
console.error("Timeout loading " + source + ".\n" +
" Is it a valid ReSpec source file?\n" +
" Did you forget --ssl-protocol=any?");
phantom.exit(1);
}
else {
if (output) console.error("Timing out in " + --timeout + "s...");
}
}, 1000);
}
});

page.onCallback = function (data) {
clearInterval(timer);
if (output) fs.write(output, data.html, "w");
else console.log(data.html);
console.error((output || "Output") + " created!");
if (output) console.error(output + " created!");
phantom.exit(0);
};

0 comments on commit 38e61f1

Please sign in to comment.