-
Notifications
You must be signed in to change notification settings - Fork 42
/
citation.js
16864 lines (14021 loc) · 602 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
module.exports = {
type: "regex",
id: function(data) {
return ["cfr", data.title, (data.section || data.part)]
.concat(data.subsections || [])
.join("/")
},
patterns: [
// done:
// 14 CFR part 25
// 38 CFR Part 74.2
// 48 CFR § 9903.201
// 24 CFR 85.25(h)
// 5 CFR §531.610(f)
// 45 C.F.R. 3009.4
// 47 CFR 54.506 (c)
// but not: 47 CFR 54.506 (whatever)
// 5CFR, part 575
// maybe:
// 13 CFR Parts 125 and 134
// 5CFR, part 575, subpart C
// 23 CFR 650, Subpart A
{
regex:
"(\\d+)\\s?" + // Title number
"C\\.?\\s?F\\.?\\s?R\\.?" + // CFR
"(?:[\\s,]+(?:§+|parts?))?" + // Extra separators (section sign, part)
"\\s*(\\d+(?:(?:[-–—]\\d+)?[a-z]?" + // Part number
"(?:\\.(?:13h[-–—]l|\\d+[-–—]?\\d*\\.5\\d|(?:\\d+T|T|\\d+[-–—]DD[-–—]|\\d+[-–—]WH[-–—]|\\d+[a-z]{1,2}\\d*[-–—])?\\d+)[a-z]{0,2}(?:(?:(?:\\([a-z\\d]{1,2}\\))*[-–—]\\d+)+[a-z]{0,2})?)?" + // Optionally: period and section number
"(?:(?:\\s*\\((?:[a-z\\d]{1,2}|[ixv]+)\\))+)?)?)", // Optionally: subsections, if there was a section number
fields: ['title', 'sections'],
processor: function(captures) {
var title = captures.title;
var part, section, subsections;
// convert all dashes to hyphens, deduplicate hyphens, and look for
// subsections starting after the last hyphen
var hyphen_split = captures.sections.split(/[-–—]+/);
var head, tail;
if (hyphen_split.length > 1) {
head = hyphen_split.slice(0, -1).join("-") + "-";
tail = hyphen_split[hyphen_split.length - 1];
} else {
head = "";
tail = hyphen_split[0];
}
// separate subsections for each section being considered
var paren_split = tail.split(/[\(\)]+/).filter(function(x) {return x;});
section = head + paren_split[0].trim();
subsections = paren_split.splice(1);
if (section.indexOf(".") > 0)
part = section.split(".")[0];
else {
part = section;
section = null;
subsections = null; // don't include empty array
}
return {
title: title,
part: part,
section: section,
subsections: subsections
};
}
}
// todo:
// parts 121 and 135 of Title 14 of the Code of Federal Regulations
// {
// regex:
// "section (\\d+[\\w\\d\-]*)((?:\\([^\\)]+\\))*)" +
// "(?:\\s+of|\\,) title (\\d+)",
// fields: ['section', 'subsections', 'title'],
// processor: function(captures) {
// return {
// title: captures.title,
// section: captures.section,
// subsections: captures.subsections.split(/[\(\)]+/).filter(function(x) {return x;})
// };
// }
// }
]
};
},{}],2:[function(require,module,exports){
var base_regex =
"(\\d+A?)" + // title
"\\s?\\-\\s?" + // dash
"([\\w\\d]+(?:\\.?[\\w\\d]+)?)" + // section identifier (letters/numbers/dots)
"((?:\\([^\\)]+\\))*)"; // subsection (any number of adjacent parenthesized subsections)
module.exports = {
type: "regex",
// normalize all cites to an ID, with and without subsections
id: function(cite) {
return ["dc-code", cite.title, cite.section]
.concat(cite.subsections)
.join("/");
},
// field to calculate parents from
parents_by: "subsections",
patterns: function(context) {
// D.C. Official Code 3-1202.04
// D.C. Official Code § 3-1201.01
// D.C. Official Code §§ 38-2602(b)(11)
// D.C. Official Code § 3- 1201.01
// D.C. Official Code § 3 -1201.01
//
// § 32-701
// § 32-701(4)
// § 3-101.01
// § 1-603.01(13)
// § 1- 1163.33
// § 1 -1163.33
// section 16-2326.01
var prefix_regex = "";
var section_regex = "(?:sections?\\s+|§+\\s*)";
var sections_regex = "(?:sections\\s+|§§\\s*)";
if (context.source != "dc_code") {
// Require "DC Official Code" but then make the section symbol optional.
prefix_regex = "D\\.?C\\.? (?:Official )?Code\\s+";
section_regex = "(?:" + section_regex + ")?";
sections_regex = "(?:" + sections_regex + ")?";
}
return [
// multiple citations
// has precedence over a single citation
// Unlike the single citation, the matched parts are just the title/section/subsection
// and omits "DC Code" and the section symbols (if present) from the matched text.
{
regex: "(" + prefix_regex + sections_regex + ")(" + base_regex + "(?:(?:,|, and|\\s+and|\\s+through|\\s+to)\\s+" + base_regex + ")+)",
fields: ["prefix", "multicite", "title1", "section1", "subsections1", "title2", "section2", "subsections2"],
processor: function(captures) {
var rx = new RegExp(base_regex, "g");
var matches = new Array();
var match;
while((match = rx.exec(captures.multicite)) !== null) {
matches.push({
_submatch: {
text: match[0],
offset: captures.prefix.length + match.index,
},
title: match[1],
section: match[2],
subsections: split_subsections(match[3])
});
}
return matches;
}
},
// a single citation
{
regex: prefix_regex + section_regex + base_regex,
fields: ["title", "section", "subsections"],
processor: function(captures) {
var title = captures.title;
var section = captures.section;
var subsections = split_subsections(captures.subsections);
return {
title: title,
section: section,
subsections: subsections
};
}
}
];
}
};
function split_subsections(match) {
if (match)
return match.split(/[\(\)]+/).filter(function(x) {return x});
else
return [];
}
},{}],3:[function(require,module,exports){
module.exports = {
type: "regex",
id: function(cite) {
return ["dc-law", cite.period, cite.number].join("/");
},
patterns: function(context) {
// If the context for this citation is the DC Code, then Law XX-YYY can be assumed
// to be a DC law. In other context, require the "DC Law" prefix. In the DC Code
// context also slurp in the "DC" prefix.
var context_regex = "D\\.?\\s*C\\.?\\s+";
if (context.source == "dc_code")
context_regex = "(?:" + context_regex + ")?"
return [
// "D.C. Law 20-17"
// "DC Law 20-17"
// "DC Law 18-135A"
{
regex:
context_regex + "Law\\s+(\\d+)\\s?[-–]+\\s?(\\d+\\w?)",
fields: ["period", "number"],
processor: function(captures) {
return {
period: captures.period,
number: captures.number
};
}
}
];
}
};
},{}],4:[function(require,module,exports){
module.exports = {
type: "regex",
id: function(cite) {
return ["dc-register", cite.volume, cite.page].join("/");
},
patterns: [
// 54 DCR 8014
{
regex:
"(\\d+)\\s+" +
"DCR" +
"\\s+(\\d+)",
fields: ['volume', 'page'],
processor: function(match) {
return {
volume: match.volume,
page: match.page,
};
}
}
]
};
},{}],5:[function(require,module,exports){
module.exports = {
type: "regex",
// normalize all cites to an ID
id: function(cite) {
return ["dcstat", cite.volume, cite.page].join("/")
},
patterns: [
// "20 DCSTAT 1952"
{
regex:
"(\\d+)\\s+" +
"DCSTAT" +
"\\s+(\\d+)",
fields: ['volume', 'page'],
processor: function(match) {
return {
volume: match.volume,
page: match.page,
};
}
}
]
};
},{}],6:[function(require,module,exports){
module.exports = {
type: "regex",
// normalize all cites to an ID
id: function(cite) {
return ["fedreg", cite.volume, cite.page].join("/")
},
patterns: [
// "75 Fed. Reg. 28404"
// "69 FR 22135"
{
regex:
"(\\d+)\\s+" +
"(?:Fed\\.?\\sReg?\\.?|F\\.?R\\.?)" +
"\\s+(\\d+)",
fields: ['volume', 'page'],
processor: function(match) {
return {
volume: match.volume,
page: match.page,
};
}
}
]
};
},{}],7:[function(require,module,exports){
module.exports = {
type: "regex",
id: function(cite) {
return ["us-law", cite.type, cite.congress, cite.number]
.concat(cite.sections || [])
.join("/");
},
canonical: function(cite) {
if (!cite.sections || cite.sections.length == 0)
// this style matches GPO at http://www.gpo.gov/fdsys/browse/collection.action?collectionCode=PLAW&browsePath=112&isCollapsed=false&leafLevelBrowse=false&ycord=0
return (cite.type == "public" ? "Pub. L." : "Pvt. L.") + " " + cite.congress + "-" + cite.number;
else
return "Section " + cite.sections[0] + cite.sections.slice(1).map(function(item) { return "(" + item + ")" }).join("")
+ " of " +
(cite.type == "public" ? "Public" : "Private") + " Law " + cite.congress + "-" + cite.number;
},
// field to calculate parents from
parents_by: "sections",
patterns: [
// "Public Law 111-89"
// "Pub. L. 112-56"
// "Pub. L. No. 110-2"
// "Pub.L. 105-33"
// "Private Law 111-72"
// "Priv. L. No. 98-23"
// "section 552 of Public Law 111-89"
// "section 4402(e)(1) of Public Law 110-2"
{
regex:
"(?:section (\\d+[\\w\\d\-]*)((?:\\([^\\)]+\\))*) of )?" +
"(pub(?:lic)?|priv(?:ate)?)\\.?\\s*l(?:aw)?\\.?(?:\\s*No\\.?)?" +
" +(\\d+)[-–]+(\\d+)",
fields: ['section', 'subsections', 'type', 'congress', 'number'],
processor: function(captures) {
var sections = [];
if (captures.section) sections.push(captures.section);
if (captures.subsections) sections = sections.concat(captures.subsections.split(/[\(\)]+/).filter(function(x) {return x}));
return {
type: captures.type.match(/^priv/i) ? "private" : "public",
congress: captures.congress,
number: captures.number,
sections: sections
};
}
},
// "PL 19-4"
// "P.L. 45-78"
// "section 552 of PL 19-4"
// "section 4402(e)(1) of PL 19-4"
{
regex:
"(?:section (\\d+[\\w\\d\-]*)((?:\\([^\\)]+\\))*) of )?" +
"P\\.?L\\.? +(\\d+)[-–](\\d+)",
fields: ['section', 'subsections', 'congress', 'number'],
processor: function(captures) {
sections = [];
if (captures.section) sections.push(captures.section);
if (captures.subsections) sections = sections.concat(captures.subsections.split(/[\(\)]+/).filter(function(x) {return x}));
return {
type: "public",
congress: captures.congress,
number: captures.number,
sections: sections
};
}
}
]
};
},{}],8:[function(require,module,exports){
module.exports = {
type: "regex",
// normalize all cites to an ID
id: function(cite) {
return ["reporter", cite.volume, cite.reporter, cite.page].join("/")
},
canonical: function(cite) {
return cite.volume + " " + cite.reporter + " " + cite.page;
},
patterns: [
{
regex:
"\\b(\\d{1,3})\\s" +
"([AFSNU]\\.\\s?[\\w\\.]+)\\s" +
"(\\d{1,4}|_{1,4})\\b",
fields: ['volume', 'reporter', 'page'],
processor: function(match) {
return {
volume: match.volume,
reporter: match.reporter,
page: match.page.indexOf('_') === -1 ? match.page : 'blank',
};
}
}
]
};
},{}],9:[function(require,module,exports){
module.exports = {
type: "regex",
// normalize all cites to an ID
id: function(cite) {
return ["stat", cite.volume, cite.page].join("/")
},
canonical: function(cite) {
return cite.volume + " Stat. " + cite.page;
},
patterns: [
// "117 Stat. 1952"
// "77 STAT. 77"
{
regex:
"(\\d+[\\w]*)\\s+" +
"Stat\\.?" +
"\\s+(\\d+)",
fields: ['volume', 'page'],
processor: function(match) {
return {
volume: match.volume,
page: match.page,
};
}
}
]
};
},{}],10:[function(require,module,exports){
module.exports = {
type: "regex",
id: function(cite) {
return ["usc", cite.title, cite.section]
.concat(cite.subsections || [])
.join("/");
},
canonical: function(cite) {
// title, which also may specify it is an appendix title
var title = cite.title;
var app = "";
var title_without_app = cite.title.replace(/-app$/, '');
if (title != title_without_app) app = "App. ";
// subsections, possibly with a note/et-seq as a leaf which should
// be rendered differently from a normal subsection item
var subsections = cite.subsections.slice(); // clone
var suffix = "";
var leaf = subsections.length > 0 ? subsections[subsections.length-1] : null;
if (leaf == "note") {
subsections.pop();
suffix = " note"
} else if (leaf == "et-seq") {
subsections.pop();
suffix = " et seq"
}
return title_without_app + " U.S.C. " + app + cite.section
+ subsections.map(function(item) { return "(" + item + ")" }).join("")
+ suffix;
},
// field to calculate parents from
parents_by: "subsections",
patterns: [
// "5 USC 552"
// "5 U.S.C. § 552(a)(1)(E)"
// "7 U.S.C. 612c note"
// "29 U.S.C. 1081 et seq"
// "50 U.S.C. App. 595"
// "45 U.S.C. 10a-10c"
// "50 U.S.C. 404o-1(a)" - single section
// "45 U.S.C. 10a(1)-10c(2)" - range
// "50 U.S.C. App. §§ 451--473" - range
{
regex:
"(\\d+)\\s+" + // title
"U\\.?\\s?S\\.?\\s?C\\.?" +
"(?:\\s+(App)\.?)?\\s*" + // appendix
"(?:(§+)\\s*)?" + // symbol
"((?:[-–—]*\\d+[\\w\\d\\-–—]*(?:\\([^\\)]+\\))*)+)" + // sections
"(?:\\s+(note|et\\s+seq))?", // note
fields: [
'title', 'appendix',
'symbol', 'sections', 'note'
],
processor: function(match) {
// a few titles have distinct appendixes
var title = match.title;
if (match.appendix) title += "-app";
var sections = match.sections.split(/[-–—]+/);
var match_sections_normalized = match.sections.replace(/[–—]/g, '-');
var range = false;
// two section symbols is unambiguous
if (match.symbol == "§§") // 2 section symbols
range = true;
// paren before dash is unambiguous
else {
var dash = match_sections_normalized.indexOf("-");
var paren = match_sections_normalized.indexOf("(");
if (dash > 0 && paren > 0 && paren < dash)
range = true;
}
// if there's a hyphen and the range is ambiguous,
// also return the original section string as one
if ((sections.length > 1) && !range)
sections.unshift(match_sections_normalized);
return sections.map(function(section) {
// separate subsections for each section being considered
var split = section.split(/[\(\)]+/).filter(function(x) {return x});
section = split[0];
subsections = split.splice(1);
if (match.note)
subsections.push(match.note.replace(" ", "-")); // "note" or "et seq"
return {
title: title,
section: section,
subsections: subsections
};
});
}
},
// "section 552 of title 5"
// "section 552, title 5"
// "section 552(a)(1)(E) of title 5"
// "section 404o-1(a) of title 50"
{
regex:
"section (\\d+[\\w\\d\\-–—]*)((?:\\([^\\)]+\\))*)" +
"(?:\\s+of|\\,) title (\\d+)",
fields: ['section', 'subsections', 'title'],
processor: function(match) {
return {
title: match.title,
section: match.section.replace(/[–—]/g, '-'),
subsections: match.subsections.split(/[\(\)]+/).filter(function(x) {return x})
};
}
},
// "Section 14123(a)(2) of 49 U.S.C."
// "Section 14123(a)(2), 49 U.S.C."
{
regex:
"section (\\d+[\\w\\d\\-–—]*)((?:\\([^\\)]+\\))*)" +
"(?:\\s+of|\\,) (\\d+) " +
"U\\.?\\s?S\\.?\\s?C\\.?",
fields: ['section', 'subsections', 'title'],
processor: function(match) {
return {
title: match.title,
section: match.section.replace(/[–—]/g, '-'),
subsections: match.subsections.split(/[\(\)]+/).filter(function(x) {return x})
};
}
}
]
};
},{}],11:[function(require,module,exports){
/* Parses citations to the United States Constitution
*
* like: U.S. CONST., art. I, ¶ 8, cl. 17
* as seen in http://pdfserver.amlaw.com/nlj/3-18-16%20dc%20council%20v%20mayor%20order%20NLJ.pdf
*/
var arabic_number = parseInt;
var roman_numeral = require('nomar');
// All of the sub-parts that might be found in the citation.
var part_types = {
amendment: { abbrev: "Amdt.", regex: "Amdt\\.?|Amend\\.?", numbering: roman_numeral },
article: { abbrev: "art.", regex: "art\\.?", numbering: roman_numeral },
section: { abbrev: "§", regex: "§", numbering: arabic_number },
paragraph: { abbrev: "¶", regex: "¶", numbering: arabic_number },
clause: { abbrev: "cl.", regex: "cl\\.?", numbering: arabic_number },
};
module.exports = {
type: "regex",
// normalize all cites to an ID
id: function(cite) {
return ["usconst"].concat((cite.part || []).map(function(part) {
if (!part) return "?";
return part.type + "-" + part.number;
})).join("/");
},
canonical: function(cite) {
var ret = "U.S. Const.";
for (var i = 0; i < (cite.part || []).length; i++)
if (cite.part[i]) // did this part parse?
ret += ", " + part_types[cite.part[i].type].abbrev + " " + cite.part[i].number_str;
return ret;
},
patterns: [
// "U.S. CONST., art. I, ¶ 8, cl. 17"
{
regex:
"U\\.? ?S\\.? ?C(?:ONST|onst)\\.?" +
"((:?,? ?" +
"(" +
Object.keys(part_types).map(function(type) { return part_types[type].regex; }).join("|") +
") ?([IVX0-9]+)" +
")*)",
fields: ['part'],
processor: function(match) {
var part = match.part;
if (part) {
// Split the comma-separated list of parts into the Constitution.
part = part.split(/, ?/);
if (part[0].length == 0)
part.shift();
part = part.map(process_part);
}
return {
part: part,
};
}
}
]
};
function process_part(part) {
for (var part_type in part_types) {
var match = new RegExp("(?:" + part_types[part_type].regex + ") ?([IVX0-9]+)" + "$" , 'i').exec(part);
if (match) {
return {
type: part_type,
number_str: match[1],
number: part_types[part_type].numbering(match[1])
};
}
}
return null; // somehow didn't match
}
},{"nomar":33}],12:[function(require,module,exports){
module.exports = {
type: "regex",
id: function(data) {
return ["va-code", data.title, data.section].join("/");
},
patterns: [
// Va. Code Ann. § 19.2-56.2 (2010)
// Va. Code Ann. § 19.2-56.2 (West 2010)
// Va. Code Ann. § 57-1
// Va. Code Ann. § 57-2.02
// Va. Code Ann. § 63.2-300
// Va. Code Ann. § 66-25.1:1
// Va. Code § 66-25.1:1
// VA Code § 66-25.1:1
{
regex:
"Va\\.? Code\\.?" +
"(?:\\s+Ann\\.?)?\\s+" +
"(?:§+\\s*)?" +
"([\\d\\.]+)\\-([\\d\\.:]+)" +
"(?:\\s+\\((?:West )?([12]\\d{3})\\))?",
fields: ['title', 'section', 'year'],
processor: function (captures) {
return {
title: captures.title,
section: captures.section,
year: captures.year
};
}
}
]
};
},{}],13:[function(require,module,exports){
/* 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: {},
// link sources that add permalink information to citations
links: {},
// 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 (if we're replacing we need it to handle
// some multiple citations, but the index will be useless to the caller after
// the replacement) so we wipe it out later.
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);
// handle _submatch, which lets the user-level citator override the
// match and index with a sub-part of the whole matched regex
if (cite._submatch) {
result.match = cite._submatch.text;
result.index += cite._submatch.offset;
delete cite._submatch;
}
// since a single text region can match multiple citations, such as when
// a range is given, clarify what this match represents
if ('canonical' in Citation.types[type])
result.citation = Citation.types[type].canonical(cite);
// cite-level info, plus ID standardization
result[type] = cite;
result[type].id = Citation.types[type].id(cite);
// add permalinks if requested and a link source exists for this citation
// type.
if (options.links)
result[type].links = Citation.getLinksForCitation(type, cite);
results.push(result);
return result;
});
// If a replace function is given, replace each matched citation by the
// result of calling the replace function with the citation passed as its
// only argument.
//
// Most citators return only a single citation match per regex match, but
// some return multiple citations for strings like "§§ 32-701 through 32-703".
// Collect the final match string here.
var finalstring = matchInfo.match;
// Get the replace function. If options.replace is a function use that,
// or if it is an object mapping the citator type to a function use that.
var replace_func = null;
if (typeof(replace) === "function")
replace_func = replace;
else if ((typeof(replace) === "object") && (typeof(replace[type]) === "function"))
replace_func = replace[type];
else
replace_func = null;
// If there's a replacement function...
if (replace_func) {
// Process the citations in the order they are returned. Assume they are
// ordered from left to right.
var last_index = 0;
var dx = 0;
for (var i = 0; i < cites.length; i++) {
// Skip citations that overlap with the previous citation (e.g. there
// may be two citations for the same text range.)
if (cites[i].index >= last_index) {
// Execute the replacement function. If the return is truth-y, perform
// a replacement.
var replacement = replace_func(cites[i]);
if (replacement) {
// Replace the substring.
finalstring = finalstring.substring(0, cites[i].index-index+dx) + replacement + finalstring.substring(cites[i].index-index+cites[i].match.length+dx);
// The replacement text may have a different length than the text
// being replaced. Keep track of the total change in string length
// as we go because we have to adjust future citation replacements's
// indexes so that we make the edit to finalstring in the right place.
dx += replacement.length - cites[i].match.length;
// And track the end of last citation so we can skip any future citations
// that overlap with this text range.
last_index = cites[i].index + cites[i].match.length;
}
}
// Per the citation API, delete the index field when doing a replacement.
// After replacements, the index will no longer be useful to the caller
// because the string has been edited.
delete cites[i].index;
}
}
return finalstring;
});