Skip to content

Commit

Permalink
Merge pull request zotero#813 from aurimasv/bibtex-case
Browse files Browse the repository at this point in the history
[BibTeX] Protect words with initial uppercase letters on export
  • Loading branch information
aurimasv committed Feb 12, 2015
2 parents daa1a05 + f79e778 commit 451a393
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions BibTeX.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"inRepository": true,
"translatorType": 3,
"browserSupport": "gcsv",
"lastUpdated": "2014-11-22 12:43:04"
"lastUpdated": "2015-02-12 07:13:30"
}

function detectImport() {
Expand Down Expand Up @@ -88,6 +88,15 @@ var fieldMap = {
assignee:"assignee"
};

// Fields for which upper case letters will be protected on export
var caseProtectedFields = [
"title",
"type",
"shorttitle",
"booktitle",
"series"
];

// Import/export in BibTeX
var extraIdentifiers = {
lccn: 'LCCN',
Expand Down Expand Up @@ -891,24 +900,9 @@ function writeField(field, value, isMacro) {
if (!isMacro && !(field == "url" || field == "doi" || field == "file" || field == "lccn" )) {
// I hope these are all the escape characters!
value = value.replace(/[|\<\>\~\^\\\{\}]/g, mapEscape).replace(/([\#\$\%\&\_])/g, "\\$1");

//disable
/** if (field == "title" || field == "type" || field == "shorttitle" || field == "booktitle" || field == "series") {
if (!isTitleCase(value)) {
//protect caps for everything but the first letter
value = value.replace(/(.)([A-Z]+)/g, "$1{$2}");
} else { //protect all-caps vords and initials
value = value.replace(/([\s.-])([A-Z]+)(?=\.)/g, "$1{$2}"); //protect initials
if(value.toUpperCase() != value) value = value.replace(/(\s)([A-Z]{2,})(?=[\.,\s]|$)/g, "$1{$2}");
}
}
**/

// Case of words with uppercase characters in non-initial positions is preserved with braces.
// we're looking at all unicode letters
var protectCaps = new ZU.XRegExp("\\b\\p{Letter}+\\p{Uppercase_Letter}\\p{Letter}*", 'g')
if (field != "pages") {
value = ZU.XRegExp.replace(value, protectCaps, "{$0}");

if (caseProtectedFields.indexOf(field) != -1) {
value = ZU.XRegExp.replace(value, protectCapsRE, "$1{$2}");
}
}
if (Zotero.getOption("exportCharset") != "UTF-8") {
Expand Down Expand Up @@ -1110,7 +1104,23 @@ function buildCiteKey (item,citekeys) {
return citekey;
}

var protectCapsRE;
function doExport() {
if (Zotero.getHiddenPref && Zotero.getHiddenPref('BibTeX.export.dontProtectInitialCase')) {
// Case of words with uppercase characters in non-initial positions is
// preserved with braces.
// Two captures because of the other regexp below
protectCapsRE = new ZU.XRegExp("()\\b(\\p{Letter}+\\p{Uppercase_Letter}\\p{Letter}*)", 'g');
} else {
// Protect all upper case letters, even if the uppercase letter is only in
// initial position of the word.
// Don't protect first word if only first letter is capitalized
protectCapsRE = new ZU.XRegExp(
"(.)\\b(\\p{Letter}*\\p{Uppercase_Letter}\\p{Letter}*)" // Non-initial words with capital letter anywhere
+ "|^()(\\p{Letter}+\\p{Uppercase_Letter}\\p{Letter}*)" // Initial word with capital in non-initial position
, 'g');
}

//Zotero.write("% BibTeX export generated by Zotero "+Zotero.Utilities.getVersion());
// to make sure the BOM gets ignored
Zotero.write("\n");
Expand Down

0 comments on commit 451a393

Please sign in to comment.