forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CSL JSON.js
65 lines (57 loc) · 1.97 KB
/
CSL JSON.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
{
"translatorID": "bc03b4fe-436d-4a1f-ba59-de4d2d7a63f7",
"label": "CSL JSON",
"creator": "Simon Kornblith",
"target": "json",
"minVersion": "3.0b3",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 3,
"browserSupport": "gcs",
"lastUpdated": "2011-09-25 20:49:56"
}
var parsedData;
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, "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};
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 {
parsedData = JSON.parse(json);
} catch(e) {
Zotero.debug(e);
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];
if(typeof item !== "object" || !item.type || !(item.type in CSL_TYPES)) {
return false;
}
}
return true;
}
function doImport() {
for(var i=0; i<parsedData.length; i++) {
var item = new Z.Item();
ZU.itemFromCSLJSON(item, parsedData[i]);
item.complete();
}
}
function doExport() {
var item, data = [];
while(item = Z.nextItem()) data.push(ZU.itemToCSLJSON(item));
Z.write(JSON.stringify(data, null, "\t"));
}