forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BIUM.js
100 lines (95 loc) · 3.9 KB
/
BIUM.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
{
"translatorID": "cae7d3ec-bc8d-465b-974f-8b0dcfe24290",
"label": "BIUM",
"creator": "Michael Berkowitz",
"target": "http://hip.bium.univ-paris5.fr/",
"minVersion": "1.0.0b4.r5",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "g",
"lastUpdated": "2012-03-01 21:14:57"
}
function detectWeb(doc, url) {
if (doc.evaluate('//td/a[@class="itemTitle"]', doc, null, XPathResult.ANY_TYPE, null).iterateNext()) {
return "multiple";
} else if (doc.evaluate('//td[1]/span[@class="uportal-channel-strong"]', doc, null, XPathResult.ANY_TYPE, null).iterateNext()) {
return "book";
}
}
function makeMARCurl(link, rsId, rrsId, query) {
return 'http://hip.bium.univ-paris5.fr/uPortal/Print?link=' + link + '&xslFileName=com/dynix/hip/uportal/channels/standard/FullMarc.xsl&F=/searching/getmarcdata&responseSessionId=' + rsId + '&responseResultSetId=' + rrsId + '&searchGroup=BIUM-13&query=' + query + '&searchTargets=16&locale=fr_FR';
}
function doWeb(doc, url) {
var n = doc.documentElement.namespaceURI;
var ns = n ? function(prefix) {
if (prefix == 'x') return n; else return null;
} : null;
var books = new Array();
if (detectWeb(doc, url) == "multiple") {
var items = new Object();
var links = doc.evaluate('//a[@class="itemTitle"]', doc, ns, XPathResult.ANY_TYPE, null);
var link;
while (link = links.iterateNext()) {
items[link.href] = Zotero.Utilities.trimInternal(link.textContent);
}
items = Zotero.selectItems(items);
var rsId = doc.evaluate('//input[@name="responseSessionId"]', doc, ns, XPathResult.ANY_TYPE, null).iterateNext().value;
var rrsId = doc.evaluate('//input[@name="responseResultSetId"]', doc, ns, XPathResult.ANY_TYPE, null).iterateNext().value;
var query = doc.evaluate('//input[@name="query"]', doc, ns, XPathResult.ANY_TYPE, null).iterateNext().value;
var linkRE = new RegExp("'([^']+)'", "g");
for (var i in items) {
var link = linkRE.exec(i)[1];
Zotero.debug(link);
books.push(makeMARCurl(link, rsId, rrsId, query));
}
} else {
var link = url.match(/link=([^&]+)/)[1];
var rsId = url.match(/responseSessionId=([^&]+)/)[1];
var rrsId = url.match(/responseResultSetId=([^&]+)/)[1];
var query = url.match(/query=([^&]+)/)[1];
books = [makeMARCurl(link, rsId, rrsId, query)];
}
var translator = Zotero.loadTranslator("import");
translator.setTranslator("a6ee60df-1ddc-4aae-bb25-45e0537be973");
var marc = translator.getTranslatorObject();
Zotero.Utilities.processDocuments(books, function(doc) {
var rows = doc.evaluate('//center/table/tbody/tr', doc, ns, XPathResult.ANY_TYPE, null);
var row;
var record = new marc.record();
while (row = rows.iterateNext()) {
var field = Zotero.Utilities.trimInternal(doc.evaluate('./td[1]', row, ns, XPathResult.ANY_TYPE, null).iterateNext().textContent.replace(":", ""));
if (field) {
var value = doc.evaluate('./td[2]', row, ns, XPathResult.ANY_TYPE, null).iterateNext().textContent;
if (value.split(/\n/)[1]) value = Zotero.Utilities.trimInternal(value.split(/\n/)[1]);
if (field == "LDR") {
record.leader = value;
} else if (field != "FMT") {
value = value.replace(/\¤([a-z])/g, marc.subfieldDelimiter+ "$1");
var code = field.substring(0, 3);
var ind = "";
if (field.length > 3) {
ind = field[3];
if (field.length > 4) {
ind += field[4];
}
}
record.addField(code, ind, value);
}
}
}
var item = new Zotero.Item();
record.translate(item);
var oldauthors = item.creators;
var newauthors = new Array();
for each (var aut in oldauthors) {
if (aut.lastName.match(/^[A-Z][^\s]+\s[^\s]+/)) newauthors.push(Zotero.Utilities.cleanAuthor(aut.lastName.match(/^[A-Z][^\s]+\s[^\s]+/)[0].replace(/^([^\s]+)\s+(.*)$/, "$2 $1"), "author"));
}
item.creators = newauthors;
item.complete();
}, function() {Zotero.done();});
Zotero.wait();
}/** BEGIN TEST CASES **/
var testCases = []
/** END TEST CASES **/