Automatically substitute event-title for event in styles

Stopgap until styles are updated in citation-style-language/styles#6151
This commit is contained in:
Dan Stillman 2022-08-10 00:52:16 -04:00
parent df5bde79e5
commit 7eab91a160
2 changed files with 69 additions and 0 deletions

View file

@ -758,6 +758,8 @@ Zotero.Style.prototype.getCiteProc = function(locale, format, automaticJournalAb
var xml = this.getXML();
}
xml = this._eventToEventTitle(xml);
try {
var citeproc;
if (Zotero.Prefs.get('cite.useCiteprocRs')) {
@ -797,6 +799,32 @@ Zotero.Style.prototype.getCiteProc = function(locale, format, automaticJournalAb
}
};
Zotero.Style.prototype._eventToEventTitle = function (xml) {
var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
.createInstance(Components.interfaces.nsIDOMParser);
var doc = parser.parseFromString(xml, "text/xml");
if (doc.querySelector('[variable*="event-title"]')) {
return xml;
}
var elems = doc.querySelectorAll('[variable*="event"]');
if (!elems.length) {
return xml;
}
var changed = false;
for (let elem of elems) {
let variable = elem.getAttribute('variable');
if (!/event( |$)/.test(variable)) {
continue;
}
elem.setAttribute('variable', variable.replace(/event(?= |$)/, 'event-title'));
changed = true;
}
if (changed) {
xml = doc.documentElement.outerHTML;
}
return xml;
};
Zotero.Style.prototype.__defineGetter__("class",
/**
* Retrieves the style class, either from the metadata that's already loaded or by loading the file

View file

@ -83,4 +83,45 @@ describe("Zotero.Styles", function() {
assert.equal(o.text, '1. Foo bar: baz qux. 2019; \n');
});
});
describe("event-title replacement", function () {
var item;
var eventStyleXML = `<?xml version="1.0" encoding="utf-8"?>
<style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" version="1.0">
<info>
<title>Test</title>
<id>http://www.zotero.org/styles/test</id>
<link href="http://www.zotero.org/styles/test" rel="self"/>
<updated>2022-04-14T13:48:43+00:00</updated>
</info>
<bibliography>
<layout>
<text variable="event"/>
<text value=" - "/>
<text variable="event event-place"/>
</layout>
</bibliography>
</style>
`;
before(async function () {
item = createUnsavedDataObject(
'item',
{
itemType: 'conferencePaper',
title: 'Conference Paper'
}
);
item.setField('conferenceName', 'Conference');
await item.saveTx();
});
it("should substitute `event-title` in style using `event`", function () {
var style = new Zotero.Style(eventStyleXML);
var cslEngine = style.getCiteProc('en-US', 'text');
var text = Zotero.Cite.makeFormattedBibliographyOrCitationList(cslEngine, [item], "text");
cslEngine.free();
assert.equal(text, 'Conference - Conference\n');
});
});
});