forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSV.js
180 lines (170 loc) · 5.55 KB
/
CSV.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
{
"translatorID": "25f4c5e2-d790-4daa-a667-797619c7e2f2",
"label": "CSV",
"creator": "Philipp Zumstein and Aurimas Vinckevicius",
"target": "csv",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"displayOptions": {
"exportCharset": "UTF-8xBOM",
"exportNotes": false
},
"inRepository": true,
"translatorType": 2,
"browserSupport": "g",
"lastUpdated": "2014-09-10 22:58:51"
}
//The export will be stuck if you try to export to a csv-file
//which is already opend with Excel. Thus, close it before or rename
//the new csv-file.
var recordDelimiter = "\n",
fieldDelimiter = ",",
fieldWrapperCharacter = '"',
replaceNewlinesWith = " ", // Set to `false` for no replacement
valueSeparator = "; "; // For multi-value fields, like creators, tags, etc.
// Exported columns in order of export
var exportedFields = [
// "Important" metadata
"key","itemType","publicationYear","creators/author","title",
"publicationTitle","ISBN","ISSN","DOI","url","abstractNote","date",
"dateAdded","dateModified",
// Other common fields
"accessDate","pages","numPages","issue","volume","numberOfVolumes",
"journalAbbreviation","shortTitle","series","seriesNumber","seriesText",
"seriesTitle","publisher","place","language","rights","type","archive",
"archiveLocation","libraryCatalog","callNumber","extra","notes",
"attachments/path","attachments/url","tags/own","tags/automatic",
// Creators
"creators/editor","creators/seriesEditor","creators/translator",
"creators/contributor","creators/attorneyAgent","creators/bookAuthor",
"creators/castMember","creators/commenter","creators/composer",
"creators/cosponsor","creators/counsel","creators/interviewer",
"creators/producer","creators/recipient","creators/reviewedAuthor",
"creators/scriptwriter","creators/wordsBy","creators/guest",
// Other fields
"number","edition","runningTime","scale","medium","artworkSize",
"filingDate","applicationNumber","assignee","issuingAuthority","country",
"meetingName","conferenceName","court","references","reporter",
"legalStatus","priorityNumbers","programmingLanguage","version","system",
"code","codeNumber","section","session","committee","history",
"legislativeBody"
];
// Creators that should map to base type
var creatorBaseTypes = {
interviewee: 'author',
director: 'author',
artist: 'author',
sponsor: 'author',
contributor: 'author',
inventor: 'auhtor',
cartographer: 'author',
performer: 'author',
presenter: 'author',
director: 'author',
podcaster: 'author',
programmer: 'author'
};
var exportNotes;
function doExport() {
exportNotes = Zotero.getOption("exportNotes");
// Until we fix UTF-8xBOM export, we'll write the BOM manually
Zotero.write("\uFEFF");
writeColumnHeaders();
var item, line;
while (item = Zotero.nextItem()) {
line = '';
for (var i=0; i<exportedFields.length; i++) {
line += (i ? fieldDelimiter : recordDelimiter)
+ getValue(item, exportedFields[i]);
}
Zotero.write(line);
}
}
var escapeRE = new RegExp(fieldWrapperCharacter, 'g');
function escapeValue(str) {
if (typeof replaceNewlinesWith == 'string') {
str = str.replace(/[\r\n]+/g, replaceNewlinesWith);
}
return str.replace(escapeRE, fieldWrapperCharacter + '$&');
}
function writeColumnHeaders() {
var line = '';
for (var i=0; i<exportedFields.length; i++) {
line += (i ? fieldDelimiter : '') + fieldWrapperCharacter;
var label = exportedFields[i].split('/');
switch (label[0]) {
case 'creators':
label = label[1];
break;
case 'tags':
label = ( label[1] == 'own' ? 'Manual Tags' : 'Automatic Tags');
break;
case 'attachments':
label = (label[1] == 'url' ? 'Link Attachments' : 'File Attachments');
break;
default:
label = label[0];
}
// Split individual words in labels and capitalize property
label = label[0].toUpperCase() + label.substr(1);
label = label.replace(/([a-z])([A-Z])/g, '$1 $2');
line += escapeValue(label) + fieldWrapperCharacter;
}
Zotero.write(line);
}
function getValue(item, field) {
var split = field.split('/'), value = fieldWrapperCharacter;
switch (split[0]) {
case 'publicationYear':
if (item.date) {
var date = ZU.strToDate(item.date);
if (date.year) value += escapeValue(date.year);
}
break;
case 'creators':
var creators = [];
for (var i=0; i<item.creators.length; i++) {
var creator = item.creators[i];
var baseCreator = creatorBaseTypes[creator.creatorType];
if (creator.creatorType != split[1] && baseCreator !== split[1]) {
continue;
}
creators.push(creator.lastName
+ (creator.firstName ? ', ' + creator.firstName : ''));
}
value += escapeValue(creators.join(valueSeparator));
break;
case 'tags':
var tags = [], tagType = split[1] == 'automatic';
for (var i=0; i<item.tags.length; i++) {
if (item.tags[i].type == tagType) tags.push(item.tags[i].tag);
}
value += escapeValue(tags.join(valueSeparator));
break;
case 'attachments':
var paths = [];
for (var i=0; i<item.attachments.length; i++) {
if (split[1] == 'path') {
paths.push(item.attachments[i].localPath);
} else if (split[1] == 'url' && !item.attachments[i].localPath) {
paths.push(item.attachments[i].url);
}
}
value += escapeValue(paths.join(valueSeparator));
break;
case 'notes':
if (!exportNotes) break;
var notes = [];
for (var i=0; i<item.notes.length; i++) {
notes.push(item.notes[i].note);
}
value += escapeValue(notes.join(valueSeparator));
break;
default:
if (item[field] || item.uniqueFields[field]) {
value += escapeValue('' + (item[field] || item.uniqueFields[field]));
}
}
return value + fieldWrapperCharacter;
}