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
/
examples.js
76 lines (73 loc) · 3.47 KB
/
examples.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
// Module core/examples
// Manages examples, including marking them up, numbering, inserting the title,
// and reindenting.
// Examples are any pre element with class "example" or "illegal-example".
// When an example is found, it is reported using the "example" event. This can
// be used by a containing shell to extract all examples.
define(
["text!core/css/examples.css", "text!core/css/examples-webspecs.css"],
function (css, cssKraken) {
var makeTitle = function (conf, $el, num, report) {
var txt = (num > 0) ? " " + num : ""
, $tit = $("<div class='example-title'><span>Example" + txt + "</span></div>");
report.title = $el.attr("title");
if (report.title) {
$tit.append($("<span style='text-transform: none'>: " + report.title + "</span>"));
$el.removeAttr("title");
}
$tit.addClass("marker") ;
return $tit;
};
return {
run: function (conf, doc, cb, msg) {
msg.pub("start", "core/examples");
var $exes = $("pre.example, pre.illegal-example, aside.example")
, num = 0
;
if ($exes.length) {
if (conf.specStatus === "webspec") css += cssKraken;
$(doc).find("head link").first().before($("<style/>").text(css));
$exes.each(function (i, ex) {
var $ex = $(ex)
, report = { number: num, illegal: $ex.hasClass("illegal-example") }
;
if ($ex.is("aside")) {
num++;
var $tit = makeTitle(conf, $ex, num, report);
$ex.prepend($tit);
msg.pub("example", report);
}
else {
var inAside = !!$ex.parents("aside").length;
if (!inAside) num++;
// reindent
var lines = $ex.html().split("\n");
while (lines.length && /^\s*$/.test(lines[0])) lines.shift();
while (lines.length && /^\s*$/.test(lines[lines.length - 1])) lines.pop();
var matches = /^(\s+)/.exec(lines[0]);
if (matches) {
var rep = new RegExp("^" + matches[1]);
for (var j = 0; j < lines.length; j++) {
lines[j] = lines[j].replace(rep, "");
}
}
report.content = lines.join("\n");
$ex.html(lines.join("\n"));
$ex.removeClass("example illegal-example");
// wrap
var $div = $("<div class='example'></div>")
, $tit = makeTitle(conf, $ex, inAside ? 0 : num, report)
;
$div.append($tit);
$div.append($ex.clone());
$ex.replaceWith($div);
if (!inAside) msg.pub("example", report);
}
});
}
msg.pub("end", "core/examples");
cb();
}
};
}
);