Skip to content

Instantly share code, notes, and snippets.

@retorquere
Created October 5, 2022 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retorquere/79efd5b72c180405a7473466dc5d9c9b to your computer and use it in GitHub Desktop.
Save retorquere/79efd5b72c180405a7473466dc5d9c9b to your computer and use it in GitHub Desktop.
{
"translatorID": "86ffd88b-6f4e-4bec-a5be-839c1034beb3",
"label": "Collections",
"description": "Export collections",
"creator": "Emiliano Heyns",
"target": "html",
"minVersion": "4.0.27",
"maxVersion": "",
"configOptions": {
"getCollections": true
},
"translatorType": 2,
"browserSupport": "gcsv",
"priority": 100,
"inRepository": false,
"lastUpdated": "2021-12-30 13:05:38"
}
class Collections {
constructor() {
this.collections = {}
let collection
let key
while (collection = Zotero.nextCollection()) {
this.registerCollection(collection, '')
}
for (const collection of Object.values(this.collections)) {
if (!this.collections[collection.parent]) delete collection.parent
}
}
root() {
return Object.values(this.collections).filter(coll => !coll.parent).map(coll => coll.key)
}
registerCollection(collection, parent) {
const key = (collection.primary ? collection.primary : collection).key
if (this.collections[key]) return // why does JM send collections twice?!
this.collections[key] = {
key,
parent,
name: collection.name,
collections: [],
}
for (const child of (collection.descendents || collection.children)) {
switch (child.type) {
case 'collection':
this.collections[key].collections.push(child.key)
this.registerCollection(child, key)
break
}
}
}
html(str) {
return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')
}
write(coll) {
if (!coll) coll = { collections: this.root() }
if (coll.name) Zotero.write(this.html(coll.name))
if (coll.collections.length) {
Zotero.write('<ul>')
for (const key of coll.collections) {
Zotero.write('<li>')
this.write(Collections.collections[key])
Zotero.write('</li>')
}
Zotero.write('</ul>')
}
}
}
function doExport() {
Zotero.write('<html><body>')
(new Collections).write()
Zotero.write('</body></html>')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment