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
/
inlines.js
112 lines (107 loc) · 6.21 KB
/
inlines.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
// Module core/inlines
// Process all manners of inline information. These are done together despite it being
// seemingly a better idea to orthogonalise them. The issue is that processing text nodes
// is harder to orthogonalise, and in some browsers can also be particularly slow.
// Things that are recognised are <abbr>/<acronym> which when used once are applied
// throughout the document, [[REFERENCES]]/[[!REFERENCES]], and RFC2119 keywords.
// CONFIGURATION:
// These options do not configure the behaviour of this module per se, rather this module
// manipulates them (oftentimes being the only source to set them) so that other modules
// may rely on them.
// - normativeReferences: a map of normative reference identifiers.
// - informativeReferences: a map of informative reference identifiers.
// - respecRFC2119: a list of the number of times each RFC2119
// key word was used. NOTE: While each member is a counter, at this time
// the counter is not used.
define(
[],
function () {
return {
run: function (conf, doc, cb, msg) {
msg.pub("start", "core/inlines");
doc.normalize();
if (!conf.normativeReferences) conf.normativeReferences = {};
if (!conf.informativeReferences) conf.informativeReferences = {};
if (!conf.respecRFC2119) conf.respecRFC2119 = {};
// PRE-PROCESSING
var abbrMap = {}, acroMap = {};
$("abbr[title]", doc).each(function () { abbrMap[$(this).text()] = $(this).attr("title"); });
$("acronym[title]", doc).each(function () { acroMap[$(this).text()] = $(this).attr("title"); });
var aKeys = [];
for (var k in abbrMap) aKeys.push(k);
for (var k in acroMap) aKeys.push(k);
aKeys.sort(function (a, b) {
if (b.length < a.length) return -1;
if (a.length < b.length) return 1;
return 0;
});
var abbrRx = aKeys.length ? "(?:\\b" + aKeys.join("\\b)|(?:\\b") + "\\b)" : null;
// PROCESSING
var txts = $("body", doc).allTextNodes(["pre"]);
var rx = new RegExp("(\\bMUST(?:\\s+NOT)?\\b|\\bSHOULD(?:\\s+NOT)?\\b|\\bSHALL(?:\\s+NOT)?\\b|" +
"\\bMAY\\b|\\b(?:NOT\\s+)?REQUIRED\\b|\\b(?:NOT\\s+)?RECOMMENDED\\b|\\bOPTIONAL\\b|" +
"(?:\\[\\[(?:!|\\\\)?[A-Za-z0-9\\.-]+\\]\\])" + ( abbrRx ? "|" + abbrRx : "") + ")");
for (var i = 0; i < txts.length; i++) {
var txt = txts[i];
var subtxt = txt.data.split(rx);
if (subtxt.length === 1) continue;
var df = doc.createDocumentFragment();
while (subtxt.length) {
var t = subtxt.shift();
var matched = null;
if (subtxt.length) matched = subtxt.shift();
df.appendChild(doc.createTextNode(t));
if (matched) {
// RFC 2119
if (/MUST(?:\s+NOT)?|SHOULD(?:\s+NOT)?|SHALL(?:\s+NOT)?|MAY|(?:NOT\s+)?REQUIRED|(?:NOT\s+)?RECOMMENDED|OPTIONAL/.test(matched)) {
matched = matched.split(/\s+/).join(" ");
df.appendChild($("<em/>").attr({ "class": "rfc2119", title: matched }).text(matched)[0]);
// remember which ones were used
conf.respecRFC2119[matched] = true;
}
// BIBREF
else if (/^\[\[/.test(matched)) {
var ref = matched;
ref = ref.replace(/^\[\[/, "");
ref = ref.replace(/\]\]$/, "");
if (ref.indexOf("\\") === 0) {
df.appendChild(doc.createTextNode("[[" + ref.replace(/^\\/, "") + "]]"));
}
else {
var norm = false;
if (ref.indexOf("!") === 0) {
norm = true;
ref = ref.replace(/^!/, "");
}
// contrary to before, we always insert the link
if (norm) conf.normativeReferences[ref] = true;
else conf.informativeReferences[ref] = true;
df.appendChild(doc.createTextNode("["));
df.appendChild($("<cite/>").wrapInner($("<a/>").attr({"class": "bibref", href: "#bib-" + ref}).text(ref))[0]);
df.appendChild(doc.createTextNode("]"));
}
}
// ABBR
else if (abbrMap[matched]) {
if ($(txt).parents("abbr").length) df.appendChild(doc.createTextNode(matched));
else df.appendChild($("<abbr/>").attr({ title: abbrMap[matched] }).text(matched)[0]);
}
// ACRO
else if (acroMap[matched]) {
if ($(txt).parents("acronym").length) df.appendChild(doc.createTextNode(matched));
else df.appendChild($("<acronym/>").attr({ title: acroMap[matched] }).text(matched)[0]);
}
// FAIL -- not sure that this can really happen
else {
msg.pub("error", "Found token '" + matched + "' but it does not correspond to anything");
}
}
}
txt.parentNode.replaceChild(df, txt);
}
msg.pub("end", "core/inlines");
cb();
}
};
}
);