-
Notifications
You must be signed in to change notification settings - Fork 787
/
Copy pathReferBibIX.js
463 lines (442 loc) · 11.8 KB
/
ReferBibIX.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
{
"translatorID": "881f60f2-0802-411a-9228-ce5f47b64c7d",
"label": "Refer/BibIX",
"creator": "Simon Kornblith",
"target": "txt",
"minVersion": "2.1",
"maxVersion": "",
"priority": 100,
"displayOptions": {
"exportCharset": "UTF-8"
},
"inRepository": true,
"translatorType": 3,
"lastUpdated": "2023-10-27 09:03:42"
}
function detectImport() {
var lineRe = /%[A-Z0-9*$] .+/;
var line;
var matched = 0;
while ((line = Zotero.read()) !== false) {
line = line.replace(/^\s+/, "");
if (line != "") {
if (lineRe.test(line)) {
matched++;
if (matched == 2) {
// threshold is two lines
return true;
}
}
else {
return false;
}
}
}
return false;
}
var fieldMap = {
T: "title",
S: "series",
V: "volume",
N: "issue",
C: "place",
I: "publisher",
R: "type",
P: "pages",
W: "archiveLocation",
"*": "rights",
"@": "ISBN",
L: "callNumber",
M: "accessionNumber",
U: "url",
7: "edition",
X: "abstractNote",
G: "language"
};
var inputFieldMap = {
J: "publicationTitle",
B: "publicationTitle",
9: "type"
};
// TODO: figure out if these are the best types for personal communication
var typeMap = {
book: "Book",
bookSection: "Book Section",
journalArticle: "Journal Article",
magazineArticle: "Magazine Article",
newspaperArticle: "Newspaper Article",
thesis: "Thesis",
letter: "Personal Communication",
manuscript: "Unpublished Work",
interview: "Personal Communication",
film: "Film or Broadcast",
artwork: "Artwork",
webpage: "Web Page",
report: "Report",
bill: "Bill",
case: "Case",
hearing: "Hearing",
patent: "Patent",
statute: "Statute",
email: "Personal Communication",
map: "Map",
blogPost: "Web Page",
instantMessage: "Personal Communication",
forumPost: "Web Page",
audioRecording: "Audiovisual Material",
presentation: "Report",
videoRecording: "Audiovisual Material",
tvBroadcast: "Film or Broadcast",
radioBroadcast: "Film or Broadcast",
podcast: "Audiovisual Material",
computerProgram: "Computer Program",
conferencePaper: "Conference Paper",
document: "Generic",
encyclopediaArticle: "Encyclopedia",
dictionaryEntry: "Dictionary"
};
// supplements outputTypeMap for importing
// TODO: BILL, CASE, COMP, CONF, DATA, HEAR, MUSIC, PAT, SOUND, STAT
var inputTypeMap = {
"Ancient Text": "book",
Audio: "audioRecording",
"Audiovisual Material": "videoRecording",
Generic: "book",
"Chart or Table": "artwork",
"Classical Work": "book",
"Conference Proceedings": "conferencePaper",
"Conference Paper": "conferencePaper",
"Edited Book": "book",
"Electronic Article": "journalArticle",
"Electronic Book": "book",
Equation: "artwork",
Figure: "artwork",
"Government Document": "document",
Grant: "document",
"Legal Rule or Regulation": "statute",
"Online Database": "webpage",
"Online Multimedia": "webpage",
"Electronic Source": "webpage"
};
function processTag(item, tag, value) {
value = Zotero.Utilities.trim(value);
if (fieldMap[tag]) {
if (item[fieldMap[tag]]) {
item[fieldMap[tag]] += ", " + value;
}
else {
item[fieldMap[tag]] = value;
}
}
else if (inputFieldMap[tag]) {
item[inputFieldMap[tag]] = value;
}
else if (tag == "0") {
if (inputTypeMap[value]) { // first check inputTypeMap
item.itemType = inputTypeMap[value];
}
else { // then check typeMap
for (var i in typeMap) {
if (value == typeMap[i]) {
item.itemType = i;
break;
}
}
}
}
else if (tag == "A" || tag == "E" || tag == "Y") {
var type;
if (tag == "A") {
type = "author";
}
else if (tag == "E") {
type = "editor";
}
else if (tag == "Y") {
type = "translator";
}
item.creators.push(Zotero.Utilities.cleanAuthor(value, type, value.includes(",")));
}
else if (tag == "Q") {
item.creators.push({ creatorType: "author", lastName: value, fieldMode: true });
}
else if (tag == "H" || tag == "O") {
if (!item.extra) item.extra = '';
else item.extra += "\n";
item.extra += value;
}
else if (tag == "Z") {
item.notes.push({ note: value });
}
else if (tag == "D") {
if (item.date) {
if (!item.date.includes(value)) {
item.date += " " + value;
}
}
else {
item.date = value;
}
}
else if (tag == "8") {
if (item.date) {
if (!value.includes(item.date)) {
item.date += " " + value;
}
}
else {
item.date = value;
}
}
else if (tag == "K") {
if (!item.tags || item.tags.length == 0) {
item.tags = [];
}
item.tags.push(...value.split("\n"));
}
}
function doImport() {
var line = true;
var tag = data = false;
do { // first valid line is type
if (line !== true) Zotero.debug("ignoring " + line);
line = Zotero.read();
line = line.replace(/^\s+/, "");
} while (line !== false && line[0] != "%");
// default to creating a book if no %0 tag is eventually found
var item = new Zotero.Item(inputTypeMap.Generic);
tag = line[1];
var data = line.substr(3);
while ((line = Zotero.read()) !== false) { // until EOF
line = line.replace(/^\s+/, "");
if (!line) {
if (tag) {
processTag(item, tag, data);
// unset info
tag = data = false;
// new item
item.complete();
item = new Zotero.Item(inputTypeMap.Generic);
}
}
else if (line[0] == "%" && line[2] == " ") {
// if this line is a tag, take a look at the previous line to map
// its tag
if (tag) {
processTag(item, tag, data);
}
// then fetch the tag and data from this line
tag = line[1];
data = line.substr(3);
}
else if (tag) {
// otherwise, assume this is data from the previous line continued
data += "\n" + line;
}
}
if (tag) { // save any unprocessed tags
processTag(item, tag, data);
item.complete();
}
}
function addTag(tag, value) {
if (value) {
Zotero.write("%" + tag + " " + value + "\r\n");
}
}
function doExport() {
var item;
while (item = Zotero.nextItem()) { // eslint-disable-line no-cond-assign
// can't store independent notes in RIS
if (item.itemType == "note" || item.itemType == "attachment") {
continue;
}
// type
addTag("0", typeMap[item.itemType] ? typeMap[item.itemType] : "Generic");
// use field map
for (let j in fieldMap) {
if (item[fieldMap[j]]) addTag(j, item[fieldMap[j]]);
}
// handle J & B tags correctly
if (item.publicationTitle) {
if (item.itemType == "journalArticle") {
addTag("J", item.publicationTitle);
}
else {
addTag("B", item.publicationTitle);
}
}
// creators
for (let j in item.creators) {
var referTag = "A";
if (item.creators[j].creatorType == "editor") {
referTag = "E";
}
else if (item.creators[j].creatorType == "translator") {
referTag = "?";
}
addTag(referTag, item.creators[j].lastName + (item.creators[j].firstName ? ", " + item.creators[j].firstName : ""));
}
// date
addTag("D", item.date);
// tags
if (item.tags) {
var keywordTag = "";
for (var i = 0; i < item.tags.length; i++) {
keywordTag += "\r\n" + item.tags[i].tag;
}
addTag("K", keywordTag.substr(2));
}
Zotero.write("\r\n");
}
}/** BEGIN TEST CASES **/
var testCases = [
{
"type": "import",
"input": "%0 Journal Article\n%A 余敏\n%A 朱江\n%A 丁照蕾\n%H Yu Min\n%H Zhu Jiang\n%H Ding Zhaolei\n%+ 中科院国家科学图书馆成都分馆,四川,成都,610041;中国科学院研究生院,北京,100049\n%+ 中科院国家科学图书馆成都分馆,四川,成都,610041\n%+ 四川大学公共管理学院,四川,成都,610064\n%T 参考文献管理工具研究\n%J 现代情报\n%J JOURNAL OF MODERN INFORMATION\n%@ 1008-0821\n%G chi\n%D 2009\n%N 2\n%V 29\n%P 94-98,93\n%K 参考文献管理 文献管理软件 学术书签网站 Zotero\n%X 介绍了参考文献管理的基本方法,对参考文献管理工具的主要功能进行了对比,最后分析了参考文献管理的趋势.\n%U http://d.wanfangdata.com.cn/Periodical_xdqb200902027.aspx\n%R 10.3969/j.issn.1008-0821.2009.02.027\n%W 北京万方数据股份有限公司",
"items": [
{
"itemType": "journalArticle",
"title": "参考文献管理工具研究",
"creators": [
{
"firstName": "",
"lastName": "余敏",
"creatorType": "author"
},
{
"firstName": "",
"lastName": "朱江",
"creatorType": "author"
},
{
"firstName": "",
"lastName": "丁照蕾",
"creatorType": "author"
}
],
"date": "2009",
"abstractNote": "介绍了参考文献管理的基本方法,对参考文献管理工具的主要功能进行了对比,最后分析了参考文献管理的趋势.",
"archiveLocation": "北京万方数据股份有限公司",
"extra": "Yu Min\nZhu Jiang\nDing Zhaolei",
"issue": "2",
"language": "chi",
"pages": "94-98,93",
"publicationTitle": "JOURNAL OF MODERN INFORMATION",
"url": "http://d.wanfangdata.com.cn/Periodical_xdqb200902027.aspx",
"volume": "29",
"attachments": [],
"tags": [
"参考文献管理 文献管理软件 学术书签网站 Zotero"
],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "import",
"input": "%0 Book\n%R oai:nb.bibsys.no:998320569524702202\n%R URN:NBN:no-nb_digibok_2016062048105\n%U https://urn.nb.no/URN:NBN:no-nb_digibok_2016062048105\n%T Usynlige byer\n%A Calvino, Italo\n%Y Aardal, Jorunn\n%E Stender Clausen, Jørgen\n%D 1982\n%G Flerspråklig\n%G Norsk (Bokmål)\n%G Italiensk\n%G Dansk\n%Z Elektronisk reproduksjon [Norge] Nasjonalbiblioteket Digital 2016-11-03\n%I Aschehoug\n%C Oslo\n%@ 8203107354\n%K Italiensk litteratur\n%K Idealbyer\n%K kjernelitteratur\n%P 173 s.",
"items": [
{
"itemType": "book",
"title": "Usynlige byer",
"creators": [
{
"firstName": "Italo",
"lastName": "Calvino",
"creatorType": "author"
},
{
"firstName": "Jorunn",
"lastName": "Aardal",
"creatorType": "translator"
},
{
"firstName": "Jørgen",
"lastName": "Stender Clausen",
"creatorType": "editor"
}
],
"date": "1982",
"ISBN": "8203107354",
"language": "Flerspråklig, Norsk (Bokmål), Italiensk, Dansk",
"place": "Oslo",
"publisher": "Aschehoug",
"url": "https://urn.nb.no/URN:NBN:no-nb_digibok_2016062048105",
"attachments": [],
"tags": [
{
"tag": "Idealbyer"
},
{
"tag": "Italiensk litteratur"
},
{
"tag": "kjernelitteratur"
}
],
"notes": [
{
"note": "Elektronisk reproduksjon [Norge] Nasjonalbiblioteket Digital 2016-11-03"
}
],
"seeAlso": []
}
]
},
{
"type": "import",
"input": "%T Corn\n%A Newman, W. H.\n%I Uniontown, Ala. : Canebrake Agricultural Experiment Station\n%D 1891\n%K Corn -- Yields\n%K Corn -- Varieties\n%K Corn -- Field experiments\n\n",
"items": [
{
"itemType": "book",
"title": "Corn",
"creators": [
{
"firstName": "W. H.",
"lastName": "Newman",
"creatorType": "author"
}
],
"date": "1891",
"publisher": "Uniontown, Ala. : Canebrake Agricultural Experiment Station",
"attachments": [],
"tags": [
{
"tag": "Corn -- Field experiments"
},
{
"tag": "Corn -- Varieties"
},
{
"tag": "Corn -- Yields"
}
],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "import",
"input": "%0 Serial\n%0 Audio\n%I Studio Omega, Verein für Christliche Radioarbeit\n%D 2021\n%C Wien\n%G German\n%T Diesseits von Eden: der Podcast der katholischen Fakultäten Österreichs & Südtirols\n%U https://diesseits.theopodcast.at/home",
"items": [
{
"itemType": "audioRecording",
"title": "Diesseits von Eden: der Podcast der katholischen Fakultäten Österreichs & Südtirols",
"creators": [],
"date": "2021",
"label": "Studio Omega, Verein für Christliche Radioarbeit",
"language": "German",
"place": "Wien",
"url": "https://diesseits.theopodcast.at/home",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/