forked from unitedstates/citation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcitation.js
324 lines (250 loc) · 10.3 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
318
319
320
321
322
323
324
/* 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
*/
module.exports = (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 = {};
if (typeof(text) !== "string") return;
// 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])
return Citation.filtered(options.filter, text, options);
// otherwise, do a single pass over the whole text.
else
return Citation.extract(text, options);
},
// 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);
// ignores any replaced text, it falls off the edge of the earth
var filtered = response.citations.map(function(result) {
Object.keys(metadata).forEach(function(key) {
result[key] = metadata[key];
});
return result;
});
results = results.concat(filtered);
});
// doesn't return replaced text
return {citations: results};
},
// run the citators over the text, 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;
// 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 = [];
// will hold the calculated context-specific patterns we are to run
// over the given text, tracked by index we expect to find them at.
// nextIndex tracks a running index as we loop through patterns.
// (citators could just be called indexedPatterns)
var citators = {};
var nextIndex = 0;
// Go through every regex-based citator and prepare a set of patterns,
// indexed by the order of a matched arguments array.
types.forEach(function(type) {
if (Citation.types[type].type != "regex") return;
// Calculate the patterns this citator will contribute to the parse.
// (individual parsers can opt to make their parsing context-specific)
var patterns = Citation.types[type].patterns;
if (typeof(patterns) == "function")
patterns = patterns(options[type] || {});
// add each pattern, keeping a running tally of what we would
// expect its primary index to be when found in the master regex.
patterns.forEach(function(pattern) {
pattern.type = type; // will be needed later
citators[nextIndex] = pattern;
nextIndex += pattern.fields.length + 1;
});
});
// If there are any regex-based patterns being applied, combine them
// and run a find/replace over the string.
var regexes = Object.keys(citators).map(function(key) {return citators[key].regex});
if (regexes.length > 0) {
// merge all regexes into one, so that each pattern will begin at a predictable place
var regex = new RegExp("(" + regexes.join(")|(") + ")", "ig");
var replaced = text.replace(regex, function() {
var match = arguments[0];
// offset is second-to-last argument
var index = arguments[arguments.length - 2];
// pull out just the regex-captured matches
var captures = Array.prototype.slice.call(arguments, 1, -2);
// find the first matched index in the captures
var matchIndex;
for (matchIndex=0; matchIndex<captures.length; matchIndex++)
if (captures[matchIndex]) break;
// look up the citator by the index we expected it at
var citator = citators[matchIndex];
if (!citator) return null; // what?
var type = citator.type;
// process the matched data into the final object
var ourCaptures = Array.prototype.slice.call(captures, matchIndex + 1);
var namedMatch = Citation.matchFor(ourCaptures, citator);
var cites = citator.processor(namedMatch);
// one match can generate one or many citation results (e.g. ranges)
if (!Array.isArray(cites)) cites = [cites];
// put together the match-level information
var matchInfo = {type: citator.type};
matchInfo.match = match.toString(); // match data can be converted to the plain string
// 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 = Citation._.flatten(cites.map(function(cite) {
return Citation.citeParents(cite, type);
}));
}
cites = cites.map(function(cite) {
var result = {};
// match-level info
Citation._.extend(result, matchInfo);
// cite-level info, plus ID standardization
result[type] = cite;
result[type].id = Citation.types[type].id(cite);
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 (types.indexOf("judicial") != -1)
results = results.concat(Citation.types.judicial.extract(text));
var response = {citations: 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 = Citation._.extend({}, citation);
parent[field] = parent[field].slice(0, i);
results.push(parent);
}
return results;
},
// given an array of captures *beginning* with values the pattern
// knows how to process, turn it into an object with those keys.
matchFor: function(captures, pattern) {
var match = {};
for (var i=0; i<captures.length; i++)
match[pattern.fields[i]] = captures[i];
return match;
},
selectedTypes: function(options) {
var types;
if (options.types) {
if (Array.isArray(options.types)) {
if (options.types.length > 0)
types = options.types;
} else
types = [options.types];
}
// only allow valid types
if (types) {
types = types.filter(function(type) {
return Object.keys(Citation.types).indexOf(type) != -1;
});
} else
types = Object.keys(Citation.types);
return types;
},
// small replacement for several functions previously served by
// the `underscore` library.
_: {
extend: function(obj) {
Array.prototype.slice.call(arguments, 1).forEach(function(source) {
if (source) {
for (var prop in source)
obj[prop] = source[prop];
}
});
return obj;
},
flatten: function(array) {
var impl = function(input, output) {
input.forEach(function(value) {
if (Array.isArray(value))
impl(value, output);
else
output.push(value);
});
return output;
}
return impl(array, []);
}
}
};
// 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.filters.lines = require("./filters/lines");
}
// auto-load in-browser
if (typeof(window) !== "undefined")
window.Citation = Citation;
return Citation;
})();