Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for an opt-in 'parents' option that returns additional …

…citations for the parent of any matched subsection
  • Loading branch information...
commit 427dd748a697d8ef9158f9ef21839b6e5b022c44 1 parent e9762ca
@konklone konklone authored
View
2  bin/cite
@@ -15,6 +15,7 @@ var args = optimist
.describe('input', 'Path to file to read for input.')
.describe('output', 'Path to file to write output.')
.describe('pretty', 'Prettify JSON output.')
+ .describe('parents', 'Include additional citations for parent sections.')
.describe('types', 'Limit citation types to a comma-separated list (e.g. "usc,law")')
.describe('excerpt', 'Number of characters of excerpt surrounding matches to include.')
.describe('help', 'Show this message.')
@@ -78,6 +79,7 @@ function cite(text) {
var options = {};
if (args.excerpt) options.excerpt = args.excerpt;
if (args.types) options.types = args.types.split(",");
+ if (args.parents) options.parents = true;
output(require('../citation').find(text, options));
}
View
29 citation.js
@@ -23,6 +23,10 @@ if (typeof(_) === "undefined" && typeof(require) !== "undefined")
// default: no excerpt
var excerpt = options.excerpt || 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;
if (options.types) {
@@ -38,7 +42,7 @@ if (typeof(_) === "undefined" && typeof(require) !== "undefined")
types = _.intersection(types, _.keys(Citation.types))
else
types = _.keys(Citation.types)
-
+
// run through every pattern, accumulate matches
var results = _.map(types, function(type) {
@@ -72,9 +76,16 @@ if (typeof(_) === "undefined" && typeof(require) !== "undefined")
}
// one match can generate one or many citation results (e.g. ranges)
- cites = processor(match);
+ cites = processor(match, {parents: parents});
if (!_.isArray(cites)) cites = [cites];
+ // if we want parent cites too, make those now
+ if (parents) {
+ cites = _.flatten(_.map(cites, function(cite) {
+ return Citation.citeParents(cite, type);
+ }));
+ }
+
_.each(cites, function(cite) {
var result = {};
@@ -95,6 +106,20 @@ if (typeof(_) === "undefined" && typeof(require) !== "undefined")
// flatten it all and remove nulls
return _.compact(_.flatten(results));
+ },
+
+ // 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 = _.clone(citation);
+ parent[field] = parent[field].slice(0, i);
+ results.push(parent);
+ }
+ return results;
}
}
View
3  citations/law.js
@@ -10,6 +10,9 @@ Citation.types.law = {
}
},
+ // field to calculate parents from
+ parents_by: "sections",
+
patterns: [
// "Public Law 111-89"
// "Pub. L. 112-56"
View
15 citations/usc.js
@@ -10,6 +10,9 @@ Citation.types.usc = {
}
},
+ // field to calculate parents from
+ parents_by: "subsections",
+
patterns: [
// "5 U.S.C. 552"
// "5 U.S.C. § 552(a)(1)(E)"
@@ -35,12 +38,16 @@ Citation.types.usc = {
var sections = match[4].split(/-+/);
var range = false;
+
+ // two section symbols is unambiguous
if (match[3] == "§§") // 2 section symbols
range = true;
- else {
+
+ // paren before dash is unambiguous
+ else {
var dash = match[4].indexOf("-");
var paren = match[4].indexOf("(");
- if (dash > 0 && paren > 0 && paren < dash) // paren before dash
+ if (dash > 0 && paren > 0 && paren < dash)
range = true;
}
@@ -60,7 +67,7 @@ Citation.types.usc = {
title: title,
section: section,
subsections: subsections
- }
+ };
});
}
},
@@ -78,7 +85,7 @@ Citation.types.usc = {
title: match[3],
section: match[1],
subsections: _.compact(match[2].split(/[\(\)]+/))
- }
+ };
}
}
]
View
27 test/law.js
@@ -106,4 +106,29 @@ exports.testSubsections = function(test) {
}
test.done();
-};
+};
+
+// can opt-in to asking for all parents of a subsection to be returned too
+exports.testParents = function(test) {
+ test.expect();
+ var text, found, citation;
+
+ text = "section 4402(e)(1) of Public Law 110-2";
+
+ found = Citation.find(text, {parents: false});
+ test.equal(found.length, 1);
+ test.equal(found[0].law.id, "public_law_110_2_4402_e_1");
+
+ found = Citation.find(text, {parents: true});
+ test.equal(found.length, 4);
+
+ if (found.length == 4) {
+ test.equal(found[0].law.id, "public_law_110_2_4402_e_1");
+ test.equal(found[1].law.id, "public_law_110_2_4402_e");
+ test.equal(found[2].law.id, "public_law_110_2_4402");
+ test.equal(found[3].law.id, "public_law_110_2");
+ } else
+ console.log(found);
+
+ test.done();
+}
View
25 test/usc.js
@@ -499,4 +499,29 @@ exports.testChapters = function(test) {
console.log(found);
test.done();
+}
+
+// can opt-in to asking for all parents of a subsection to be returned too
+exports.testParents = function(test) {
+ test.expect();
+ var text, found, citation;
+
+ text = "31 USC 5318A(a)(1)(A)";
+
+ found = Citation.find(text, {parents: false});
+ test.equal(found.length, 1);
+ test.equal(found[0].usc.id, "31_usc_5318A_a_1_A");
+
+ found = Citation.find(text, {parents: true});
+ test.equal(found.length, 4);
+
+ if (found.length == 4) {
+ test.equal(found[0].usc.id, "31_usc_5318A_a_1_A");
+ test.equal(found[1].usc.id, "31_usc_5318A_a_1");
+ test.equal(found[2].usc.id, "31_usc_5318A_a");
+ test.equal(found[3].usc.id, "31_usc_5318A");
+ } else
+ console.log(found);
+
+ test.done();
}
Please sign in to comment.
Something went wrong with that request. Please try again.