-
Notifications
You must be signed in to change notification settings - Fork 762
/
Bosworth Toller's Anglo-Saxon Dictionary Online.js
496 lines (466 loc) · 14.5 KB
/
Bosworth Toller's Anglo-Saxon Dictionary Online.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
{
"translatorID": "b2d07a2a-c8c6-4426-ba6b-35f094a4d916",
"label": "Bosworth Toller's Anglo-Saxon Dictionary Online",
"creator": "Zoë C. Ma",
"target": "^https://bosworthtoller\\.com/",
"minVersion": "5.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2023-08-18 07:39:58"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2023 Zoë C. Ma
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) {
// The logic is that "a page's type is determined by its content", because
// the current implementation of the dictionary web app may not be able to
// sync URL correctly all the time.
if (getSearchResults(doc, true/* checkOnly */)) {
return "multiple";
}
if (doc.querySelector("#btd--entry-single")) {
return "dictionaryEntry";
}
return false;
}
function getSearchResults(doc, checkOnly = false) {
let items = {};
let found = false;
let rows = doc.querySelectorAll(".btd--search-entry");
for (let row of rows) {
// Don't retrieve the "similar entry" links
let href = attr(row, ".btd--search-entry-header a", "href");
let title = ZU.trimInternal(text(row, ".btd--entry-grammar").trim());
if (!title) {
title = text(row, ".btd--search-entry-header a"); // fallback
}
if (!href || !title) continue;
if (checkOnly) return true;
found = true;
items[href] = title;
}
return found ? items : false;
}
async function doWeb(doc, url) {
if (detectWeb(doc) === 'multiple') {
let items = await Z.selectItems(getSearchResults(doc));
if (!items) return;
for (let url of Object.keys(items)) {
scrape(await requestDocument(url));
}
}
else {
scrape(doc, url);
}
}
const BOSWORTH_TOLLER_INFO = {
dictionaryTitle: "An Anglo-Saxon Dictionary Online",
language: "en",
place: "Prague",
publisher: "Faculty of Arts, Charles University",
date: "2014",
creators: [
{ firstName: "Joseph", lastName: "Bosworth", creatorType: "author" },
{ firstName: "Thomas Northcote", lastName: "Toller", creatorType: "editor" },
{ firstName: "Christ", lastName: "Sean", creatorType: "editor" },
{ firstName: "Ondřej", lastName: "Tichy", creatorType: "editor" },
],
};
function scrape(doc, url = doc.location.href) {
let item = new Z.Item("dictionaryEntry");
// "Constant" fields
Object.assign(item, BOSWORTH_TOLLER_INFO);
// Page-specific data
item.url = url;
// Word entry
item.title = normalizeLemma(doc) || "[Unknown entry]";
// Original publication and page number in it, if any, as extra
item.extra = getExtraInfo(doc);
// Snapshot
item.attachments = [{
document: doc,
title: "Snapshot",
mimeType: "text/html"
}];
item.complete();
}
// See https://bosworthtoller.com/images-dictionary/frontback_matter.pdf
var BOOK_ORIG_INFO = {
b: "Original Dictionary Title: An Anglo-Saxon Dictionary, Based on the Manuscript Collections of the Late Joseph Bosworth, D.D., F.R.S.\nOriginal Date: 1898\nOriginal Publisher: Oxford University Press\nOriginal Place: London",
d: "Original Dictionary Title: An Anglo-Saxon Dictionary, Based on the Manuscript Collections of the Late Joseph Bosworth; Supplement\nOriginal Date: 1921\nOriginal Publisher: Oxford University Press\nOriginal Place: London",
};
// Get the extra info including original volume info and page number by parsing
// the URL of the scanned page linked to the article. Returns a string where
// each extra entry occupies one line in `key: value` format, or empty string
// if the original book and page cannot be determined.
function getExtraInfo(doc) {
let imageURL = attr(doc, ".btd--image-pin-pan > img", "src");
if (!imageURL) {
return "";
}
// "b" for main book (1898), "d" for supplement (1912)
let pageMatch = imageURL.match(/^\/images-dictionary\/bt_([bd])(\d+)\..+$/);
if (!pageMatch) {
return "";
}
let [, bookKey, page] = pageMatch;
return BOOK_ORIG_INFO[bookKey] // static original publication info
+ "\nOriginal Page: " + page.replace(/^0*/, ""); // trim leading zero
}
// Normalize the lemma's vowel display-form, following the original book's
// orthography (acute for long vowel).
// Why is this necessary? Because the lemma prominently displayed on the page
// main body can be configured by the user (by clicking on the icons: acute,
// macron, and none). But we want the form used in our item data normalized, no
// matter the display option, in order to not lose information and avoid
// duplication.
// NOTE that the letter case is not normalized -- the display on the page
// corresponds to the lemma in the original book.
function normalizeLemma(doc) {
// The key is to apply the correct vowel length even if the user disables
// its display. This "canonical" form (which corresponds to the original
// form in the print book) can be found either in the metadata or in the
// "citation" block under "entry information" in the doc, no matter the
// citation style in use on the page.
let titleRaw = text(doc, "#btd--entry-lemma").trim();
let titleNormalized = removeDiacritics(titleRaw);
// lemma from the meta field, not normally amenable to client-side
// modification
let metaTitle = attr(doc, 'meta[property="og:title"]', "content").trim();
let metaTitleNormalized = removeDiacritics(metaTitle);
if (metaTitle && titleNormalized === metaTitleNormalized) {
return metaTitle;
}
return null;
}
// Utility functions
// Remove the acute accent and macron if any.
function removeDiacritics(str) {
return str.normalize("NFD").replace(/[\u0301\u0304]/g, "");
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://bosworthtoller.com/search?q=heorte",
"defer": true,
"items": "multiple"
},
{
"type": "web",
"url": "https://bosworthtoller.com/search/advanced?q=%7B%22minFields%22%3A1,%22fields%22%3A%5B%7B%22query%22%3A%22diacon%22,%22field%22%3A%22headword%22,%22is_regex%22%3Afalse%7D,%7B%22query%22%3A%22%22,%22field%22%3A%22headword%22,%22condition%22%3A%22and%22,%22is_regex%22%3Afalse%7D%5D,%22wordclass%22%3A%7B%22include%22%3A%5B%221%22%5D,%22exclude%22%3A%5B%5D%7D,%22gender%22%3A%7B%22include%22%3A%5B%221%22%5D,%22exclude%22%3A%5B%5D%7D,%22subcategory%22%3A%7B%22include%22%3A%5B%5D,%22exclude%22%3A%5B%5D%7D,%22volume%22%3Anull%7D",
"defer": true,
"items": "multiple"
},
{
"type": "web",
"url": "https://bosworthtoller.com/23205",
"items": [
{
"itemType": "dictionaryEntry",
"title": "mucg-wyrt",
"creators": [
{
"firstName": "Joseph",
"lastName": "Bosworth",
"creatorType": "author"
},
{
"firstName": "Thomas Northcote",
"lastName": "Toller",
"creatorType": "editor"
},
{
"firstName": "Christ",
"lastName": "Sean",
"creatorType": "editor"
},
{
"firstName": "Ondřej",
"lastName": "Tichy",
"creatorType": "editor"
}
],
"date": "2014",
"dictionaryTitle": "An Anglo-Saxon Dictionary Online",
"extra": "Original Dictionary Title: An Anglo-Saxon Dictionary, Based on the Manuscript Collections of the Late Joseph Bosworth, D.D., F.R.S.\nOriginal Date: 1898\nOriginal Publisher: Oxford University Press\nOriginal Place: London\nOriginal Page: 700",
"language": "en",
"libraryCatalog": "Bosworth Toller's Anglo-Saxon Dictionary Online",
"place": "Prague",
"publisher": "Faculty of Arts, Charles University",
"url": "https://bosworthtoller.com/23205",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://bosworthtoller.com/7096",
"items": [
{
"itemType": "dictionaryEntry",
"title": "CYN",
"creators": [
{
"firstName": "Joseph",
"lastName": "Bosworth",
"creatorType": "author"
},
{
"firstName": "Thomas Northcote",
"lastName": "Toller",
"creatorType": "editor"
},
{
"firstName": "Christ",
"lastName": "Sean",
"creatorType": "editor"
},
{
"firstName": "Ondřej",
"lastName": "Tichy",
"creatorType": "editor"
}
],
"date": "2014",
"dictionaryTitle": "An Anglo-Saxon Dictionary Online",
"extra": "Original Dictionary Title: An Anglo-Saxon Dictionary, Based on the Manuscript Collections of the Late Joseph Bosworth, D.D., F.R.S.\nOriginal Date: 1898\nOriginal Publisher: Oxford University Press\nOriginal Place: London\nOriginal Page: 183",
"language": "en",
"libraryCatalog": "Bosworth Toller's Anglo-Saxon Dictionary Online",
"place": "Prague",
"publisher": "Faculty of Arts, Charles University",
"url": "https://bosworthtoller.com/7096",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://bosworthtoller.com/27305",
"items": [
{
"itemType": "dictionaryEntry",
"title": "secgan",
"creators": [
{
"firstName": "Joseph",
"lastName": "Bosworth",
"creatorType": "author"
},
{
"firstName": "Thomas Northcote",
"lastName": "Toller",
"creatorType": "editor"
},
{
"firstName": "Christ",
"lastName": "Sean",
"creatorType": "editor"
},
{
"firstName": "Ondřej",
"lastName": "Tichy",
"creatorType": "editor"
}
],
"date": "2014",
"dictionaryTitle": "An Anglo-Saxon Dictionary Online",
"extra": "Original Dictionary Title: An Anglo-Saxon Dictionary, Based on the Manuscript Collections of the Late Joseph Bosworth, D.D., F.R.S.\nOriginal Date: 1898\nOriginal Publisher: Oxford University Press\nOriginal Place: London\nOriginal Page: 855",
"language": "en",
"libraryCatalog": "Bosworth Toller's Anglo-Saxon Dictionary Online",
"place": "Prague",
"publisher": "Faculty of Arts, Charles University",
"url": "https://bosworthtoller.com/27305",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://bosworthtoller.com/23035",
"items": [
{
"itemType": "dictionaryEntry",
"title": "mód-c-wánig",
"creators": [
{
"firstName": "Joseph",
"lastName": "Bosworth",
"creatorType": "author"
},
{
"firstName": "Thomas Northcote",
"lastName": "Toller",
"creatorType": "editor"
},
{
"firstName": "Christ",
"lastName": "Sean",
"creatorType": "editor"
},
{
"firstName": "Ondřej",
"lastName": "Tichy",
"creatorType": "editor"
}
],
"date": "2014",
"dictionaryTitle": "An Anglo-Saxon Dictionary Online",
"extra": "Original Dictionary Title: An Anglo-Saxon Dictionary, Based on the Manuscript Collections of the Late Joseph Bosworth, D.D., F.R.S.\nOriginal Date: 1898\nOriginal Publisher: Oxford University Press\nOriginal Place: London\nOriginal Page: 694",
"language": "en",
"libraryCatalog": "Bosworth Toller's Anglo-Saxon Dictionary Online",
"place": "Prague",
"publisher": "Faculty of Arts, Charles University",
"url": "https://bosworthtoller.com/23035",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://bosworthtoller.com/53107",
"items": [
{
"itemType": "dictionaryEntry",
"title": "hrǽw",
"creators": [
{
"firstName": "Joseph",
"lastName": "Bosworth",
"creatorType": "author"
},
{
"firstName": "Thomas Northcote",
"lastName": "Toller",
"creatorType": "editor"
},
{
"firstName": "Christ",
"lastName": "Sean",
"creatorType": "editor"
},
{
"firstName": "Ondřej",
"lastName": "Tichy",
"creatorType": "editor"
}
],
"date": "2014",
"dictionaryTitle": "An Anglo-Saxon Dictionary Online",
"extra": "Original Dictionary Title: An Anglo-Saxon Dictionary, Based on the Manuscript Collections of the Late Joseph Bosworth; Supplement\nOriginal Date: 1921\nOriginal Publisher: Oxford University Press\nOriginal Place: London\nOriginal Page: 562",
"language": "en",
"libraryCatalog": "Bosworth Toller's Anglo-Saxon Dictionary Online",
"place": "Prague",
"publisher": "Faculty of Arts, Charles University",
"url": "https://bosworthtoller.com/53107",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://bosworthtoller.com/42878",
"items": [
{
"itemType": "dictionaryEntry",
"title": "dón",
"creators": [
{
"firstName": "Joseph",
"lastName": "Bosworth",
"creatorType": "author"
},
{
"firstName": "Thomas Northcote",
"lastName": "Toller",
"creatorType": "editor"
},
{
"firstName": "Christ",
"lastName": "Sean",
"creatorType": "editor"
},
{
"firstName": "Ondřej",
"lastName": "Tichy",
"creatorType": "editor"
}
],
"date": "2014",
"dictionaryTitle": "An Anglo-Saxon Dictionary Online",
"extra": "Original Dictionary Title: An Anglo-Saxon Dictionary, Based on the Manuscript Collections of the Late Joseph Bosworth; Supplement\nOriginal Date: 1921\nOriginal Publisher: Oxford University Press\nOriginal Place: London\nOriginal Page: 154",
"language": "en",
"libraryCatalog": "Bosworth Toller's Anglo-Saxon Dictionary Online",
"place": "Prague",
"publisher": "Faculty of Arts, Charles University",
"url": "https://bosworthtoller.com/42878",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/