-
Notifications
You must be signed in to change notification settings - Fork 767
/
Bryn Mawr Classical Review.js
131 lines (129 loc) · 4.99 KB
/
Bryn Mawr Classical Review.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
{
"translatorID": "635c1246-e0c8-40a0-8799-a73a0b013ad8",
"label": "Bryn Mawr Classical Review",
"creator": "Michael Berkowitz",
"target": "^https?://bmcr\\.brynmawr\\.edu",
"minVersion": "1.0.0b4.r5",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2012-01-30 22:52:15"
}
function detectWeb(doc, url) {
if (url.match(/by_reviewer/) || url.match(/by_author/) || url.match(/recent.html/) || url.match(/\/\d{4}\/$/)) {
return "multiple";
} else if (url.match(/[\d\-]+\.html$/)) {
return "journalArticle";
}
}
function doWeb(doc, url) {
var ns = doc.documentElement.namespaceURI;
var nsResolver = ns ? function(prefix) {
if (prefix == 'x') return ns; else return null;
} : null;
var arts = new Array();
if (detectWeb(doc, url) == "multiple") {
var items = new Object();
if (doc.evaluate('//table/tbody/tr/td/ul/li/i', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()) {
var boxes = doc.evaluate('//table/tbody/tr/td/ul/li', doc, nsResolver, XPathResult.ANY_TYPE, null);
var box;
while (box = boxes.iterateNext()) {
var link = doc.evaluate('./a', box, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().href;
var title = doc.evaluate('./i', box, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
items[link] = title;
}
} else if (doc.evaluate('//table/tbody/tr/td/ul/li', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()) {
var title = doc.evaluate('//table/tbody/tr/td/ul/li', doc, nsResolver, XPathResult.ANY_TYPE, null);
var next;
while (next = title.iterateNext()) {
items[next.href] = Zotero.Utilities.trimInternal(next.textContent);
}
} else if (url.match(/google\.com/)) {
var titles = doc.evaluate('//h2[@class="r"]/a[@class="l"]', doc, nsResolver, XPathResult.ANY_TYPE, null);
var title;
while (title = titles.iterateNext()) {
items[title.href] = title.textContent;
}
}
items = Zotero.selectItems(items);
for (var i in items) {
arts.push(i);
}
} else {
arts = [url];
}
Zotero.Utilities.processDocuments(arts, function(doc) {
var item = new Zotero.Item("journalArticle");
var title = doc.evaluate('//h3/i', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
item.title = "Review of: " + Zotero.Utilities.trimInternal(title);
var data = doc.evaluate('//h3[i]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
var title = title.replace("(", "\\(").replace(")", "\\)");
var author = doc.evaluate('//b[contains(text(), "Reviewed")]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent.match(/Reviewed by\s+([^,]+),/)[1];
item.creators.push(Zotero.Utilities.cleanAuthor(author, "author"));
var splitRe = new RegExp(title);
var authors = data.split(splitRe)[0].replace(/\([^)]+\)/, "").split(/(,|and)\s+/);
Zotero.debug(authors);
Zotero.debug(authors);
for each (var aut in authors) {
if (aut.match(/\w/) && (aut != "and")) {
item.creators.push(Zotero.Utilities.cleanAuthor(aut, "reviewedAuthor"));
}
}
item.url = doc.location.href;
item.attachments = [{url:item.url, title:item.title, mimeType:"text/html"}];
if (doc.evaluate('/html/body/center/table/tbody/tr/td/center/table/tbody/tr/td/center/font', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()) {
item.date = Zotero.Utilities.trimInternal(doc.evaluate('/html/body/center/table/tbody/tr/td/center/table/tbody/tr/td/center/font', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent.replace("Bryn Mawr Classical Review ", "").replace(/\./g, "/"));
} else {
item.date = Zotero.Utilities.trimInternal(doc.evaluate('/html/body/h3', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent.replace("Bryn Mawr Classical Review ", "").replace(/\./g, "/"))
}
item.complete();
}, function() {Zotero.done();});
Zotero.wait();
}/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "http://bmcr.brynmawr.edu/2010/2010-01-02.html",
"items": [
{
"itemType": "journalArticle",
"creators": [
{
"firstName": "Christina S.",
"lastName": "Kraus",
"creatorType": "author"
},
{
"firstName": "Michael",
"lastName": "Comber",
"creatorType": "reviewedAuthor"
},
{
"firstName": "Catalina",
"lastName": "Balmaceda",
"creatorType": "reviewedAuthor"
}
],
"notes": [],
"tags": [],
"seeAlso": [],
"attachments": [
{
"url": "http://bmcr.brynmawr.edu/2010/2010-01-02.html",
"title": "Review of: Sallust: The War Against Jugurtha. Aris and Phillips Classical Texts",
"mimeType": "text/html"
}
],
"title": "Review of: Sallust: The War Against Jugurtha. Aris and Phillips Classical Texts",
"url": "http://bmcr.brynmawr.edu/2010/2010-01-02.html",
"date": "2010/01/02",
"libraryCatalog": "Bryn Mawr Classical Review",
"accessDate": "CURRENT_TIMESTAMP",
"shortTitle": "Review of"
}
]
}
]
/** END TEST CASES **/