forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdLibra.js
167 lines (138 loc) · 4.48 KB
/
dLibra.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
{
"translatorID":"915e3ae2-afa9-4b1d-9780-28ed3defe0ab",
"label":"dLibra",
"creator":"Pawel Kolodziej <p.kolodziej@gmail.com>",
"target":"/.*dlibra\\/(doccontent|docmetadata|collectiondescription|results)|/dlibra/?",
"minVersion":"1.0.0b3.r1",
"maxVersion":"",
"priority":100,
"inRepository":"1",
"translatorType":4,
"lastUpdated":"2011-04-18 23:20:17"
}
/*
dLibra Translator
Copyright (C) 2010 Pawel Kolodziej, p.kolodziej@gmail.com
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/>.
*/
dd = Zotero.debug; // shortcut
function detectWeb(doc, url) {
var singleRe = /.*dlibra\/(doccontent|docmetadata|publication).*/;
var multipleRe = /.*dlibra\/(collectiondescription|results).*|.*\/dlibra\/?/;
if(singleRe.test(url))
return "document";
if(multipleRe.test(url))
return "multiple";
return "";
}
function list2txt(list)
{
var a= new Array();
for each(var i in list)
a.push(i.text());
return a.join(", ");
}
function translateType(type)
{
var types = {
"book": /.*książka.*/ ,
"journalArticle": /.*artykuł.*/
}
for (var t in types)
if (types[t].test(type))
return t;
}
function doWeb(doc, url) {
if(detectWeb(doc,url)=="multiple"){
var namespace = doc.documentElement.namespaceURI;
var nsResolver = namespace ?
function(prefix) {
if (prefix == 'x') return namespace; else return null;
}: null;
var itemsXPath = '//ol[@class="itemlist"]/li/a | //td[@class="searchhit"]/b/a';
var obj = doc.evaluate(itemsXPath, doc, nsResolver, XPathResult.ANY_TYPE, null);
var itemHtml;
var items= new Object() ;
while(itemHtml = obj.iterateNext())
items[itemHtml.href] = itemHtml.textContent;
items = Zotero.selectItems(items);
for (var itemUrl in items)
doSingleItem(itemUrl)
}else
doSingleItem(url);
return true;
}
function doSingleItem(url)
{
var reSingle= new RegExp("(.*/dlibra)/(?:doccontent|docmetadata|publication).*[?&]id=([0-9]*).*");
var m = reSingle.exec(url);
if(!m)
return "";
var baseUrl = m[1];
var id = m[2];
var isPIA = baseUrl.match("lib.pia.org.pl|cyfrowaetnografia.pl");
var rdf = Zotero.Utilities.retrieveSource( baseUrl + "/rdf.xml?type=e&id="+id);
rdf = rdf.replace(/<\?xml[^>]*\?>/, "");
var rdfXml = new XML(rdf);
rdf = new Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
dc = new Namespace("http://purl.org/dc/elements/1.1/");
var desc = rdfXml.rdf::Description
var itemType = translateType(list2txt(desc.dc::type));
if(!itemType)
{
if( isPIA )
itemType = "journalArticle";
else
var itemType = "document";
}
var item = new Zotero.Item(itemType);
item.title = list2txt(desc.dc::title);
item.rights = list2txt(desc.dc::rights)
item.publisher = list2txt(desc.dc::publisher)
var reComa = new RegExp(".*,.*");
for each(var i in desc.dc::creator){
var hasComa = new Boolean(reComa.exec(i.toString()));
item.creators.push( Zotero.Utilities.cleanAuthor(i.toString(), "author", hasComa));
}
for each(var i in desc.dc::contributor){
var hasComa = new Boolean(reComa.exec(i.toString()));
item.creators.push( Zotero.Utilities.cleanAuthor(i.toString(), "contributor", hasComa));
}
item.date = list2txt(desc.dc::date);
item.language = list2txt(desc.dc::language);
item.description = list2txt(desc.dc::description);
if(isPIA)
{ // hacks for lib.pia.org.pl
// trim title at "/" character
var stripedTitle = item.title.match("[^/]*");
if(stripedTitle)
item.title = stripedTitle[0];
d = desc.dc::description.match("([A-Za-z/ ]*)(.*)")
item.publicationTitle = d[1];
if(d[2])
{
vol = d[2].match("t\\. *([0-9-, ]*[0-9])");
//dd(d[2]);
if(vol)
item.volume = vol[1];
pages = d[2].match("s\\. *([0-9-, ]*[0-9])");
if(pages)
item.pages = pages[1];
issue = d[2].match("z\\. *([0-9-, ]*[0-9])");
if(issue)
item.issue = issue[1];
}
}
// Zotero.debug(item);
item.complete();
return item;
}