This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
forked from speced/respec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
respec2html.js
executable file
·132 lines (117 loc) · 4.41 KB
/
respec2html.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env phantomjs --ssl-protocol=any
/*global phantom, respecEvents, respecConfig*/
// 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.slice()
, fs = require("fs")
, timer
, reportErrors = false
, reportWarnings = false
, ignoreScripts = false
, errors = []
, warnings = []
, delay = 0
;
// report console.error on stderr
console.error = function () {
require("system").stderr.write(Array.prototype.join.call(arguments, ' ') + '\n');
};
var eOption = args.indexOf("-e");
if (eOption !== -1) {
args.splice(args.indexOf("-e"), 1);
reportErrors = true;
}
if (args.indexOf("-w") !== -1) {
args.splice(args.indexOf("-w"), 1);
reportWarnings = true;
}
if (args.indexOf("--delay") !== -1) {
delay = args.splice(args.indexOf("--delay"), 2)[1];
}
if (args.indexOf("--exclude-script") !== -1) {
var idx = args.indexOf("--exclude-script");
var values = args.splice(idx, 2);
ignoreScripts = values[1];
}
// Reading other parameters
var source = args[1]
, output = args[2]
, timeout = isNaN(args[3]) ? 10: parseInt(args[3], 10);
if (args.length < 2 || args.length > 4) {
var usage = "Usage:\n phantomjs --ssl-protocol=any respec2html.js [-e] [-w] [--exclude-script url] respec-source [html-output] [timeout]\n" +
" respec-source ReSpec source file, or an URL to the file\n" +
" [-e] Report ReSpec errors on stderr\n" +
" [-w] Report ReSpec warnings on stderr\n" +
" [--exclude-script url] Do not load scripts whose source\n" +
" starts with the passed URL\n" +
" [html-output] Name for the HTML file to be generated," +
" defaults to stdout\n" +
" [timeout] An optional timeout in seconds, default is 10\n";
console.error(usage);
phantom.exit(1);
}
// Dealing with ReSpec source being loaded with scheme-relative link
// i.e. <script src='//www.w3.org/Tools/respec/respec-w3c-common'>
page.onResourceRequested = function (requestData, networkRequest) {
if (requestData.url === "file://www.w3.org/Tools/respec/respec-w3c-common") {
networkRequest.changeUrl("https://www.w3.org/Tools/respec/respec-w3c-common");
} else if (ignoreScripts && requestData.url.indexOf(ignoreScripts) === 0) {
networkRequest.abort();
}
};
page.onConsoleMessage = function (msg) {
if (msg.match(/^ERROR: /)) {
errors.push(msg);
} else if (msg.match(/^WARN: /)) {
warnings.push(msg);
}
};
page.open(source, function (status) {
if (status !== "success") {
console.error("Unable to access ReSpec source file.");
phantom.exit(1);
}
else {
console.error("Loading " + source);
setTimeout(function() { page.evaluateAsync(function () {
function saveToPhantom () {
require(["core/ui", "ui/save-html"], function (ui, saver) {
saver.show(ui, respecConfig, document, respecEvents);
window.callPhantom({ html: saver.toString() });
});
}
if (document.respecDone) saveToPhantom();
else respecEvents.sub("end-all", saveToPhantom);
}, delay*1000);
});
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 {
console.error("Timing out in " + --timeout + "s...");
}
}, 1000);
}
});
page.onCallback = function (data) {
clearInterval(timer);
var exit = 0;
if (warnings.length && reportWarnings) {
console.error(warnings.join("\n"));
exit = 65;
}
if (errors.length && reportErrors) {
console.error(errors.join("\n"));
exit = 64;
}
if (output) fs.write(output, data.html, "w");
else console.log(data.html);
if (output) console.error(output + " created!");
phantom.exit(exit);
};