forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CanLII (English).js
159 lines (148 loc) · 4.62 KB
/
CanLII (English).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
{
"translatorID": "84799379-7bc5-4e55-9817-baf297d129fe",
"label": "CanLII (English)",
"creator": "Sebastian Karcher",
"target": "^https?://(www\\.)?canlii\\.org.*/en/",
"minVersion": "2.1.9",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "g",
"lastUpdated": "2012-03-24 00:26:50"
}
var canLiiRegexp = /http:\/\/(www\.)?canlii\.org\/.*en\/[^\/]+\/[^\/]+\/doc\/.+/;
function detectWeb(doc, url) {
if (canLiiRegexp.test(url)) {
return "case";
} else {
var aTags = doc.getElementsByTagName("a");
for (var i = 0; i < aTags.length; i++) {
if (canLiiRegexp.test(aTags[i].href)) {
return "multiple";
}
}
}
}
function scrape(doc) {
var newItem = new Zotero.Item("case");
var voliss = ZU.xpathText(doc, '//td[@class="label" and contains(text(), "Citation:")]/following-sibling::td');
//Z.debug("voliss: ("+voliss+")")
var casename = voliss.match(/.+?,/)[0].replace(/,/, "").trim();
//Z.debug("casename: ("+casename+")");
var court = voliss.match(/,\s*\d{4}\s*[A-Z]+/);
//Z.debug("court: ("+court+")");
var reportvl = voliss.match(/\]\s*\d+/);
//Z.debug("reportvl: ("+reportvl+")");
var reporter = voliss.match(/\]\s*\d+\s*[A-Z]+/);
//Z.debug("reporter: ("+reporter+")");
var reporterpg = voliss.match(/\]\s*\d+\s*[A-Z]+\s*\d+/);
//Z.debug("reporterpg: ("+reporterpg+")");
var page = voliss.match(/,\s*\d{4}\s*[A-Z]+\s*\d+/);
var date = ZU.xpathText(doc, '//td[@class="label" and contains(text(), "Date:")]/following-sibling::td');
//Z.debug("date: ("+date+")")
var docket = ZU.xpathText(doc, '//td[@class="label" and contains(text(), "Docket:")]/following-sibling::td');
//Z.debug("docket: ("+docket+")")
newItem.caseName = newItem.title = casename;
if (court) newItem.court = court[0].replace(/,\s*\d{4}\s*/, "").trim();;
if (reporter) newItem.reporter = reporter[0].replace(/\]\s*\d+\s*/, "");
newItem.dateDecided = date;
if (docket) newItem.docketNumber = docket.trim();
if (reporterpg) newItem.firstPage = reporterpg[0].replace(/\]\s*\d+\s*[A-Z]+\s*/, "");
//
if (!reporterpg && page) newItem.firstPage = page[0].replace(/,\s*\d{4}\s*[A-Z]+\s*/, "");
if (reportvl) newItem.reporterVolume = reportvl[0].replace(/\]\s*/, "");
// attach link to pdf version
var pdfurl = ZU.xpathText(doc, '//td/a[contains(text(), "PDF Format")]/@href');
if (pdfurl) {
pdfurl = "http://canlii.ca" + pdfurl;
newItem.attachments = [{
url: pdfurl,
title: "CanLII Full Text PDF",
mimeType: "application/pdf"
}];
}
newItem.attachments.push({
document: doc,
title: "CanLII Snapshot",
mimeType: "text/html"
});
newItem.complete();
}
function doWeb(doc, url) {
if (canLiiRegexp.test(url)) {
scrape(doc, url);
} else {
var items = Zotero.Utilities.getItemArray(doc, doc, canLiiRegexp);
var articles = [];
Zotero.selectItems(items, function (items) {
if (!items) {
return true;
}
for (var i in items) {
articles.push(i);
}
Zotero.Utilities.processDocuments(articles, scrape, function () {
Zotero.done();
});
Zotero.wait();
});
}
}
/** BEGIN TEST CASES **/
var testCases = [{
"type": "web",
"url": "http://canlii.org/en/ca/scc/nav/date/2010.html",
"items": "multiple"
}, {
"type": "web",
"url": "http://canlii.org/en/ca/scc/doc/2010/2010scc2/2010scc2.html",
"items": [{
"itemType": "case",
"creators": [],
"notes": [],
"tags": [],
"seeAlso": [],
"attachments": [{
"title": "CanLII Full Text PDF",
"mimeType": "application/pdf"
}, {
"title": "CanLII Snapshot",
"mimeType": "text/html"
}],
"title": "MiningWatch Canada v. Canada (Fisheries and Oceans)",
"caseName": "MiningWatch Canada v. Canada (Fisheries and Oceans)",
"court": "SCC",
"reporter": "SCR",
"dateDecided": "2010-01-21",
"docketNumber": "32797",
"firstPage": "6",
"reporterVolume": "1",
"libraryCatalog": "CanLII"
}]
}, {
"type": "web",
"url": "http://canlii.org/eliisa/highlight.do?language=en&searchTitle=SOR%2F97-457&origin=%2Fen%2Fca%2Flaws%2Fregu%2Fsor-97-457%2Flatest%2Fsor-97-457.html&translatedOrigin=%2Ffr%2Fca%2Flegis%2Fregl%2Fdors-97-457%2Fderniere%2Fdors-97-457.html&path=/en/ca/fct/doc/2011/2011fc119/2011fc119.html",
"items": [{
"itemType": "case",
"creators": [],
"notes": [],
"tags": [],
"seeAlso": [],
"attachments": [{
"title": "CanLII Full Text PDF",
"mimeType": "application/pdf"
}, {
"title": "CanLII Snapshot",
"mimeType": "text/html"
}],
"title": "Suttie v. Canada (Attorney General)",
"caseName": "Suttie v. Canada (Attorney General)",
"court": "FC",
"dateDecided": "2011-02-02",
"docketNumber": "T-1089-10",
"firstPage": "119",
"libraryCatalog": "CanLII"
}]
}]
/** END TEST CASES **/