forked from unitedstates/citation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
citation.js
317 lines (237 loc) · 10.1 KB
/
citation.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* Citation.js - a legal citation extractor.
*
* Open source, dedicated to the public domain: https://github.com/unitedstates/citation
*
* Originally authored by Eric Mill (@konklone), at the Sunlight Foundation,
* many contributions by https://github.com/unitedstates/citation/graphs/contributors
*/
/*
TODO: move this out of the namespace, see #56
* rework how citators load Citation
* replace _.contains with indexOf (?)
* replace _.omit with ?
* replace _.extend with Object.extend ?
* replace _.find with ?
* replace _.flatten with ?
* replace _.compact with filter (null || undefined)
* replace _.intersection with ?
* replace _.isArray with ?
* move XRegExp into the closure
*/
if (typeof(require) !== "undefined") {
underscore = require("underscore");
XRegExp = require('xregexp').XRegExp;
}
(function(Citation) {
Citation = {
// will be filled in by individual citation types as available
types: {},
// filters that can pre-process text and post-process citations
filters: {},
// TODO: document this inline
// check a block of text for citations of a given type -
// return an array of matches, with citation broken out into fields
find: function(text, options) {
if (!options) options = {};
// client can apply a filter that pre-processes text before extraction,
// and post-processes citations after extraction
var results;
if (options.filter && Citation.filters[options.filter])
results = Citation.filtered(options.filter, text, options);
// otherwise, do a single pass over the whole text.
else
results = Citation.extract(text, options);
return results;
},
// return an array of matched and filter-mapped cites
filtered: function(name, text, options) {
var results = [];
var filter = Citation.filters[name];
// filter can break up the text into pieces with accompanying metadata
filter.from(text, options[name], function(piece, metadata) {
var response = Citation.extract(piece, options);
var filtered = response.citations.map(function(result) {
Object.keys(metadata).forEach(function(key) {
result[key] = metadata[key];
});
return result;
});
results = results.concat(filtered);
});
return {citations: results};
},
// return an array of matched cites
extract: function(text, options) {
if (!options) options = {};
// default: no excerpt
var excerpt = options.excerpt ? parseInt(options.excerpt, 10) : 0;
// whether to return parent citations
// default: false
var parents = options.parents || false;
// default: all types, can be filtered to one, or an array of them
var types = Citation.selectedTypes(options);
if (types.length === 0) return null;
// caller can provide optional context that can change what patterns individual citators apply
var context = options.context || {};
// The caller can provide a replace callback to alter every found citation.
// this function will be called with each (found and processed) cite object,
// and should return a string to be put in the cite's place.
//
// The resulting transformed string will be in the returned object as a 'text' field.
// this field will only be present if a replace callback was provided.
//
// providing this callback will also cause matched cites not to return the 'index' field,
// as the replace process will completely screw them up. only use the 'index' field if you
// plan on doing your own replacing.
var replace = options.replace;
// accumulate the results
var results = [];
///////////// prepare citation patterns /////////////
// figure out which patterns we're going apply, assign each an identifier
var citators = {};
// first, handle all regex-based citators
types.forEach(function(type) {
if (Citation.types[type].type != "regex") return;
var patterns = Citation.types[type].patterns;
// individual parsers can opt to make their parsing context-specific
if (typeof(patterns) == "function")
patterns = patterns(context[type] || {});
patterns.forEach(function(pattern, i) {
var name = type + "_" + i; // just needs to be unique
// small pre-process on each regex - prefix named captures to ensure uniqueness.
// will be un-prefixed before passing to processor.
var uniquified = pattern.regex
.replace(new RegExp("\\(\\?<([a-z0-9]+)>", "ig"), "(?<" + name + "_" + "$1>");
citators[name] = {
regex: uniquified,
processor: pattern.processor, // original processor method per-cite, expects named captures
type: type // store so we can figure out per-cite what we're talking about
};
});
});
// if there are any regex-based citators being applied, use them
var names = Object.keys(citators);
if (names.length > 0) {
// now let's merge each pattern's regex into a single regex, using named capture groups
var regex = names.map(function(name) {
return "(?<" + name + ">" + citators[name].regex + ")";
}).join("|");
regex = new XRegExp(regex, "ig");
var replaced = XRegExp.replace(text, regex, function() {
var match = arguments[0];
// establish which pattern matched - each pattern name must be unique (even among individual named groups)
var name = underscore.find(names, function(citeName) {if (match[citeName]) return true;});
var type = citators[name].type;
var processor = citators[name].processor;
// extract and de-prefix any captured groups from the individual citator's regex
var captures = Citation.capturesFrom(name, match);
// process the matched data into the final object
var cites = processor(captures);
if (!underscore.isArray(cites)) cites = [cites]; // one match can generate one or many citation results (e.g. ranges)
// put together the match-level information
var matchInfo = {type: type};
matchInfo.match = match.toString(); // match data can be converted to the plain string
var index = arguments[arguments.length - 2]; // offset is second-to-last argument
// store the matched character offset, except if we're replacing
if (!replace)
matchInfo.index = index;
// use index to grab surrounding excerpt
if (excerpt > 0) {
var proposedLeft = index - excerpt;
var left = proposedLeft > 0 ? proposedLeft : 0;
var proposedRight = index + matchInfo.match.length + excerpt;
var right = (proposedRight <= text.length) ? proposedRight : text.length;
matchInfo.excerpt = text.substring(left, right);
}
// if we want parent cites too, make those now
if (parents && Citation.types[type].parents_by) {
cites = underscore.flatten(cites.map(function(cite) {
return Citation.citeParents(cite, type);
}));
}
cites = cites.map(function(cite) {
var result = {};
// match-level info
underscore.extend(result, matchInfo);
// cite-level info, plus ID standardization
result[type] = cite;
underscore.extend(result[type], Citation.types[type].standardize(result[type]));
results.push(result);
return result;
});
// I don't know what to do about ranges yet - but for now, screw it
var replacedCite;
if (typeof(replace) === "function")
replacedCite = replace(cites[0]);
else if ((typeof(replace) === "object") && (typeof(replace[type]) === "function"))
replacedCite = replace[type](cites[0]);
if (replacedCite)
return replacedCite;
else
return matchInfo.match;
});
}
// TODO: do for any external cite types, not just "judicial"
if (underscore.contains(types, "judicial"))
results = results.concat(Citation.types.judicial.extract(text));
var response = {citations: underscore.compact(results)};
if (options.replace) response.text = replaced;
return response;
},
// for a given set of cite-specific details,
// return itself and its parent citations
citeParents: function(citation, type) {
var field = Citation.types[type].parents_by;
var results = [];
for (var i=citation[field].length; i >= 0; i--) {
var parent = underscore.clone(citation);
parent[field] = parent[field].slice(0, i);
results.push(parent);
}
return results;
},
// internal function - given a XRegExp match object, and a name prefix,
// return a new object with the de-prefixed captured values
capturesFrom: function(name, match) {
var captures = {};
Object.keys(match).forEach(function(key) {
if (key.indexOf(name + "_") === 0)
captures[key.replace(name + "_", "")] = match[key];
});
return captures;
},
selectedTypes: function(options) {
var types;
if (options.types) {
if (underscore.isArray(options.types)) {
if (options.types.length > 0)
types = options.types;
} else
types = [options.types];
}
// only allow valid types
if (types)
types = underscore.intersection(types, Object.keys(Citation.types));
else
types = Object.keys(Citation.types);
return types;
}
};
// TODO: load only the citation types asked for
if (typeof(require) !== "undefined") {
Citation.types.usc = require("./citations/usc");
Citation.types.law = require("./citations/law");
Citation.types.cfr = require("./citations/cfr");
Citation.types.va_code = require("./citations/va_code");
Citation.types.dc_code = require("./citations/dc_code");
Citation.types.dc_register = require("./citations/dc_register");
Citation.types.dc_law = require("./citations/dc_law");
Citation.types.stat = require("./citations/stat");
Citation.types.judicial = require("./citations/judicial");
Citation.filters.lines = require("./filters/lines");
}
if (typeof(window) !== "undefined")
window.Citation = Citation;
if (typeof(module) !== "undefined" && module.exports)
module.exports = Citation;
})();