Skip to content

Instantly share code, notes, and snippets.

@retorquere
Created February 15, 2022 16:13
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/79538591388d49da10377cf7fea9eb25 to your computer and use it in GitHub Desktop.
Save retorquere/79538591388d49da10377cf7fea9eb25 to your computer and use it in GitHub Desktop.
{
"translatorID": "f4a4148e-990c-45fb-a25f-579e90dfbcd7",
"label": "BlaBla Export",
"creator": "Emiliano Heyns",
"target": "xml",
"minVersion": "5.0.0",
"maxVersion": null,
"priority": 50,
"inRepository": false,
"translatorType": 2,
"lastUpdated":"2022-02-14 21:37:13"
}
/*
BlaBla RSS export
Copyright (C) 2022 Emiliano Heyns
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class Builder {
constructor(doc, root) {
this.doc = doc
this.root = root || doc.documentElement
}
add(options) {
Zotero.debug(JSON.stringify(options))
const elem = this.root.appendChild(options.ns ? this.doc.createElementNS(options.ns, options.name) : this.doc.createElement(options.name))
if (options.text) elem.appendChild(this.doc.createTextNode(options.text))
if (options.attr) {
for (const [a, v] of Object.entries(options.attr)) {
elem.setAttribute(a, v)
}
}
return new Builder(this.doc, elem)
}
}
const link = {
PMID: function(id) {
return `https://pubmed.ncbi.nlm.nih.gov/${id}/?utm_source=Feeder&amp;utm_medium=rss&amp;utm_campaign=pubmed-2&amp;utm_content=1854v2hhZntRmhG-1DjaJDpN-iK-63HziomJhzriNGSfUxKw-J&amp;fc=20220215080302&amp;ff=20220215080401&amp;v=2.17.5`
},
PMC: function(id) {
return `https://www.ncbi.nlm.nih.gov/pmc/${id}/?utm_source=Feeder&utm_medium=rss&utm_campaign=pubmed-2&utm_content=1854v2hhZntRmhG-1DjaJDpN-iK-63HziomJhzriNGSfUxKw-J&fc=20220215080302&ff=20220215080401&v=2.17.5">`
},
DOI: function(id) {
return `href=https://doi.org/${id}`
},
}
function escape(text) {
return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#039;')
}
function doExport() {
Zotero.setCharacterSet("utf-8")
const parser = new DOMParser()
const doc = parser.parseFromString('<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" encoding="utf-8"/>', 'application/xml')
const channel = new Builder(doc).add({ name: 'channel' })
channel.add({ name: 'title', text: 'Test-App-Feed' })
channel.add({ name: 'link', text: 'https://www.BLABLA.com' })
channel.add({ name: 'description', text: 'Manueller News-Feed' })
channel.add({ name: 'docs', text: 'http://blogs.law.harvard.edu/tech/rss' })
channel.add({ name: 'pubDate', text: (new Date).toString() })
channel.add({ name: 'lastBuildDate', text: (new Date).toString() })
channel.add({ name: 'link', text: 'https://www.BLABLA.com/Test-App-Feed.xml', ns: 'http://www.w3.org/2005/Atom', attr: { rel: 'self', type: 'application/rss+xml' } })
let i
while (i = Zotero.nextItem()) {
if (i.itemType === 'attachment' || i.itemType === 'note') continue
let m
for (const line = (item.extra || '').split('\n')) {
if (m = line.match(/^(PMID|PMCID|PMC|DOI):\s*(.+)/i)) {
i[m[1].toUpperCase()] = m[2]
}
}
const item = channel.add({ name: 'item' })
item.add({ name: 'title', text: i.title })
if (i.PMID) item.add({ name: 'link', text: link.pmid(i.PMID) })
if (i.abstractNote) {
let abstractNote = '<div>'
const journal = i.journalAbbreviation) || i.publicationTitle
if (journal) {
abstractNote += '<p style="color: #4aa564;">'
abstractNote += escape(journal)
abstractNote += '</p>'
}
abstractNote += '<p><b>ABSTRACT</b></p>'
abstractNote += `<p>${escape(i.abstractNote)}</p>`
abstractNote += '<p style="color: lightgray">'
abstractNote += ['PMID', 'PMC', 'DOI'].filter(id => i[id]).map(`${id}:<a href="${escape(link[id](i[id]))}">${escape(i[id])}</a>`).join(' | ')
abstractNote += '</p>'
abstractNote += '</div>'
item.add({ name: 'description', text: abstractNote })
}
item.add({ name: 'pubDate', text: i.dateModified })
if (i.PMID) item.add({ name: 'guid', text: `pubmed:${i.PMID}`, attr: { isPermaLink: 'false' })
}
Zotero.write('<?xml version="1.0" encoding="UTF-8"?>\n')
const serializer = new XMLSerializer()
Zotero.write(serializer.serializeToString(doc))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment