automatic restaging
authorLuc Moreau <l.moreau@ecs.soton.ac.uk>
Wed, 20 Feb 2013 23:49:04 +0000
changeset 5635 3971808d7bbb
parent 5634 d23266421855
child 5636 9df76a4c14e9
automatic restaging
model/respec2html.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/model/respec2html.js	Wed Feb 20 23:49:04 2013 +0000
@@ -0,0 +1,56 @@
+// respec2html is a command line utility that converts a ReSpec source file to an HTML file.
+// Depends on PhantomJS <http://phantomjs.org>.
+
+var page = require('webpage').create(),
+    args = require('system').args,
+    fs = require('fs'),
+    source = args[1],
+    output = args[2],
+    timeout = (!isNaN(args[3])) ? parseInt(args[3], 10) : 10;
+
+if (args.length !== 3 && args.length !== 4) {
+    console.log('Usage:\n   phantomjs respec2html.js respec-source html-output [timeout]\n');
+    console.log('   respec-source  ReSpec source file, or an URL to the file');
+    console.log('   html-output    Name for the HTML file to be generated');
+    console.log('   [timeout]      An optional timeout in seconds, default is 10\n');
+    phantom.exit();
+}
+
+page.open(source, function(status) {
+    page.onResourceRequested = function(request) {
+        console.log('Loading ' + request.url);
+    };
+    
+    if (status !== 'success') {
+        console.log('Unable to access ReSpec source file.');
+        phantom.exit();
+    } else {
+        console.log('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 (unsupported by PhantomJS).
+            var done = page.evaluate(function() { return (document.respecDone) ? true : false; });
+            if (done) {
+                clearInterval(timer);
+                console.log('Serializing the DOM into HTML...');
+                var html = page.evaluate(function() {
+                    // Serialize the DOM using the built-in serializer.
+                    (new berjon.respec()).toHTMLSource();
+                    var outer = document.querySelector('html').outerHTML;
+                    return '<!DOCTYPE html>\n' + outer.replace('<head>', '\n<head>');
+                });
+                fs.write(output, html, 'w');
+                console.log(output + ' created!\n');
+                phantom.exit();
+            } else {
+                if (timeout === 0) {
+                    clearInterval(timer);
+                    console.log('Timeout loading ' + source + '. Is it a valid ReSpec source file?');
+                    phantom.exit();
+                } else {
+                    console.log('Timing out in ' + timeout-- + ' s');
+                }
+            }
+        }, 1000);
+    }
+});
\ No newline at end of file