Skip to content
Permalink
Browse files

Get citekey from future citationKey or extract from extra field (#1814)

* BibLaTex, BibTeX, TEI, CSL JSON
* CSL JSON id is useful for pandoc; TEI was already updated for .citationKey but didn't have the extra-field parser
  • Loading branch information...
retorquere authored and adam3smith committed Jan 31, 2019
1 parent 1400b21 commit a340b9b4cc7a613c06e2052b3dc1d76027621983
Showing with 51 additions and 26 deletions.
  1. +10 −9 BibLaTeX.js
  2. +10 −5 BibTeX.js
  3. +13 −3 CSL JSON.js
  4. +18 −9 TEI.js
@@ -15,7 +15,7 @@
"exportFileData": false,
"useJournalAbbreviation": false
},
"lastUpdated": "2018-09-07 17:20:00"
"lastUpdated": "2019-01-30 17:16:00"
}

//%a = first listed creator surname
@@ -386,7 +386,12 @@ function creatorCheck(item, ctype) {
return false;
}

function buildCiteKey(item, citekeys) {
function buildCiteKey (item, extraFields, citekeys) {
const citationKey = extraFields.findIndex(field => field.field && field.value && field.field.toLowerCase() === 'citation key')
if (citationKey >= 0) return extraFields.splice(citationKey, 1)[0].value

if (item.citationKey) return item.citationKey

var basekey = "";
var counter = 0;
citeKeyFormatRemaining = citeKeyFormat;
@@ -478,11 +483,8 @@ function encodeFilePathComponent(value) {

if (!type) type = "misc";

var citekey = "";
if (!citekey) {
// create a unique citation key
citekey = buildCiteKey(item, citekeys);
}
var extraFields = item.extra ? parseExtraFields(item.extra) : null;
var citekey = buildCiteKey(item, extraFields, citekeys);

// write citation key (removed the comma)
Zotero.write((first ? "" : "\n\n") + "@" + type + "{" + citekey);
@@ -727,9 +729,8 @@ function encodeFilePathComponent(value) {
}
}

if (item.extra) {
if (extraFields) {
// Export identifiers
var extraFields = parseExtraFields(item.extra);
// Dedicated fields
for (var i=0; i<extraFields.length; i++) {
var rec = extraFields[i];
@@ -19,7 +19,7 @@
"inRepository": true,
"translatorType": 3,
"browserSupport": "gcsv",
"lastUpdated": "2018-03-03 13:10:16"
"lastUpdated": "2019-01-30 17:16:00"
}

function detectImport() {
@@ -1171,7 +1171,12 @@ var citeKeyConversions = {
}


function buildCiteKey (item,citekeys) {
function buildCiteKey (item, extraFields, citekeys) {
const citationKey = extraFields.findIndex(field => field.field && field.value && field.field.toLowerCase() === 'citation key')
if (citationKey >= 0) return extraFields.splice(citationKey, 1)[0].value

if (item.citationKey) return item.citationKey

var basekey = "";
var counter = 0;
citeKeyFormatRemaining = citeKeyFormat;
@@ -1270,7 +1275,8 @@ function doExport() {
if (!type) type = "misc";

// create a unique citation key
var citekey = buildCiteKey(item, citekeys);
var extraFields = item.extra ? parseExtraFields(item.extra) : null;
var citekey = buildCiteKey(item, extraFields, citekeys);

// write citation key
Zotero.write((first ? "" : "\n\n") + "@"+type+"{"+citekey);
@@ -1374,9 +1380,8 @@ function doExport() {
}
}

if (item.extra) {
if (extraFields) {
// Export identifiers
var extraFields = parseExtraFields(item.extra);
for (var i=0; i<extraFields.length; i++) {
var rec = extraFields[i];
if (!rec.field || !revExtraIds[rec.field]) continue;
@@ -12,7 +12,7 @@
"inRepository": true,
"translatorType": 3,
"browserSupport": "gcsibv",
"lastUpdated": "2017-07-05 19:32:38"
"lastUpdated": "2019-01-31 00:12:00"
}

function parseInput() {
@@ -108,7 +108,17 @@ function importNext(data, resolve, reject) {

function doExport() {
var item, data = [];
while (item = Z.nextItem()) data.push(ZU.itemToCSLJSON(item));
while (item = Z.nextItem()) {
if (item.extra) {
item.extra = item.extra.replace(/(?:^|\n)citation key\s*:\s*([^\s]+)(?:\n|$)/i, (m, citationKey) => {
item.citationKey = citationKey;
return '\n';
}).trim();
}
var cslItem = ZU.itemToCSLJSON(item);
if (item.citationKey) cslItem.id = item.citationKey;
data.push(cslItem);
}
Z.write(JSON.stringify(data, null, "\t"));
}
/** BEGIN TEST CASES **/
@@ -144,4 +154,4 @@ var testCases = [
]
}
]
/** END TEST CASES **/
/** END TEST CASES **/
27 TEI.js
@@ -19,7 +19,7 @@
"Full TEI Document": false,
"Export Collections": false
},
"lastUpdated": "2019-01-22 09:47:00"
"lastUpdated": "2019-01-31 00:12:00"
}

// ********************************************************************
@@ -35,17 +35,17 @@

// 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
// 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/>.
// along with this program. If not, see <http://www.gnu.org/licenses/>.


// *********************************************************************
//
// This script does fairly well with papers, theses, websites and
// books. Some item properties, important for the more exotic
// books. Some item properties, important for the more exotic
// publication types, are still missing. That means, the first 30 are
// implemented, the rest may be added when I need them. If you like to
// see some particular item property and you also have a basic idea
@@ -96,13 +96,22 @@ function replaceFormatting(title) {
}

function genXMLId(item) {
// use Better BibTeX for Zotero citation key if available
if (item.extra) {
item.extra = item.extra.replace(/(?:^|\n)citation key\s*:\s*([^\s]+)(?:\n|$)/i, (m, citationKey) => {
item.citationKey = citationKey
return '\n'
}).trim()
}
if (item.citationKey) return item.citationKey

var xmlid = '';
if (item.creators && item.creators[0] && (item.creators[0].lastName || item.creators[0].name)) {
if (item.creators[0].lastName){
xmlid = item.creators[0].lastName;
xmlid = item.creators[0].lastName;
}
if (item.creators[0].name){
xmlid = item.creators[0].name;
xmlid = item.creators[0].name;
}
if (item.date) {
var date = Zotero.Utilities.strToDate(item.date);
@@ -122,13 +131,13 @@ function genXMLId(item) {
// [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] |
// [#x10000-#xEFFFF]

// Name = NameStartChar | "-" | "." | [0-9] | #xB7 |
// Name = NameStartChar | "-" | "." | [0-9] | #xB7 |
// [#x0300-#x036F] | [#x203F-#x2040]

xmlid = xmlid.replace(/^[^A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u10000-\uEFFFF]/, "");
xmlid = xmlid.replace(/[^-A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u10000-\uEFFFF.0-9\u00B7\u0300-\u036F\u203F-\u2040]/g, "");
} else {
// "zoteroItem_item.key" as value for entries without creator
// "zoteroItem_item.key" as value for entries without creator
var str = item.uri;
var n = str.lastIndexOf('/');
var result = str.substring(n + 1);
@@ -182,7 +191,7 @@ function generateItem(item, teiDoc) {

if (Zotero.getOption("Generate XML IDs")) {
if (!generatedItems[item.uri]) {
var xmlid = item.citationKey || genXMLId(item); // use Better BibTeX for Zotero citation key if available
var xmlid = genXMLId(item);
bibl.setAttributeNS(ns.xml, "xml:id", xmlid);
exportedXMLIds[xmlid] = bibl;
} else {

0 comments on commit a340b9b

Please sign in to comment.
You can’t perform that action at this time.