forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRePEc.js
181 lines (125 loc) · 4.68 KB
/
RePEc.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
{
"translatorID":"411f9a8b-64f3-4465-b7df-a3c988b602f3",
"translatorType":4,
"label":"RePEc",
"creator":"Asa Kusuma",
"target":"^https?://ideas\\.repec\\.org/",
"minVersion":"1.0.0b4.r1",
"maxVersion":"",
"priority":100,
"inRepository":true,
"lastUpdated":"2011-01-11 04:31:00"
}
function detectWeb(doc, url) {
var namespace = doc.documentElement.namespaceURI;
var nsResolver = namespace ? function(prefix) {
if (prefix == 'x') return namespace; else return null;
} : null;
var singXpath = '//html/body/a/table/tbody/tr/td/font/b';
var multXpath = '//html/body/h2';
if (doc.evaluate(multXpath, doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()) {
if(doc.evaluate(multXpath, doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent.indexOf("Search")!=-1)
return "multiple";
} else if(doc.evaluate(singXpath, doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()) {
return "journalArticle";
}
}
function strrev(str) {
if (!str) return '';
var revstr='';
for (i = str.length-1; i>=0; i--)
revstr+=str.charAt(i)
return revstr;
}
function parseRIS(uris) {
Zotero.Utilities.HTTP.doGet(uris, function(text){
// load translator for RIS
var translator = Zotero.loadTranslator("import");
translator.setTranslator("32d59d2d-b65a-4da4-b0a3-bdd3cfb979e7");
translator.setString(text);
translator.translate();
Zotero.done();
}, function() {});
Zotero.wait();
}
function doWeb(doc, url) {
var namespace = doc.documentElement.namespaceURI;
var nsResolver = namespace ? function(prefix) {
if (prefix == 'x') return namespace; else return null;
} : null;
var singXpath = '//html/body/a/table/tbody/tr/td/font/b';
var multXpath = '//html/body/h2';
if (doc.evaluate(multXpath, doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()) {
if(doc.evaluate(multXpath, doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent.indexOf("Search")!=-1)
shortXpath = '//html/body/strong/a';
longXpath = '//html/body/dl/dt/strong/a';
var multXpath='';
if(doc.evaluate(shortXpath, doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()) {
multXpath=shortXpath;
} else {
multXpath=longXpath;
}
var bibElmts = doc.evaluate(multXpath, doc, nsResolver, XPathResult.ANY_TYPE, null);
var titleElmts = doc.evaluate(multXpath, doc, nsResolver, XPathResult.ANY_TYPE, null);
var titleElmt;
var bibElmt;
bibElmt = bibElmts.iterateNext();
titleElmt = titleElmts.iterateNext();
var items = new Array();
do {
var bibcode = bibElmt.href;
bibcode=bibcode.substr(24);
bibcode=strrev(bibcode);
bibcode=bibcode.substr(5,bibcode.length);
bibcode=strrev(bibcode);
//Replace slashes with colons
bibcode=bibcode.replace("/",":","g");
//Insert colons between numbers and letters and letters and numbers
bibcode=bibcode.replace(/([A-Za-z])([0-9])/g,
function (str, p1, p2, offset, s) {
return p1 + ":" + p2;
}
)
bibcode=bibcode.replace(/([0-9])([A-Za-z])/g,
function (str, p1, p2, offset, s) {
return p1 + ":" + p2;
}
)
items[bibcode] = Zotero.Utilities.trimInternal(titleElmt.textContent);
} while((bibElmt = bibElmts.iterateNext()) && (titleElmt = titleElmts.iterateNext()));
items = Zotero.selectItems(items);
if(!items) return true;
var bibcodes="";
var uris = new Array();
for(var bibcode in items) {
var getURL = "http://ideas.repec.org/cgi-bin/ref.cgi?handle=RePEc";
getURL = getURL + bibcode + "&output=3";
uris.push(getURL);
}
parseRIS(uris);
} else if(doc.evaluate(singXpath, doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()) {
var bibcode = url;
bibcode=bibcode.substr(24);
bibcode=strrev(bibcode);
bibcode=bibcode.substr(5,bibcode.length);
bibcode=strrev(bibcode);
//Replace slashes with colons
bibcode=bibcode.replace("/",":","g");
//Insert colons between numbers and letters and letters and numbers
bibcode=bibcode.replace(/([A-Za-z])([0-9])/g,
function (str, p1, p2, offset, s) {
return p1 + ":" + p2;
}
)
bibcode=bibcode.replace(/([0-9])([A-Za-z])/g,
function (str, p1, p2, offset, s) {
return p1 + ":" + p2;
}
)
var getURL = "http://ideas.repec.org/cgi-bin/ref.cgi?handle=RePEc";
getURL = getURL + bibcode + "&output=3";
var idarray = new Array();
idarray.push(getURL);
parseRIS(idarray);
}
}