-
Notifications
You must be signed in to change notification settings - Fork 1
/
Common-Place.js
218 lines (208 loc) · 6.06 KB
/
Common-Place.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
{
"translatorID": "c3edb423-f267-47a1-a8c2-158c247f87c2",
"label": "Common-Place",
"creator": "Frederick Gibbs, Philipp Zumstein",
"target": "^https?://(www\\.)?(common-place\\.org/|common-place-archives\\.org/)",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2016-09-10 09:34:34"
}
function detectWeb(doc, url) {
if (getSearchResults(doc, true)) {//multiples works only on search pages
return "multiple";
} else if (doc.body.className.indexOf("single-article")>-1 || url.indexOf("common-place-archives.org")>-1) {
return "journalArticle";
}
}
function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = ZU.xpath(doc, '//h3[contains(@class, "article-title")]/a|//h2/a');
for (var i=0; i<rows.length; i++) {
var href = rows[i].href;
var title = ZU.trimInternal(rows[i].textContent);
if (!href || !title) continue;
if (checkOnly) return true;
found = true;
items[href] = title;
}
return found ? items : false;
}
function doWeb(doc, url) {
if (detectWeb(doc, url) == "multiple") {
Zotero.selectItems(getSearchResults(doc, false), function (items) {
if (!items) {
return true;
}
var articles = [];
for (var i in items) {
articles.push(i);
}
ZU.processDocuments(articles, scrape);
});
} else {
scrape(doc, url);
}
}
function scrape(doc, url) {
var newItem = new Zotero.Item("journalArticle");
newItem.publicationTitle = "Common-Place";
newItem.url = url;
if (doc.body.className.indexOf("single-article")>-1) {
newItem.title = ZU.xpathText(doc, '//article/h1');
var author = ZU.xpathText(doc, '//article/h1/following-sibling::p');
if (author) {
newItem.creators.push(ZU.cleanAuthor(author, "author"));
}
newItem.abstractNote = ZU.xpathText(doc, '//article/div[contains(@class, "entry-excerpt")]');
newItem.date = ZU.strToISO(ZU.trimInternal(ZU.xpathText(doc, '//article/ol[contains(@class, "breadcrumb")]/li/text()')));
var volno = ZU.xpathText(doc, '//article/ol[contains(@class, "breadcrumb")]/li[1]/a');
var m = volno.match(/Vol\. (\d+) No\. (\d+)/);
if (m) {
newItem.volume = m[1];
newItem.issue = m[2];
}
} else {
//get issue year and month
//these will determine what xpaths we use for title and author
//e.g. <a href="/vol-12/no-01/">vol. 12 · no. 1 · October 2011</a>
var dateRe = /<a href="\/vol-(\d+)\/no-(\d+)\/">([^<]*)<\/a>/;
var m = dateRe.exec(ZU.trimInternal(doc.getElementsByTagName("body")[0].innerHTML));
if(m) {
newItem.volume = m[1];
newItem.issue = m[2];
var n = m[3].match(/· ([\w\s]+)$/);
if (n) {
newItem.date = ZU.strToISO(n[1]);
}
}
var author = ZU.xpathText(doc, '//div[@id="content"]/p/span[1]');
var title = ZU.xpathText(doc, '//div[@id="content"]/p/span[2]');
if (author) {
//determine if we have a book review
// if so, get the publication information
if (author.indexOf("Review by") != -1 ) {
title = String.concat("Review of ", title);
author = author.substring(10);
}
newItem.creators.push(ZU.cleanAuthor(author, "author"));
} else { //we have older issue
//check if we are on a review
var review = ZU.xpathText(doc, '/html/body/table/tbody/tr/td[2]/p[2]');
if (review.indexOf("Review") != -1) {
title = ZU.xpathText(doc, '/html/body/table/tbody/tr/td[2]/p/i');
title = "Review of " + title;
author = review.substring(10);
} else { //for articles
title = ZU.xpathText(doc, '/html/body/table/tbody/tr/td[2]/p/b');
author = ZU.xpathText(doc, '/html/body/table/tbody/tr/td[2]/p[1]').split(/\n/)[1];;
}
newItem.creators.push(ZU.cleanAuthor(author, "author"));
}
newItem.title = title;
}
newItem.attachments.push({document:doc, title:doc.title});
newItem.complete();
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "http://www.common-place-archives.org/vol-12/no-01/tales/",
"items": [
{
"itemType": "journalArticle",
"title": "Looking for Limbs in all the Right Places",
"creators": [
{
"firstName": "Megan Kate",
"lastName": "Nelson",
"creatorType": "author"
}
],
"date": "2011-10",
"issue": "01",
"libraryCatalog": "Common-Place",
"publicationTitle": "Common-Place",
"url": "http://www.common-place-archives.org/vol-12/no-01/tales/",
"volume": "12",
"attachments": [
{
"title": "Common-place: Tales from the Vault"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://www.common-place-archives.org/vol-03/no-03/mccaffrey/",
"items": [
{
"itemType": "journalArticle",
"title": "American Originals",
"creators": [
{
"firstName": "Katherine Stebbins",
"lastName": "McCaffrey",
"creatorType": "author"
}
],
"date": "2003-04",
"issue": "03",
"libraryCatalog": "Common-Place",
"publicationTitle": "Common-Place",
"url": "http://www.common-place-archives.org/vol-03/no-03/mccaffrey/",
"volume": "03",
"attachments": [
{
"title": "Common-place: American Originals"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://common-place.org/book/alive-with-the-sound-of-music/",
"items": [
{
"itemType": "journalArticle",
"title": "Alive with the Sound of Music",
"creators": [
{
"firstName": "Douglas",
"lastName": "Shadle",
"creatorType": "author"
}
],
"date": "2008-04",
"abstractNote": "Next to Stephen Foster, William Henry Fry was arguably the most important American composer working before the Civil War.",
"issue": "3",
"libraryCatalog": "Common-Place",
"publicationTitle": "Common-Place",
"url": "http://common-place.org/book/alive-with-the-sound-of-music/",
"volume": "8",
"attachments": [
{
"title": "Alive with the Sound of Music › Common-placeCommon-place: The Journal of early American Life"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/