forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cambridge Scientific Abstracts.js
211 lines (193 loc) · 6.76 KB
/
Cambridge Scientific Abstracts.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
{
"translatorID": "82174f4f-8c13-403b-99b2-affc7bc7769b",
"label": "Cambridge Scientific Abstracts",
"creator": "Simon Kornblith and Michael Berkowitz",
"target": "^https?://[^/]+/ids70/(?:results\\.php|view_record\\.php)",
"minVersion": "1.0.0b3.r1",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "g",
"lastUpdated": "2011-09-28 11:45:02"
}
/* Provides support for databases of Cambridge Scientific Abstracts
Tested with CSA Illumina, http://www.csa.com/
CSA does not provide stable URLs
*/
function detectWeb(doc, url) {
var namespace = doc.documentElement.namespaceURI;
var nsResolver = namespace ? function(prefix) {
if (prefix == 'x') return namespace; else return null;
} : null;
if(url.indexOf("/results.php") != -1) {
var type = doc.evaluate('//td[@class="rt_tab_on"]', doc, nsResolver, XPathResult.ANY_TYPE,
null).iterateNext().textContent;
if(type.substr(0, 15) == "Published Works") {
return "multiple";
}
} else {
// default to journal
var itemType = "journalArticle";
var type = doc.evaluate('//tr[td[1][@class="data_heading"]/text() = "Publication Type"]/td[3]',
doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext();
if(type) {
type = Zotero.Utilities.trimInternal(type.textContent);
if(type == "Book Chapter") {
return "bookSection";
} else if(type.substr(0, 4) == "Book") {
return "book";
} else if(type.substr(0, 12) == "Dissertation") {
return "thesis";
} else if(type == "Catalog") {
return "magazineArticle";
}
}
return "journalArticle";
}
return false;
}
function scrape(doc) {
var namespace = doc.documentElement.namespaceURI;
var nsResolver = namespace ? function(prefix) {
if (prefix == 'x') return namespace; else return null;
} : null;
var itemType = "journalArticle";
var type = doc.evaluate('//tr[td[1][@class="data_heading"]/text() = "Publication Type"]/td[3]',
doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext();
if(type) {
type = Zotero.Utilities.trimInternal(type.textContent);
if(type == "Book Chapter") {
itemType = "bookSection";
} else if(type.substr(0, 4) == "Book") {
itemType = "book";
} else if(type.substr(0, 12) == "Dissertation") {
itemType = "thesis";
} else if(type == "Catalog") {
itemType = "magazineArticle";
}
}
var newItem = new Zotero.Item(itemType);
newItem.attachments = [{document:doc, title:"Cambridge Scientific Abstracts Snapshot"}];
newItem.title = Zotero.Utilities.trimInternal(doc.evaluate('//tr/td[3][@class="data_emphasis"]', doc, nsResolver,
XPathResult.ANY_TYPE, null).iterateNext().textContent);
var dataRows = doc.evaluate('//tr[td[3][@class="data_content"]]', doc, nsResolver,
XPathResult.ANY_TYPE, null);
var dataRow;
while(dataRow = dataRows.iterateNext()) {
var tds = dataRow.getElementsByTagName("td");
var heading = Zotero.Utilities.trimInternal(tds[0].textContent).toLowerCase();
var content = Zotero.Utilities.trimInternal(tds[2].textContent);
if(heading == "database") {
newItem.repository = "Cambridge Scientific Abstracts ("+content+")";
} else if(heading == "author") {
var authors = content.split("; ");
for each(var author in authors) {
newItem.creators.push(Zotero.Utilities.cleanAuthor(author.replace(/\d+/g, ""), "author", true));
}
} else if(heading == "source") {
if(itemType == "journalArticle") {
var parts = content.split(/(,|;)/);
newItem.publicationTitle = parts.shift();
for each (var i in parts) {
if (i.match(/\d+/)) {
if (i.match(/v(ol)?/)) {
newItem.volume = i.match(/\d+/)[0];
} else if (i.match(/pp/)) {
newItem.pages = i.match(/[\d\-]+/)[0];
} else if (i.match(/no?/)) {
newItem.issue = i.match(/\d+/)[0];
} else if (i.match(/\d{4}/)) {
newItem.date = Zotero.Utilities.trimInternal(i);
}
}
}
} else if(itemType == "book") {
var m = content.match(/^([^:]+): ([^,0-9]+)/);
if(m) {
newItem.place = m[1];
newItem.publisher = m[2];
}
} else if(itemType == "bookSection") {
var untitled = !newItem.publicationTitle;
if(untitled || (content.length > newItem.publicationTitle.length
&& content.substr(0, newItem.publicationTitle.length) == newItem.publicationTitle)
|| content.indexOf(newItem.publicationTitle)) {
if (content.indexOf(newItem.publicationTitle) > 4) {
// This means we probably have a book author or editor first
var m = content.match(/^([^\.]+)\./);
if (m) newItem.creators.push(
Zotero.Utilities.cleanAuthor(m[1], "bookAuthor", true));
}
var m = content.match(/\)\. ([^:()]+): ([^,0-9]+)/);
if(m) {
if (untitled) {
var n = content.match(/\([0-9]{4}\)([^(]*)/);
if (n) newItem.publicationTitle = n[1];
}
newItem.place = m[1];
newItem.publisher = m[2];
}
m = content.match(/\(pp. ([\-0-9]+)\)/);
if(m) newItem.pages = m[1];
}
}
} else if(heading == "monograph title") {
newItem.publicationTitle = content;
} else if(heading == "series title") {
newItem.series = content;
} else if(heading == "issn") {
newItem.ISSN = content;
} else if(heading == "isbn") {
newItem.ISBN = content;
} else if(heading == "abstract") {
newItem.abstractNote = content;
} else if(heading == "notes") {
newItem.extra = content;
} else if(heading == "publication year") {
if(!newItem.date) newItem.date = content;
} else if(heading == "information provider") {
if(content.substr(0, 19) == "http://dx.doi.org/") {
newItem.DOI = content.substr(19);
}
} else if(heading == "journal volume") {
newItem.volume = content;
} else if(heading == "doi") {
newItem.DOI = content;
} else if(heading == "journal pages") {
newItem.pages = content;
} else if(heading == "journal issue") {
newItem.issue = content;
} else if(heading == "affiliation") {
if(newItem.itemType == "thesis") {
newItem.publisher = content;
}
} else if(heading == "pages") { // This is for book sections
newItem.pages = content;
} else if(heading == "language") {
newItem.language = content;
}
}
var terms = doc.evaluate('//input[substring(@name, 1, 4) = "term"]', doc, nsResolver,
XPathResult.ANY_TYPE, null);
var term;
while(term = terms.iterateNext()) {
newItem.tags.push(term.value.replace(/ [0-9]{3,}$/, ""));
}
newItem.complete();
}
function doWeb(doc, url) {
if(url.indexOf("/results.php") != -1) {
var items = Zotero.Utilities.getItemArray(doc, doc, '/view_record\.php\?', '^(?:View Record|More\.{3})$');
items = Zotero.selectItems(items);
if(!items) return true;
var urls = new Array();
for(var url in items) {
urls.push(url);
}
Zotero.Utilities.processDocuments(urls, scrape, function() { Zotero.done() })
Zotero.wait();
} else {
scrape(doc);
}
}