forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 34
/
ACLAnthology.js
159 lines (147 loc) · 4.53 KB
/
ACLAnthology.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": "9b2f2d29-02e3-4693-b728-4cd435650669",
"label": "ACLAnthology",
"creator": "Guy Aglionby",
"target": "^https://aclanthology(\\.coli\\.uni\\-saarland\\.de|\\.info)/(events|papers|volumes)/[^#]+",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2018-09-07 18:10:44"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2018 Guy Aglionby
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
function detectWeb(doc, url) {
if (url.includes('/events/') || url.includes('/volumes/')) {
return 'multiple';
} else if (url.includes('/papers/')) {
let id = url.split('/').pop().toLowerCase();
return id[0] == 'j' || id[0] == 'q' ? 'journalArticle' : 'conferencePaper';
}
}
function doWeb(doc, url) {
const WEBSITE_STUB = 'https://aclanthology.coli.uni-saarland.de/';
if (detectWeb(doc, url) === 'multiple') {
Zotero.selectItems(getSearchResults(doc), function (selected) {
if (!selected) {
return true;
}
Object.keys(selected).forEach(function (id) {
let bibtexUrl = ZU.xpath(doc, '//p[contains(., "' + id + '")]/a[img[@alt = "Export"]]')[0].href;
ZU.doGet(bibtexUrl, function(responseString, responseObj) {
scrapeBibtex(responseString);
});
});
});
} else if(url.endsWith('.bib')) {
scrapeBibtex(ZU.xpath(doc, '//pre')[0].innerHTML);
} else {
let partialBibtexUrl = ZU.xpath(doc, '//a[contains(., "BibTeX")]/@href')[0].value;
let bibtexUrl = WEBSITE_STUB + partialBibtexUrl;
ZU.doGet(bibtexUrl, function(responseString, responseObj) {
scrapeBibtex(responseString);
});
}
}
function getSearchResults(doc) {
// additional example not covered in tests (fails in automated testing)
// https://aclanthology.coli.uni-saarland.de/volumes/computational-linguistics-volume-44-issue-1-april-2018",
let listing = ZU.xpath(doc, '//div[@id="content"]//p');
let items = {};
for (let i = 0; i < listing.length; i++) {
let title = ZU.xpath(listing[i], 'strong')[0].textContent;
let bibtexLink = ZU.xpath(listing[i], 'a[img[@alt = "Export"]]/@href')[0].value;
let id = bibtexLink.split('/')[2];
items[id] = title;
}
return items;
}
function scrapeBibtex(responseString) {
let translator = Zotero.loadTranslator("import");
translator.setTranslator("9cb70025-a888-4a29-a210-93ec52da40d4");
translator.setString(responseString);
translator.setHandler("itemDone", function (obj, item) {
item.attachments.push({
url: item.url,
title: 'Full Text PDF',
mimeType: 'application/pdf'
});
delete item.itemID;
item.complete();
});
translator.translate();
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://aclanthology.coli.uni-saarland.de/events/cl-2018",
"items": "multiple"
},
{
"type": "web",
"url": "https://aclanthology.coli.uni-saarland.de/papers/J18-1002/j18-1002",
"items": [
{
"itemType": "journalArticle",
"title": "On the Derivational Entropy of Left-to-Right Probabilistic Finite-State Automata and Hidden Markov Models",
"creators": [
{
"firstName": "Joan Andreu",
"lastName": "Sánchez",
"creatorType": "author"
},
{
"firstName": "Martha Alicia",
"lastName": "Rocha",
"creatorType": "author"
},
{
"firstName": "Verónica",
"lastName": "Romero",
"creatorType": "author"
},
{
"firstName": "Mauricio",
"lastName": "Villegas",
"creatorType": "author"
}
],
"date": "2018",
"DOI": "10.1162/COLI_a_00306",
"issue": "1",
"libraryCatalog": "ACLAnthology",
"pages": "17–37",
"publicationTitle": "Computational Linguistics",
"url": "http://aclweb.org/anthology/J18-1002",
"volume": "44",
"attachments": [
{
"title": "Full Text PDF",
"mimeType": "application/pdf"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/