From 44b6cd05256bdc93a9068571815201aa4dd74a86 Mon Sep 17 00:00:00 2001 From: Adomas Ven Date: Thu, 16 Dec 2021 00:28:41 +0200 Subject: [PATCH] citeproc-rs support (#2220) Disabled under zotero.cite.useCiteprocRs by default --- chrome/content/zotero/fileInterface.js | 12 +- chrome/content/zotero/rtfScan.jsx | 5 +- chrome/content/zotero/tools/csledit.js | 4 +- chrome/content/zotero/tools/csledit.xul | 1 + chrome/content/zotero/tools/cslpreview.js | 4 +- chrome/content/zotero/xpcom/cite.js | 1 - .../content/zotero/xpcom/citeprocRsBridge.js | 285 + chrome/content/zotero/xpcom/integration.js | 83 +- chrome/content/zotero/xpcom/quickCopy.js | 9 +- chrome/content/zotero/xpcom/style.js | 49 +- chrome/content/zotero/xpcom/utilities | 2 +- components/zotero-service.js | 1 + defaults/preferences/zotero.js | 1 + package-lock.json | 6963 ++++++++++++++++- package.json | 1 + resource/citeproc_rs_wasm.js | 1 + resource/citeproc_rs_wasm_bg.wasm | 1 + resource/citeproc_rs_wasm_include.js | 1 + resource/require.js | 10 +- scripts/babel-worker.js | 1 - scripts/config.js | 6 +- test/tests/citeprocRsBridgeTest.js | 170 + 22 files changed, 7429 insertions(+), 182 deletions(-) create mode 100644 chrome/content/zotero/xpcom/citeprocRsBridge.js create mode 120000 resource/citeproc_rs_wasm.js create mode 120000 resource/citeproc_rs_wasm_bg.wasm create mode 120000 resource/citeproc_rs_wasm_include.js create mode 100644 test/tests/citeprocRsBridgeTest.js diff --git a/chrome/content/zotero/fileInterface.js b/chrome/content/zotero/fileInterface.js index 39d0be740a..2a8b9306c8 100644 --- a/chrome/content/zotero/fileInterface.js +++ b/chrome/content/zotero/fileInterface.js @@ -750,7 +750,7 @@ var Zotero_File_Interface = new function() { var clipboardService = Components.classes["@mozilla.org/widget/clipboard;1"]. getService(Components.interfaces.nsIClipboard); style = Zotero.Styles.get(style); - var cslEngine = style.getCiteProc(locale); + var cslEngine = style.getCiteProc(locale, 'html'); if (asCitations) { cslEngine.updateItems(items.map(item => item.id)); @@ -779,11 +779,13 @@ var Zotero_File_Interface = new function() { else { // Generate engine again to work around citeproc-js problem: // https://github.com/zotero/zotero/commit/4a475ff3 - cslEngine = style.getCiteProc(locale); - output = Zotero.Cite.makeFormattedBibliographyOrCitationList(cslEngine, items, "text"); + cslEngine.free(); + cslEngine = style.getCiteProc(locale, 'text'); + output = Zotero.Cite.makeFormattedBibliographyOrCitationList(cslEngine, items, 'text'); } } - + cslEngine.free(); + var str = Components.classes["@mozilla.org/supports-string;1"]. createInstance(Components.interfaces.nsISupportsString); str.data = output; @@ -831,7 +833,7 @@ var Zotero_File_Interface = new function() { } else { var style = Zotero.Styles.get(io.style); - var cslEngine = style.getCiteProc(locale); + var cslEngine = style.getCiteProc(locale, format); var bibliography = Zotero.Cite.makeFormattedBibliographyOrCitationList(cslEngine, items, format, io.mode === "citations"); } diff --git a/chrome/content/zotero/rtfScan.jsx b/chrome/content/zotero/rtfScan.jsx index c0b3c099cd..c8b2b71e99 100644 --- a/chrome/content/zotero/rtfScan.jsx +++ b/chrome/content/zotero/rtfScan.jsx @@ -465,8 +465,7 @@ var Zotero_RTFScan = new function() { // load style and create ItemSet with all items var zStyle = Zotero.Styles.get(document.getElementById("style-listbox").value) var locale = document.getElementById("locale-menu").value; - var style = zStyle.getCiteProc(locale); - style.setOutputFormat("rtf"); + var style = zStyle.getCiteProc(locale, 'rtf'); var isNote = zStyle.class == "note"; // create citations @@ -563,6 +562,8 @@ var Zotero_RTFScan = new function() { } } } + + cslEngine.free(); Zotero.File.putContents(outputFile, contents); diff --git a/chrome/content/zotero/tools/csledit.js b/chrome/content/zotero/tools/csledit.js index b787d46b2e..abb618b2ea 100644 --- a/chrome/content/zotero/tools/csledit.js +++ b/chrome/content/zotero/tools/csledit.js @@ -173,7 +173,7 @@ var Zotero_CSL_Editor = new function() { var selectedLocale = document.getElementById("locale-menu").value; var styleEngine; try { - styleEngine = style.getCiteProc(style.locale || selectedLocale); + styleEngine = style.getCiteProc(style.locale || selectedLocale, 'html'); } catch(e) { iframe.contentDocument.documentElement.innerHTML = '
' + Zotero.getString('styles.editor.warning.parseError') + '
'+e+'
'; throw e; @@ -231,7 +231,7 @@ var Zotero_CSL_Editor = new function() { iframe.contentDocument.documentElement.innerHTML = '
' + Zotero.getString('styles.editor.warning.renderError') + '
'+e+'
'; throw e; } - editor.styleEngine = styleEngine; + styleEngine.free(); } diff --git a/chrome/content/zotero/tools/csledit.xul b/chrome/content/zotero/tools/csledit.xul index bb08bca9a4..29428e7d66 100644 --- a/chrome/content/zotero/tools/csledit.xul +++ b/chrome/content/zotero/tools/csledit.xul @@ -35,6 +35,7 @@ id="csl-edit" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" onload="Zotero_CSL_Editor.init();" + onunload="Zotero_CSL_Editor.onUnload()" title="&styles.editor;">