forked from melat0nin/zotero-translators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Freemindv2.js
314 lines (263 loc) · 11.8 KB
/
Freemindv2.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
{
"translatorID": "43b6b1f5-d27a-4b79-a19f-4f0feb5275d7",
"label": "Freemind v2",
"creator": "Laurence Diver",
"target": "mm",
"minVersion": "4.0",
"maxVersion": "",
"priority": 50,
"configOptions": {
"getCollections": true
},
"inRepository": false,
"translatorType": 2,
"lastUpdated": "2016-10-16 11:46:00"
}
/* Set up XML */
var parser = new DOMParser();
var doc = parser.parseFromString('<map/>', 'application/xml');
//doc.removeChild(document.Element);
//var map = doc.createElement("map");
var mapVersion = doc.createAttribute("version");
mapVersion.value = "1.0.1";
doc.documentElement.setAttributeNode(mapVersion);
var rootNode = doc.createElement("node");
var collectionKeys = new Array();
/* Colours */
var green = "#007439";
var red = "#990066";
/* Get articles object */
function getArticles() {
var articles = {};
while (item = Zotero.nextItem()) {
articles[item.itemID] = {};
articles[item.itemID].collections = item.collections;
articles[item.itemID].title = item.title;
articles[item.itemID].date = (typeof item.date != 'undefined') ? new Date(item.date).getFullYear() : 'N/A';
articles[item.itemID].contributors = new Array();
articles[item.itemID].tags = new Array();
if (typeof item.creators != 'undefined' && item.creators instanceof Array) {
var contributorsArray = new Array();
for (var j = 0; j < item.creators.length; j++) {
var name = item.creators[j].lastName;
contributorsArray.push(name);
}
articles[item.itemID].contributors = contributorsArray;
}
if (typeof item.tags != 'undefined' && item.tags instanceof Array) {
var tagsArray = new Array();
for (var k = 0; k < item.tags.length; k++) {
tagsArray.push(item.tags[k].tag);
}
articles[item.itemID].tags = tagsArray;
}
}
return articles;
}
/* Get collections object */
function getCollections() {
var collections = {}, tmpCollections = {};
while(collection = Zotero.nextCollection()) { // First grab all the collections
collections[collection.primary.key] = {};
collections[collection.primary.key].parentKey = collection.fields.parentKey;
collections[collection.primary.key].title = collection.fields.name;
collections[collection.primary.key].articles = {};
collections[collection.primary.key].collections = {};
//tmpCollections[collection.primary.key] = collection;
collectionKeys.push(collection.primary.key);
}
return collections;
}
/* Generic XML node attribute creator */
Element.prototype.setAttributes = function (attrs) {
for (var idx in attrs) {
this.setAttribute(idx, attrs[idx]);
}
}
Element.prototype.addChild = function (name, attributes, children) {
var c = doc.createElement(name);
c.setAttributes(attributes);
if (typeof children != 'undefined' && children instanceof Array) { // Add any specified children
for (var i=0; i< children.length; i++) {
c.addChild(children[i].name, children[i].attrs);
}
}
this.appendChild(c);
}
function doExport() {
articles = getArticles();
collections = getCollections();
// Sort articles into their respective collection(s)
for (var key in articles) {
if (!articles.hasOwnProperty(key)) continue; // skip loop if the property is from prototype
var article = articles[key];
var artCollections = article.collections;
for(var i=0; i<artCollections.length; i++) { // Loop collection keys this article is in
var collectionKey = artCollections[i]; // Current collection key
if (typeof collections[collectionKey] != 'undefined' && collections[collectionKey] instanceof Object) {
collections[collectionKey].articles[key] = article;
}
}
}
// Nest child collections
for (var key in collections) {
if (!collections.hasOwnProperty(key)) continue; // skip loop if the property is from prototype
var collection = collections[key];
if (collectionKeys.indexOf(collection.parentKey) > -1) { // if a collection's parent isn't the root
collections[collection.parentKey].collections[key] = collection;
delete collections[key];
}
}
Zotero.write(JSON.stringify(collections));
//Zotero.write(JSON.stringify(articles));
//Zotero.write('<?xml version="1.0" encoding="UTF-8"?>\n');
//var serializer = new XMLSerializer();
//Zotero.write(serializer.serializeToString(doc));
}
function doE1xport() {
var tmpArticles = new Array();
var articles = getItems();
var collections = getCollections();
for(var i=0; i < collections.length; i++) { // Loop through parent collections
thisCollection = collections[i];
var collectionObj = doc.createElement("node");
collectionObj.setAttributes({
"text" : thisCollection.name,
"id" : thisCollection.id,
"position" : (i % 2 == 0) ? "left" : "right"
});
// Set up any child collection nodes
var descendents = thisCollection.descendents;
for (var j=0; j < descendents.length; j++) { // Loop descendents
var thisDescendent = descendents[j];
if (thisDescendent.type === "collection") { // Descendent is a collection, set up node
var childCollectionObj = doc.createElement("node");
childCollectionObj.setAttributes({
"text" : thisDescendent.name,
"id" : thisDescendent.id
});
childCollectionObj.addChild("cloud", {
"color" : "#ffffff"
});
var childItems = thisDescendent.children;
for (var k = 0; k < childItems.length; k++){ // Loop items
var thisItem = childItems[k];
if (thisItem.type === "item") { // Set up item node
var itemContributors = articles[thisItem.id].contributors, contributorsString = '';
for(var l = 0; l < itemContributors.length; l++) {
if (l == 2) { // Max 2 authors displayed
contributorsString += "... ";
break;
}
contributorsString += itemContributors[l] + ", ";
}
childCollectionObj.addChild("node", {
"text" : "[" + articles[thisItem.id].date + " " + contributorsString.substring(0, (contributorsString.length-2)) + "] " + articles[thisItem.id].title,
"id" : thisItem.id,
"color" : ""
}, [{
name : "icon",
attrs : {
}
}]);
/*
var itemObj = doc.createElement("node"); // item
var itemTitle = doc.createAttribute("text");
//var itemDate = doc.createAttribute("date");
var itemID = doc.createAttribute("id");
var itemColour = doc.createAttribute("color");
//var itemContributors = articles[thisItem.id].contributors, contributorsString = '';
var itemRead = doc.createElement("icon");
var itemReadIcon = doc.createAttribute("builtin");
itemTitle.value = "[" + articles[thisItem.id].date + " " + contributorsString.substring(0, (contributorsString.length-2)) + "] " + articles[thisItem.id].title;
itemObj.setAttributeNode(itemTitle);
//itemDate.value = articles[thisItem.id].date
//itemObj.setAttributeNode(itemDate);
itemID.value = thisItem.id;
itemObj.setAttributeNode(itemID);
// Green and red tags (core and important)
if (articles[thisItem.id].tags.indexOf("Core") > -1) {
itemColour.value = green;
} else if (articles[thisItem.id].tags.indexOf("Important") > -1) {
itemColour.value = red;
}
itemObj.setAttributeNode(itemColour);
// Read/unread icon, from tag
if (articles[thisItem.id].tags.indexOf("Unread") > -1) { // Read/unread tags
itemReadIcon.value = "help";
} else {
itemReadIcon.value = "button_ok";
}
itemRead.setAttributeNode(itemReadIcon);
itemObj.appendChild(itemRead);
*/
/* Subnodes for individual contributors -- commented out because Freemind ignores it
var itemContributors = articles[thisItem.id].contributors;
for(var l = 0; l < itemContributors.length; l++) {
var contributorObj = doc.createElement("contributor");
var contributorName = doc.createTextNode( itemContributors[l] );
contributorObj.appendChild(contributorName);
itemObj.appendChild(contributorObj);
}*/
//childCollectionObj.appendChild(itemObj); // Add item to child collection
}
}
collectionObj.appendChild(childCollectionObj); // Add child collection to parent
} /*else if (thisDescendent.type === "item") {
var itemObj = doc.createElement("node"); // item
var itemTitle = doc.createAttribute("text");
//var itemDate = doc.createAttribute("date");
var itemID = doc.createAttribute("id");
var itemColour = doc.createAttribute("color");
var itemContributors = articles[thisDescendent.id].contributors, contributorsString = '';
var itemRead = doc.createElement("icon");
var itemReadIcon = doc.createAttribute("builtin");
for(var l = 0; l < itemContributors.length; l++) {
if (l == 2) { // Max 2 authors displayed
contributorsString += "... ";
break;
}
contributorsString += itemContributors[l] + ", ";
}
itemTitle.value = "[" + articles[thisDescendent.id].date + " " + contributorsString.substring(0, (contributorsString.length-2)) + "] " + articles[thisDescendent.id].title;
itemObj.setAttributeNode(itemTitle);
//itemDate.value = articles[thisDescendent.id].date;
//itemObj.setAttributeNode(itemDate);
itemID.value = thisDescendent.id;
itemObj.setAttributeNode(itemID);
// Green and red tags (core and important)
if (articles[thisDescendent.id].tags.indexOf("Core") > -1) {
itemColour.value = green;
} else if (articles[thisDescendent.id].tags.indexOf("Important") > -1) {
itemColour.value = red;
}
itemObj.setAttributeNode(itemColour);
// Read/unread tags
if (articles[thisDescendent.id].tags.indexOf("Unread") > -1) {
itemReadIcon.value = "help";
} else {
itemReadIcon.value = "button_ok";
}
itemRead.setAttributeNode(itemReadIcon);
itemObj.appendChild(itemRead);
/* Subnodes for individual contributors -- commented out because Freemind ignores it
var itemContributors = articles[thisDescendent.id].contributors;
for(var l = 0; l < itemContributors.length; l++) {
var contributorObj = doc.createElement("contributor");
var contributorName = doc.createTextNode( itemContributors[l] );
contributorObj.appendChild(contributorName);
itemObj.appendChild(contributorObj);
}*!/
collectionObj.appendChild(itemObj); // Add item to child collection
} */
}
// TODO add items for this (parent) collection
rootNode.appendChild(collectionObj);
doc.documentElement.appendChild(rootNode);
//doc.documentElement.appendChild(collectionObj);
}
Zotero.write(JSON.stringify(articles));
//Zotero.write('<?xml version="1.0" encoding="UTF-8"?>\n');
//var serializer = new XMLSerializer();
//Zotero.write(serializer.serializeToString(doc));
}