-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
biblio.js
207 lines (201 loc) · 8.7 KB
/
biblio.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Module core/biblio
// Handles bibliographic references
// Configuration:
// - localBiblio: override or supplement the official biblio with your own.
/*jshint jquery: true*/
/*globals console*/
"use strict";
define(
[],
function () {
var getRefKeys = function (conf) {
var informs = conf.informativeReferences
, norms = conf.normativeReferences
, del = []
, getKeys = function (obj) {
var res = [];
for (var k in obj) res.push(k);
return res;
}
;
for (var k in informs) if (norms[k]) del.push(k);
for (var i = 0; i < del.length; i++) delete informs[del[i]];
return {
informativeReferences: getKeys(informs),
normativeReferences: getKeys(norms)
};
};
var REF_STATUSES = {
"NOTE": "W3C Note"
, "WG-NOTE": "W3C Working Group Note"
, "ED": "W3C Editor's Draft"
, "FPWD": "W3C First Public Working Draft"
, "WD": "W3C Working Draft"
, "LCWD": "W3C Last Call Working Draft"
, "CR": "W3C Candidate Recommendation"
, "PR": "W3C Proposed Recommendation"
, "PER": "W3C Proposed Edited Recommendation"
, "REC": "W3C Recommendation"
};
var stringifyRef = function(ref) {
if (typeof ref === "string") return ref;
var output = "";
if (ref.authors && ref.authors.length) {
output += ref.authors.join("; ");
if (ref.etAl) output += " et al";
output += ". ";
}
if(ref.publisher){
output += ref.publisher;
if(/\.$/.test(ref.publisher)){
output += " ";
}else{
output += ". ";
}
}
if (ref.href) output += '<a href="' + ref.href + '"><cite>' + ref.title + "</cite></a>. ";
else output += '<cite>' + ref.title + '</cite>. ';
if (ref.date) output += ref.date + ". ";
if (ref.status) output += (REF_STATUSES[ref.status] || ref.status) + ". ";
if (ref.href) output += 'URL: <a href="' + ref.href + '">' + ref.href + "</a>";
return output;
};
var bibref = function (conf, msg) {
// this is in fact the bibref processing portion
var badrefs = {}
, refs = getRefKeys(conf)
, informs = refs.informativeReferences
, norms = refs.normativeReferences
, aliases = {}
;
if (!informs.length && !norms.length && !conf.refNote) return;
var $refsec = $("<section id='references' class='appendix'><h2>References</h2></section>").appendTo($("body"));
if (conf.refNote) $("<p></p>").html(conf.refNote).appendTo($refsec);
var types = ["Normative", "Informative"];
for (var i = 0; i < types.length; i++) {
var type = types[i]
, refs = (type == "Normative") ? norms : informs;
if (!refs.length) continue;
var $sec = $("<section><h3></h3></section>")
.appendTo($refsec)
.find("h3")
.text(type + " references")
.end()
;
$sec.makeID(null, type + " references");
refs.sort();
var $dl = $("<dl class='bibliography'></dl>").appendTo($sec);
if (conf.doRDFa) $dl.attr("resource", "");
for (var j = 0; j < refs.length; j++) {
var ref = refs[j];
$("<dt></dt>")
.attr({ id:"bib-" + ref })
.text("[" + ref + "]")
.appendTo($dl)
;
var $dd = $("<dd></dd>").appendTo($dl);
var refcontent = conf.biblio[ref]
, circular = {}
, key = ref;
circular[ref] = true;
while (refcontent && refcontent.aliasOf) {
if (circular[refcontent.aliasOf]) {
refcontent = null;
msg.pub("error", "Circular reference in biblio DB between [" + ref + "] and [" + key + "].");
}
else {
key = refcontent.aliasOf;
refcontent = conf.biblio[key];
circular[key] = true;
}
}
aliases[key] = aliases[key] || [];
if (aliases[key].indexOf(ref) < 0) aliases[key].push(ref);
if (refcontent) {
$dd.html(stringifyRef(refcontent) + "\n");
if (conf.doRDFa) {
var $a = $dd.children("a");
$a.attr("property", type === "Normative" ? "dc:requires" : "dc:references");
}
}
else {
if (!badrefs[ref]) badrefs[ref] = 0;
badrefs[ref]++;
$dd.html("<em style='color: #f00'>Reference not found.</em>\n");
}
}
}
for (var k in aliases) {
if (aliases[k].length > 1) {
msg.pub("warn", "[" + k + "] is referenced in " + aliases[k].length + " ways (" + aliases[k].join(", ") + "). This causes duplicate entries in the reference section.");
}
}
for (var item in badrefs) {
if (badrefs.hasOwnProperty(item)) msg.pub("error", "Bad reference: [" + item + "] (appears " + badrefs[item] + " times)");
}
};
return {
stringifyRef: stringifyRef,
run: function (conf, doc, cb, msg) {
function finish() {
msg.pub("end", "core/biblio");
cb();
}
msg.pub("start", "core/biblio");
if(!conf.localBiblio){
conf.localBiblio = {};
}
if(conf.biblio){
var warn = "Overriding `.biblio` in config. Please use `.localBiblio` for custom biblio entries.";
msg.pub("warn", warn);
}
conf.biblio = {};
var localAliases = Array
.from(Object.keys(conf.localBiblio))
.filter(function(key){
return conf.localBiblio[key].hasOwnProperty("aliasOf");
})
.map(function(key){
return conf.localBiblio[key].aliasOf;
});
var allRefs = getRefKeys(conf);
var externalRefs = allRefs.normativeReferences
.concat(allRefs.informativeReferences)
// Filter, as to not go to network for local refs
.filter(function(key){
return !conf.localBiblio.hasOwnProperty(key);
})
// but include local aliases, in case they refer to external specs
.concat(localAliases)
// remove duplicates
.reduce(function(collector, item){
if(!collector.includes(item)){
collector.push(item);
}
return collector;
}, [])
.sort();
// If we don't need to go to network, just use internal biblio
if (!externalRefs.length) {
Object.assign(conf.biblio, conf.localBiblio);
bibref(conf, msg);
finish();
return;
}
var url = "https://labs.w3.org/specrefs/bibrefs?refs=" + externalRefs.join(",");
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(data) {
Object.assign(conf.biblio, data, conf.localBiblio);
bibref(conf, msg);
})
.catch(function(err) {
console.error(err);
})
.then(finish);
}
};
}
);