forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACM.js
366 lines (340 loc) · 10.3 KB
/
ACM.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
{
"translatorID": "f3f092bf-ae09-4be6-8855-a22ddd817925",
"label": "ACM Digital Library",
"creator": "Simon Kornblith, Michael Berkowitz, John McCaffery, and Sebastian Karcher",
"target": "^https?://[^/]*dl\\.acm\\.org[^/]*/(?:results\\.cfm|citation\\.cfm)",
"minVersion": "1.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "g",
"lastUpdated": "2012-03-09 19:51:25"
}
/*
ACM Digital Library Translator
Copyright (C) 2011 Sebastian Karcher and CHNM
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
function detectWeb(doc, url) {
if (url.indexOf("/results.cfm") != -1) {
//Zotero.debug("Multiple items detected");
return "multiple";
} else if (url.indexOf("/citation.cfm") != -1) {
//Zotero.debug("Single item detected");
return getArticleType(doc, url);
}
}
function doWeb(doc, url) {
var URIs = new Array();
var items = new Object();
if (detectWeb(doc, url) == "multiple") {
var xpath = '//tr/td/a[@target="_self"]';
var articles = doc.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null);
var next_art = articles.iterateNext();
while (next_art) {
items[next_art.href] = next_art.textContent;
next_art = articles.iterateNext();
}
Zotero.selectItems(items, function (items) {
if (!items) {
return true;
}
for (var i in items) {
i = i.replace(/\&preflayout\=(tabs|flat)/, "") + "&preflayout=flat"
//Z.debug(i)
URIs.push(i);
}
Zotero.Utilities.processDocuments(URIs, scrape, function () {
Zotero.done();
});
Zotero.wait();
});
} else {
var newURL;
newURL = url.replace(/\&preflayout\=(tabs|flat)/, "") + "&preflayout=flat"
//Z.debug(newURL);
scrape(doc, newURL);
}
}
//get abstract where possible - this fails frequently
function scrape(doc) {
var xpath = '//div/div[@style="display:inline"]';
var abs = getText(xpath, doc);
//get genric URL
var url = getText('//meta[@name="citation_abstract_html_url"]/@content', doc);
//Zotero.debug('generic URL: ' + url);
var matchtest = url.match(/[0-9]+\.[0-9]+/);
//get item ID and parent ID
//Some items have no parent ID - set the parent ID for them to empty
if (url.match(/[0-9]+\.[0-9]/) != null) {
var itemid = String(url.match(/\.[0-9]+/)).replace(/\./, '');
var parentid = String(url.match(/id\=[0-9]+/)).replace(/id\=/, "");
} else {
var itemid = String(url.match(/id\=[0-9]+/)).replace(/id\=/, "");
var parentid = "";
}
//compose bibtex URL
var bibtexstring = 'id=' + itemid + '&parent_id=' + parentid + '&expformat=bibtex';
var bibtexURL = url.replace(/citation\.cfm/, "exportformats.cfm").replace(/id\=.+/, bibtexstring);
Zotero.debug('bibtex URL: ' + bibtexURL);
Zotero.Utilities.HTTP.doGet(bibtexURL, function (text) {
var translator = Zotero.loadTranslator("import");
var incomplete = [];
translator.setTranslator("9cb70025-a888-4a29-a210-93ec52da40d4");
translator.setString(text);
//Zotero.debug('bibtex data: ' + text);
translator.setHandler("itemDone", function (obj, item) {
//get the URL for the pdf fulltext from the metadata
var pdfURL = getText('//meta[@name="citation_pdf_url"]/@content', doc);
item.attachments = [{
url: pdfURL,
title: "ACM Full Text PDF",
mimeType: "application/pdf"
}];
//fix DOIs if they're in URL form
if (item.DOI) item.DOI = item.DOI.replace(/^.*\/10\./, "10.");
//The Abstract from above - may or may not work
if (abs) item.abstractNote = abs;
//Conference Locations shouldn't go int Loc in Archive (nor should anything else)
item.archiveLocation = "";
// some bibtext contains odd </kwd> tags - remove them
if (item.tags) item.tags = String(item.tags).replace(/\<\/kwd\>/g, "").split(",");
incomplete.push(item);
});
translator.translate();
// assuming the first item is the right one return just that.
if (incomplete.length > 0) incomplete[0].complete();
});
}
//Simon's helper funcitons.
/**
* Find out what kind of document this is by checking google metadata
* @param doc The XML document describing the page
* @param url The URL of the page being scanned
* @param nsResolver the namespace resolver function
* @return a string with either "multiple", "journalArticle", "conferencePaper", or "book" in it, depending on the type of document
*/
function getArticleType(doc, url) {
//var toc = doc.evaluate(tocX, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
if (url.indexOf("results.cfm") != -1) {
//Zotero.debug("Type: multiple");
return "multiple";
}
var conference = getText('//meta[@name="citation_conference"]/@content', doc);
var journal = getText('//meta[@name="citation_journal_title"]/@content', doc);
//Zotero.debug(conference);
if (journal.indexOf(" ") != -1) return "journalArticle";
else if (conference.indexOf(" ") != -1) return "conferencePaper";
else return "book";
}
/**
* Get the text from the first node defined by the given xPathString
* @param pathString the XPath indicating which node to get the text from
* @param doc The XML document describing the page
* @param nsResolver the namespace resolver function
* @return the text in the defined node or "Unable to scrape text" if the node was not found or if there was no text content
*/
function getText(pathString, doc) {
var path = doc.evaluate(pathString, doc, null, XPathResult.ANY_TYPE, null);
var node = path.iterateNext();
if (node == null || node.textContent == undefined || node.textContent == null) {
//Zotero.debug("Unable to retrieve text for XPath: " + pathString);
return "";
}
return node.textContent;
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "http://dl.acm.org/citation.cfm?id=1596682&preflayout=tabs",
"items": [
{
"itemType": "conferencePaper",
"creators": [
{
"firstName": "Nagy",
"lastName": "Mostafa",
"creatorType": "author"
},
{
"firstName": "Chandra",
"lastName": "Krintz",
"creatorType": "author"
}
],
"notes": [],
"tags": [
"calling context tree",
"performance-aware revision control",
"profiling"
],
"seeAlso": [],
"attachments": [
{
"url": "http://dl.acm.org/ft_gateway.cfm?id=1596682&type=pdf",
"title": "ACM Full Text PDF",
"mimeType": "application/pdf"
}
],
"title": "Tracking performance across software revisions",
"publicationTitle": "Proceedings of the 7th International Conference on Principles and Practice of Programming in Java",
"series": "PPPJ '09",
"date": "2009",
"ISBN": "978-1-60558-598-7",
"pages": "162–171",
"url": "http://doi.acm.org/10.1145/1596655.1596682",
"DOI": "10.1145/1596655.1596682",
"publisher": "ACM",
"place": "New York, NY, USA",
"libraryCatalog": "ACM Digital Library",
"accessDate": "CURRENT_TIMESTAMP"
}
]
},
{
"type": "web",
"url": "http://dl.acm.org/citation.cfm?id=1717186&coll=DL&dl=GUIDE",
"items": [
{
"itemType": "book",
"creators": [
{
"firstName": "Jon",
"lastName": "Loeliger",
"creatorType": "author"
}
],
"notes": [],
"tags": [
""
],
"seeAlso": [],
"attachments": [
{
"url": "",
"title": "ACM Full Text PDF",
"mimeType": "application/pdf"
}
],
"title": "Version Control with Git: Powerful Tools and Techniques for Collaborative Software Development",
"date": "2009",
"ISBN": "0596520123, 9780596520120",
"edition": "1st",
"publisher": "O'Reilly Media, Inc.",
"libraryCatalog": "ACM Digital Library",
"shortTitle": "Version Control with Git"
}
]
},
{
"type": "web",
"url": "http://dl.acm.org/citation.cfm?id=254650.257486&coll=DL&dl=GUIDE",
"items": [
{
"itemType": "journalArticle",
"creators": [
{
"firstName": "Mick",
"lastName": "Tegethoff",
"creatorType": "author"
},
{
"firstName": "Tom",
"lastName": "Chen",
"creatorType": "author"
}
],
"notes": [],
"tags": [
"DFM",
"DFT",
"MCM",
"SMT",
"board",
"simulation",
"test",
"yield"
],
"seeAlso": [],
"attachments": [
{
"url": "http://dl.acm.org/ft_gateway.cfm?id=257486&type=pdf",
"title": "ACM Full Text PDF",
"mimeType": "application/pdf"
}
],
"title": "Simulation Techniques for the Manufacturing Test of MCMs",
"publicationTitle": "J. Electron. Test.",
"volume": "10",
"issue": "1-2",
"date": "February 1997",
"ISSN": "0923-8174",
"pages": "137–149",
"url": "http://dx.doi.org/10.1023/A:1008286901817",
"DOI": "10.1023/A:1008286901817",
"publisher": "Kluwer Academic Publishers",
"place": "Norwell, MA, USA",
"libraryCatalog": "ACM Digital Library",
"accessDate": "CURRENT_TIMESTAMP"
}
]
},
{
"type": "web",
"url": "http://dl.acm.org/citation.cfm?id=258948.258973&coll=DL&dl=ACM",
"items": [
{
"itemType": "journalArticle",
"creators": [
{
"firstName": "Conal",
"lastName": "Elliott",
"creatorType": "author"
},
{
"firstName": "Paul",
"lastName": "Hudak",
"creatorType": "author"
}
],
"notes": [],
"tags": [
""
],
"seeAlso": [],
"attachments": [
{
"url": "http://dl.acm.org/ft_gateway.cfm?id=258973&type=pdf",
"title": "ACM Full Text PDF",
"mimeType": "application/pdf"
}
],
"title": "Functional reactive animation",
"publicationTitle": "SIGPLAN Not.",
"volume": "32",
"issue": "8",
"date": "August 1997",
"ISSN": "0362-1340",
"pages": "263–273",
"url": "http://doi.acm.org/10.1145/258949.258973",
"DOI": "10.1145/258949.258973",
"publisher": "ACM",
"place": "New York, NY, USA",
"libraryCatalog": "ACM Digital Library",
"accessDate": "CURRENT_TIMESTAMP"
}
]
}
]
/** END TEST CASES **/