forked from V-A-collaboration/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSL JSON.js
More file actions
243 lines (224 loc) · 7.19 KB
/
CSL JSON.js
File metadata and controls
243 lines (224 loc) · 7.19 KB
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
{
"translatorID": "bc03b4fe-436d-4a1f-ba59-de4d2d7a63f7",
"label": "CSL JSON",
"creator": "Simon Kornblith",
"target": "json",
"minVersion": "4.0.27",
"maxVersion": "",
"priority": 100,
"configOptions": {
"async": true
},
"inRepository": true,
"translatorType": 3,
"browserSupport": "gcsibv",
"lastUpdated": "2020-01-31 00:12:00"
}
var mimeTypes = {
"PDF": "application/pdf",
"DOC": "application/msword",
"DOCX": "application/msword",
"HTML": "text/html",
"HTM": "text/html",
"TXT": "text/plain",
"DEFAULT": "application/octet-stream"
};
var mimeRex = new RegExp("(" + Object.keys(mimeTypes).join("|") + ")$", "i");
function getMimeType(str) {
var mimeKey = "DEFAULT";
var m = mimeRex.exec(str);
if (m) {
mimeKey = m[1].toUpperCase();
}
return mimeTypes[mimeKey];
}
function parseInput() {
var str, json = "";
// Read in the whole file at once, since we can't easily parse a JSON stream. The
// chunk size here is pretty arbitrary, although larger chunk sizes may be marginally
// faster. We set it to 1MB.
while ((str = Z.read(1048576)) !== false) json += str;
try {
return JSON.parse(json);
} catch(e) {
Zotero.debug(e);
}
}
function detectImport() {
const CSL_TYPES = {"article":true, "article-journal":true, "article-magazine":true,
"article-newspaper":true, "bill":true, "book":true, "broadcast":true,
"chapter":true, "dataset":true, "entry":true, "entry-dictionary":true,
"entry-encyclopedia":true, "figure":true, "graphic":true, "interview":true,
"legal_case":true, "legal_commentary": true, "legislation":true, "manuscript":true, "map":true,
"motion_picture":true, "musical_score":true, "pamphlet":true,
"paper-conference":true, "patent":true, "personal_communication":true,
"post":true, "post-weblog":true, "report":true, "review":true, "review-book":true,
"song":true, "speech":true, "thesis":true, "treaty":true, "webpage":true,
"gazette":true, "regulation":true, "classic":true, "standard":true, "hearing":true, "video":true};
var parsedData = parseInput();
if (!parsedData) return false;
if (typeof parsedData !== "object") return false;
if (!(parsedData instanceof Array)) parsedData = [parsedData];
for (var i=0; i<parsedData.length; i++) {
var item = parsedData[i];
// second argument is for "strict"
if (typeof item !== "object" || !item.type || !CSL_TYPES[item.type]) {
return false;
}
}
return true;
}
function doImport() {
if (typeof Promise == 'undefined') {
startImport(
function () {},
function (e) {
throw e;
}
);
}
else {
return new Promise(function (resolve, reject) {
startImport(resolve, reject);
});
}
}
function startImport(resolve, reject) {
try {
var parsedData = parseInput();
if (!parsedData) resolve();
if (!Array.isArray(parsedData)) parsedData = [parsedData];
importNext(parsedData, resolve, reject);
}
catch (e) {
reject (e);
}
}
function importNext(data, resolve, reject) {
try {
var d;
while (d = data.shift()) {
// Handle legacy CSL JSON exports
if (d.journalAbbreviation) {
d["container-title-short"] = d.journalAbbreviation;
delete d.journalAbbreviation;
}
var item = new Z.Item();
// Default to 'article' (Document) if no type given. 'type' is required in CSL-JSON,
// but some DOI registration agencies provide bad data, and this is better than failing.
// (itemFromCSLJSON() will already default to 'article' for unknown 'type' values.)
//
// Technically this should go in the DOI Content Negotation translator, but it's easier
// to do this here after the JSON has been parsed, and it might benefit other translators.
//
// This is just for imports from other translators. File/clipboard imports without
// 'type' still won't work, because a valid 'type' is required in detectImport().
//
// https://forums.zotero.org/discussion/85273/error-importing-dois-via-add-item-by-identifier
if (!d.type) {
d.type = 'article';
}
ZU.itemFromCSLJSON(item, d);
item.attachments = [];
item.tags = [];
if (d.attachments && d.attachments.length) {
for (var att of d.attachments) {
var title = null, path = null, note = null, tags = [];
if (typeof att === "string") {
title = att.replace(/^.*\//, "");
path = att;
} else if (att.path) {
if (att.title) {
title = att.title;
} else {
title = att.path.replace(/^.*\//, "");
}
if (att.note) {
note = att.note;
}
path = att.path;
}
if (att.tags) {
tags = att.tags;
}
if (title && path) {
item.attachments.push({
title: title,
path: path,
tags: tags,
note: note,
mimeType: getMimeType(path)
});
}
}
}
if (d.tags) {
var tags = d.tags;
if (typeof d.tags === "string") {
tags = d.tags.split(/\s*,\s*/);
}
item.tags = tags;
}
var maybePromise = item.complete();
if (maybePromise) {
maybePromise.then(function () {
importNext(data, resolve, reject);
});
return;
}
}
}
catch (e) {
reject(e);
}
resolve();
}
function doExport() {
var item, data = [];
while (item = Z.nextItem()) {
if (item.extra) {
item.extra = item.extra.replace(/(?:^|\n)citation key\s*:\s*([^\s]+)(?:\n|$)/i, (m, citationKey) => {
item.citationKey = citationKey;
return '\n';
}).trim();
}
var cslItem = ZU.itemToCSLJSON(item);
if (item.citationKey) cslItem.id = item.citationKey;
data.push(cslItem);
}
Z.write(JSON.stringify(data, null, "\t"));
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "import",
"input": "[\n\t{\n\t\t\"id\": \"http://zotero.org/users/96641/items/BDQRTS3T\",\n\t\t\"type\": \"book\",\n\t\t\"title\": \"Stochastic biomathematical models: With applications to neuronal modeling\",\n\t\t\"collection-title\": \"Lecture notes in mathematics\",\n\t\t\"publisher\": \"Springer\",\n\t\t\"publisher-place\": \"Heidelberg\",\n\t\t\"volume\": \"2058\",\n\t\t\"number-of-pages\": \"206\",\n\t\t\"event-place\": \"Heidelberg\",\n\t\t\"ISBN\": \"978-3-642-32156-6\",\n\t\t\"language\": \"en\",\n\t\t\"author\": [\n\t\t\t{\n\t\t\t\t\"family\": \"Bachar\",\n\t\t\t\t\"given\": \"Mostafa\"\n\t\t\t}\n\t\t],\n\t\t\"issued\": {\n\t\t\t\"date-parts\": [\n\t\t\t\t[\n\t\t\t\t\t\"2013\",\n\t\t\t\t\t1,\n\t\t\t\t\t1\n\t\t\t\t]\n\t\t\t]\n\t\t}\n\t}\n]",
"items": [
{
"itemType": "book",
"title": "Stochastic biomathematical models: With applications to neuronal modeling",
"creators": [
{
"lastName": "Bachar",
"firstName": "Mostafa",
"creatorType": "author"
}
],
"date": "January 1, 2013",
"ISBN": "978-3-642-32156-6",
"itemID": "http://zotero.org/users/96641/items/BDQRTS3T",
"language": "en",
"numPages": "206",
"place": "Heidelberg",
"publisher": "Springer",
"series": "Lecture notes in mathematics",
"volume": "2058",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/