From fb5be170575653f6e2c082fc3627e08aba71e941 Mon Sep 17 00:00:00 2001 From: Pranam Lashkari Date: Tue, 3 Jan 2023 08:29:10 +0530 Subject: [PATCH] zotero: fetch the selected citation style details fetching selected style language details will allow us to make adjustments to citation texts this is particularly helpful in handling citation numbers and citation clusters Signed-off-by: Pranam Lashkari Change-Id: Ia469e0e44fc0550c083ce572f8fe9f512947a1f4 --- browser/src/control/Control.Zotero.js | 50 +++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/browser/src/control/Control.Zotero.js b/browser/src/control/Control.Zotero.js index 38764b94e..6bca2c492 100644 --- a/browser/src/control/Control.Zotero.js +++ b/browser/src/control/Control.Zotero.js @@ -473,6 +473,54 @@ L.Control.Zotero = L.Control.extend({ app.socket.sendMessage('commandvalues command=.uno:SetDocumentProperties?namePrefix=ZOTERO_PREF_'); }, + setFetchedCitationFormat: function(style) { + if (!style) + style = this.settings.style; + + var that = this; + fetch('https://www.zotero.org/styles/' + style) + .then(function (response) { return response.text(); }) + .then(function (html) { + var csl = new DOMParser().parseFromString(html, 'text/xml'); + var categories = csl.getElementsByTagName('category'); + for (var i = 0; i < categories.length; i++) { + if (categories[i].getAttribute('citation-format')) { + that.settings.citationFormat = categories[i].getAttribute('citation-format'); + break; + } + } + + var citation = csl.getElementsByTagName('citation')[0]; + + if (citation) { + that.setCitationLayout(citation); + that.updateCitations(); + } else { + var link = csl.getElementsByTagName('link'); + for (var i = 0; i < link.length; i++) { + if (link[i].getAttribute('rel') === 'independent-parent') { + that.setFetchedCitationFormat(link[i].getAttribute('href').substring(link[i].getAttribute('href').lastIndexOf('/')+1)); + break; + } + } + } + }); + }, + + setCitationLayout: function(ciatationNode) { + var layout = ciatationNode.getElementsByTagName('layout')[0]; + this.settings.layout = {}; + this.settings.layout['prefix'] = layout && layout.getAttribute('prefix') ? layout.getAttribute('prefix') : ''; + this.settings.layout['suffix'] = layout && layout.getAttribute('suffix') ? layout.getAttribute('suffix') : ''; + this.settings.layout['delimiter'] = layout && layout.getAttribute('delimiter') ? layout.getAttribute('delimiter') : ''; + + var group = layout.getElementsByTagName('group')[0]; + this.settings.group = {}; + this.settings.group['prefix'] = group && group.getAttribute('prefix') ? group.getAttribute('prefix') : ''; + this.settings.group['suffix'] = group && group.getAttribute('suffix') ? group.getAttribute('suffix') : ''; + this.settings.group['delimiter'] = group && group.getAttribute('delimiter') ? group.getAttribute('delimiter') : ''; + }, + setFetchedStyle: function(userDefinedProperties) { var valueString = ''; for (var i = 0; i < userDefinedProperties.length; i++) { @@ -486,6 +534,7 @@ L.Control.Zotero = L.Control.extend({ var locale = styleNode.getAttribute('locale'); if (locale) this.settings.locale = locale; + this.setFetchedCitationFormat(); return; }, @@ -551,6 +600,7 @@ L.Control.Zotero = L.Control.extend({ } this.map.sendUnoCommand('.uno:SetDocumentProperties', style); + this.setFetchedCitationFormat(); }, _findEntryWithUrlImpl: function(entry, url) {