forked from 18F/linkify-citations
-
Notifications
You must be signed in to change notification settings - Fork 1
/
linkify.min.js
1 lines (1 loc) · 14.6 KB
/
linkify.min.js
1
(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);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.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=function(Citation){Citation={types:{},filters:{},find:function(text,options){if(!options)options={};if(typeof text!=="string")return;var results;if(options.filter&&Citation.filters[options.filter])return Citation.filtered(options.filter,text,options);else return Citation.extract(text,options)},filtered:function(name,text,options){var results=[];var filter=Citation.filters[name];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}},extract:function(text,options){if(!options)options={};var excerpt=options.excerpt?parseInt(options.excerpt,10):0;var parents=options.parents||false;var types=Citation.selectedTypes(options);if(types.length===0)return null;var replace=options.replace;var results=[];var citators={};var nextIndex=0;types.forEach(function(type){if(Citation.types[type].type!="regex")return;var patterns=Citation.types[type].patterns;if(typeof patterns=="function")patterns=patterns(options[type]||{});patterns.forEach(function(pattern){pattern.type=type;citators[nextIndex]=pattern;nextIndex+=pattern.fields.length+1})});var regexes=Object.keys(citators).map(function(key){return citators[key].regex});if(regexes.length>0){var regex=new RegExp("("+regexes.join(")|(")+")","ig");var replaced=text.replace(regex,function(){var match=arguments[0];var index=arguments[arguments.length-2];var captures=Array.prototype.slice.call(arguments,1,-2);var matchIndex;for(matchIndex=0;matchIndex<captures.length;matchIndex++)if(captures[matchIndex])break;var citator=citators[matchIndex];if(!citator)return null;var type=citator.type;var ourCaptures=Array.prototype.slice.call(captures,matchIndex+1);var namedMatch=Citation.matchFor(ourCaptures,citator);var cites=citator.processor(namedMatch);if(!Array.isArray(cites))cites=[cites];var matchInfo={type:citator.type};matchInfo.match=match.toString();matchInfo.index=index;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(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={};Citation._.extend(result,matchInfo);if(cite._submatch){result.match=cite._submatch.text;result.index+=cite._submatch.offset;delete cite._submatch}result[type]=cite;result[type].id=Citation.types[type].id(cite);results.push(result);return result});var finalstring=matchInfo.match;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(replace_func){var last_index=0;var dx=0;for(var i=0;i<cites.length;i++){if(cites[i].index>=last_index){var replacement=replace_func(cites[i]);if(replacement){finalstring=finalstring.substring(0,cites[i].index-index+dx)+replacement+finalstring.substring(cites[i].index-index+cites[i].match.length+dx);dx+=replacement.length-cites[i].match.length;last_index=cites[i].index+cites[i].match.length}}delete cites[i].index}}return finalstring})}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},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},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]}if(types){types=types.filter(function(type){return Object.keys(Citation.types).indexOf(type)!=-1})}else types=Object.keys(Citation.types);return types},_:{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,[])}}};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.dc_stat=require("./citations/dc_stat");Citation.types.stat=require("./citations/stat");Citation.types.reporter=require("./citations/reporter");Citation.filters.lines=require("./filters/lines")}if(typeof window!=="undefined")window.Citation=Citation;return Citation}()},{"./citations/cfr":2,"./citations/dc_code":3,"./citations/dc_law":4,"./citations/dc_register":5,"./citations/dc_stat":6,"./citations/law":7,"./citations/reporter":8,"./citations/stat":9,"./citations/usc":10,"./citations/va_code":11,"./filters/lines":12}],2:[function(require,module,exports){module.exports={type:"regex",id:function(data){return["cfr",data.title,data.section||data.part].concat(data.subsections||[]).join("/")},patterns:[{regex:"(\\d+)\\s?"+"C\\.?\\s?F\\.?\\s?R\\.?"+"(?:[\\s,]+(?:§+|parts?))?"+"\\s*((?:\\d+\\.?\\d*(?:\\s*\\((?:[a-zA-Z\\d]{1,2}|[ixvIXV]+)\\))*)+)",fields:["title","sections"],processor:function(captures){var title=captures.title;var part,section,subsections;var split=captures.sections.split(/[\(\)]+/).filter(function(x){return x});section=split[0].trim();subsections=split.splice(1);if(section.indexOf(".")>0)part=section.split(".")[0];else{part=section;section=null;subsections=null}return{title:title,part:part,section:section,subsections:subsections}}}]}},{}],3:[function(require,module,exports){var base_regex="(\\d+A?)"+"\\s?\\-\\s?"+"([\\w\\d]+(?:\\.?[\\w\\d]+)?)"+"((?:\\([^\\)]+\\))*)";module.exports={type:"regex",id:function(cite){return["dc-code",cite.title,cite.section].concat(cite.subsections).join("/")},parents_by:"subsections",patterns:function(context){var prefix_regex="";var section_regex="(?:sections?|§+)\\s+";var sections_regex="(?:sections|§§)\\s+";if(context.source!="dc_code"){prefix_regex="D\\.?C\\.? (?:Official )?Code\\s+";section_regex="(?:"+section_regex+")?";sections_regex="(?:"+sections_regex+")?"}return[{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}},{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[]}},{}],4:[function(require,module,exports){module.exports={type:"regex",id:function(cite){return["dc-law",cite.period,cite.number].join("/")},patterns:function(context){var context_regex="D\\.?\\s*C\\.?\\s+";if(context.source=="dc_code")context_regex="(?:"+context_regex+")?";return[{regex:context_regex+"Law\\s+(\\d+)\\s?[-–]+\\s?(\\d+\\w?)",fields:["period","number"],processor:function(captures){return{period:captures.period,number:captures.number}}}]}}},{}],5:[function(require,module,exports){module.exports={type:"regex",id:function(cite){return["dc-register",cite.volume,cite.page].join("/")},patterns:[{regex:"(\\d+)\\s+"+"DCR"+"\\s+(\\d+)",fields:["volume","page"],processor:function(match){return{volume:match.volume,page:match.page}}}]}},{}],6:[function(require,module,exports){module.exports={type:"regex",id:function(cite){return["dcstat",cite.volume,cite.page].join("/")},patterns:[{regex:"(\\d+)\\s+"+"DCSTAT"+"\\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("/")},parents_by:"sections",patterns:[{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}}},{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",id:function(cite){return["reporter",cite.volume,cite.reporter,cite.page].join("/")},patterns:[{regex:"(\\d{1,3})\\s"+"(\\w+(?:\\.\\w+(?:\\.)?)?(?:\\.\\dd)?|U\\.?\\s?S\\.?|F\\. Supp\\.(?:\\s\\dd)?)\\s"+"(\\d{1,4})",fields:["volume","reporter","page"],processor:function(match){return{volume:match.volume,reporter:match.reporter,page:match.page}}}]}},{}],9:[function(require,module,exports){module.exports={type:"regex",id:function(cite){return["stat",cite.volume,cite.page].join("/")},patterns:[{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("/")},parents_by:"subsections",patterns:[{regex:"(\\d+)\\s+"+"U\\.?\\s?S\\.?\\s?C\\.?"+"(?:\\s+(App).?)?"+"(?:\\s+(§+))?"+"\\s+((?:\\-*\\d+[\\w\\d\\-]*(?:\\([^\\)]+\\))*)+)"+"(?:\\s+(note|et\\s+seq))?",fields:["title","appendix","symbol","sections","note"],processor:function(match){var title=match.title;if(match.appendix)title+="-app";var sections=match.sections.split(/-+/);var range=false;if(match.symbol=="§§")range=true;else{var dash=match.sections.indexOf("-");var paren=match.sections.indexOf("(");if(dash>0&&paren>0&&paren<dash)range=true}if(sections.length>1&&!range)sections.unshift(match.sections);return sections.map(function(section){var split=section.split(/[\(\)]+/).filter(function(x){return x});section=split[0];subsections=split.splice(1);if(match.note)subsections.push(match.note.replace(" ","-"));return{title:title,section:section,subsections:subsections}})}},{regex:"section (\\d+[\\w\\d-]*)((?:\\([^\\)]+\\))*)"+"(?:\\s+of|\\,) title (\\d+)",fields:["section","subsections","title"],processor:function(match){return{title:match.title,section:match.section,subsections:match.subsections.split(/[\(\)]+/).filter(function(x){return x})}}}]}},{}],11:[function(require,module,exports){module.exports={type:"regex",id:function(cite){return["va-code",data.title,data.section].join("/")},patterns:[{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}}}]}},{}],12:[function(require,module,exports){module.exports={from:function(text,options,extract){var delimiter=options&&options.delimiter||/[\n\r]+/;var lines=text.split(new RegExp(delimiter));lines.forEach(function(line,i){extract(line,{line:i+1})})}}},{}],13:[function(require,module,exports){var Citation=require("citation");var citationToURL=function(citation){var url=getURLfromCitation(citation);if(url)return"<a class='citation' href='"+url+"'>"+citation.match+"</a>";else return citation.match};var replaceDOM=function(document){var thePage=document.documentElement.cloneNode(true);var citations=Citation.find(thePage.innerHTML).citations;for(i=0;i<citations.length;i++){var link=citationToURL(citations[i]);thePage.innerHTML=thePage.innerHTML.replace(citations[i].match,link)}return thePage.innerHTML};var getURLfromCitation=function(citation){var url="http://api.fdsys.gov/link?collection=";switch(citation.type){case"usc":return url+"uscode&title="+citation.usc.title+"&year=mostrecent§ion="+citation.usc.section+"&type=usc";case"law":return url+"plaw&congress="+citation.law.congress+"&lawtype=public&lawnum="+citation.law.number;case"cfr":return"http://api.fdsys.gov/link?collection=cfr&titlenum="+citation.cfr.title+"&partnum="+citation.cfr.part+"§ionnum=";citation.cfr.section+"&year=mostrecent";case"stat":return url+"statute&volume="+citation.stat.volume+"&page="+citation.stat.page;case"fedreg":return url+"fr&volume="+citation.fedreg.volume+"&page="+citation.fedreg.page;default:return false}};if(typeof window==="undefined"){module.exports={getURLfromCitation:getURLfromCitation,replaceDOM:replaceDOM,citationToURL:citationToURL}}else{window.document.addEventListener("DOMContentLoaded",function(){window.document.documentElement.innerHTML=replaceDOM(window.document)})}},{citation:1}]},{},[13]);