-
Notifications
You must be signed in to change notification settings - Fork 767
/
ARTFL Encyclopedie.js
283 lines (254 loc) · 6.88 KB
/
ARTFL Encyclopedie.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
{
"translatorID": "72cb2536-3211-41e0-ae8b-974c0385e085",
"label": "ARTFL Encyclopedie",
"creator": "Sean Takats, Sebastian Karcher, and Abe Jellinek",
"target": "^https?://artflsrv\\d+\\.uchicago\\.edu/philologic4/encyclopedie\\d+/(navigate/|query)",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2021-06-30 19:55:06"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2021 Abe Jellinek
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('/navigate/')) {
return "encyclopediaArticle";
}
else if (getSearchResults(doc, true)) {
return "multiple";
}
return false;
}
function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = doc.querySelectorAll('.philologic_cite .citation:first-child a');
for (let row of rows) {
let href = row.href;
let title = ZU.trimInternal(row.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) ZU.processDocuments(Object.keys(items), scrape);
});
}
else {
scrape(doc, url);
}
}
function scrape(doc, url) {
let path = url.match(/(\/philologic4\/[^/]+\/)navigate((?:\/\d+)+)/);
if (!path) {
throw new Error('Unknown entry path format');
}
let [, base, id] = path;
id = id.replace(/\//g, ' ').trim();
ZU.doGet(
`${base}reports/navigation.py?report=navigation&philo_id=${id}&byte=`,
function (respText) {
let json = JSON.parse(respText);
scrapeFromJSON(doc, url, json);
}
);
}
function scrapeFromJSON(doc, url, json) {
let item = new Zotero.Item('encyclopediaArticle');
let meta = json.metadata_fields;
item.title = meta.head;
item.encyclopediaTitle = meta.title.replace(/\.?\s*Tome \d+\.?/, '');
item.volume = meta.vol;
item.numberOfVolumes = '17';
item.place = meta.pub_place;
item.publisher = meta.publisher;
item.date = meta.pub_date;
let firstPage;
let lastPage;
let pageRe = /\[page \d+:([\da-zA-Z]+)\]/g;
let matchArray;
while ((matchArray = pageRe.exec(json.text)) !== null) {
// iterate through page heading matches. if we haven't set the first
// page yet, set it to the page in the heading we just found. always
// set the last page to the heading we just found. when we're done,
// the first page will correspond to the first heading and the last page
// to the last.
if (!firstPage) {
firstPage = matchArray[1];
}
lastPage = matchArray[1];
}
if (firstPage && lastPage) {
if (firstPage == lastPage) {
item.pages = firstPage;
}
else {
item.pages = `${firstPage}-${lastPage}`;
}
}
item.url = url;
item.language = 'fr';
item.archive = 'ARTFL Encyclopédie Project (Spring 2021 Edition)';
item.libraryCatalog = '';
item.creators.push({
firstName: "Denis",
lastName: "Diderot",
creatorType: "editor"
});
item.creators.push({
firstName: "Jean le Rond",
lastName: "d'Alembert",
creatorType: "editor"
});
item.creators.push(
ZU.cleanAuthor(
meta.kafauth.replace(/\s*\(.*\)/, ''), 'author', true
)
);
if (doc) {
item.attachments.push({
title: 'Snapshot',
document: doc
});
}
if (json.imgs.current_obj_img && json.imgs.current_obj_img.length) {
let url = json.imgs.current_obj_img[0];
item.attachments.push({
title: 'Page Scan',
mimeType: `image/${url.split('.').pop()}`,
url
});
}
item.complete();
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://artflsrv03.uchicago.edu/philologic4/encyclopedie0521/navigate/1/929/",
"items": [
{
"itemType": "encyclopediaArticle",
"title": "ADULTERE",
"creators": [
{
"firstName": "Denis",
"lastName": "Diderot",
"creatorType": "editor"
},
{
"firstName": "Jean le Rond",
"lastName": "d'Alembert",
"creatorType": "editor"
},
{
"firstName": "François-Vincent",
"lastName": "Toussaint",
"creatorType": "author"
}
],
"date": "1751",
"archive": "ARTFL Encyclopédie Project (Spring 2021 Edition)",
"encyclopediaTitle": "Encyclopédie, Dictionnaire raisonné des sciences, des arts et des métiers, par une Société de Gens de lettres",
"language": "fr",
"numberOfVolumes": "17",
"pages": "150",
"place": "Paris",
"publisher": "Le Breton",
"url": "https://artflsrv03.uchicago.edu/philologic4/encyclopedie0521/navigate/1/929/",
"volume": "1",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
},
{
"title": "Page Scan",
"mimeType": "image/jpeg"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://artflsrv03.uchicago.edu/philologic4/encyclopedie0521/navigate/1/925/",
"items": [
{
"itemType": "encyclopediaArticle",
"title": "ADULTE",
"creators": [
{
"firstName": "Denis",
"lastName": "Diderot",
"creatorType": "editor"
},
{
"firstName": "Jean le Rond",
"lastName": "d'Alembert",
"creatorType": "editor"
},
{
"firstName": "Pierre",
"lastName": "Tarin",
"creatorType": "author"
}
],
"date": "1751",
"archive": "ARTFL Encyclopédie Project (Spring 2021 Edition)",
"encyclopediaTitle": "Encyclopédie, Dictionnaire raisonné des sciences, des arts et des métiers, par une Société de Gens de lettres",
"language": "fr",
"numberOfVolumes": "17",
"pages": "150",
"place": "Paris",
"publisher": "Le Breton",
"url": "https://artflsrv03.uchicago.edu/philologic4/encyclopedie0521/navigate/1/925/",
"volume": "1",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
},
{
"title": "Page Scan",
"mimeType": "image/jpeg"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://artflsrv03.uchicago.edu/philologic4/encyclopedie0521/query?report=concordance&method=proxy&attribution=&objecttype=&q=amour&start=0&end=0",
"items": "multiple"
}
]
/** END TEST CASES **/