New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper nouns are not escaped in Biblatex export #1667

Open
adam-ah opened this Issue May 29, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@adam-ah

adam-ah commented May 29, 2018

(Bug moved here from retorquere/zotero-better-bibtex#975)
There was a previous PR a while ego to preserve (wrap with {}) the proper nouns in titles when exporting to Biblatex. In some cases it still does work (for instance, ERG is turned into {ERG}) but other cases don't ("...of Facebook on..." stays as is.)
Original PR #813

Apparently Bibtex.js export has this fix, Better Biblatex supports this too, but missing from BibLaTeX.js

Exporting references

Sample working well:

@article{ordy_visual_1968,
	title = {Visual acuity and {ERG}-{CFF} in relation to the morphologic organization of the retina among diurnal and nocturnal primates},
	volume = {8},
	issn = {00426989},
	url = {http://linkinghub.elsevier.com/retrieve/pii/004269896890028X},
	doi = {10.1016/0042-6989(68)90028-X},
	pages = {1205--1225},
	number = {9},
	journaltitle = {Vision Research},
	author = {Ordy, J.M. and Samorajski, T.},
	urldate = {2018-05-19},
	date = {1968-09},
	langid = {english}

Broken sample:

@article{fardouly_social_2015,
	title = {Social comparisons on social media: the impact of Facebook on young women's body image Piglet concerns and mood},
	volume = {13},
	issn = {17401445},
	url = {http://linkinghub.elsevier.com/retrieve/pii/S174014451400148X},
	doi = {10.1016/j.bodyim.2014.12.002},
	shorttitle = {Social comparisons on social media},
	abstract = {....},
	pages = {38--45},
	journaltitle = {Body Image},
	author = {Fardouly, Jasmine and Diedrichs, Phillippa C. and Vartanian, Lenny R. and Halliwell, Emma},
	urldate = {2018-05-12},
	date = {2015-03},
	langid = {english},
	file = {...}

@adam-ah adam-ah referenced this issue May 29, 2018

Closed

Proper nouns are not escaped in Biblatex export #975

4 of 4 tasks complete
@retorquere

This comment has been minimized.

Show comment
Hide comment
@retorquere

retorquere May 29, 2018

Contributor

For clarity's sake, the bug wasn't moved here in the sense that it was a BBT problem triggered by Zotero, it's just that the stock bib(la)tex exporters have a bug. It was first reported on the bbt tracker, but that's the only relation.

Contributor

retorquere commented May 29, 2018

For clarity's sake, the bug wasn't moved here in the sense that it was a BBT problem triggered by Zotero, it's just that the stock bib(la)tex exporters have a bug. It was first reported on the bbt tracker, but that's the only relation.

@adam-ah

This comment has been minimized.

Show comment
Hide comment
@adam-ah

adam-ah May 30, 2018

If anyone wants to create & merge the fix, the following patch will bring BibLatex.js exporting to the same escaping functionality as the current Bibtex.js:

@@ -60,6 +60,15 @@
 	PMID: 'pmid',
 	DOI: 'doi'
 };
+//
+// Fields for which upper case letters will be protected on export
+var caseProtectedFields = [
+	"title",
+	"type",
+	"shorttitle",
+	"booktitle",
+	"series"
+];
 
 // Imported by BibTeX. Exported by BibLaTeX only
 var revEprintIds = {
@@ -277,9 +286,8 @@
 
 		// 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$3}"); // only $2 or $3 will have a value, not both
 		}
 
 		// Page ranges should use double dash
@@ -443,7 +451,22 @@
 	return value.replace(encodeFilePathRE, "\\$&");
 }
 
-	function doExport() {
+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 extra 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("% biblatex export generated by Zotero "+Zotero.Utilities.getVersion());
 		// to make sure the BOM gets ignored
 		Zotero.write("\n");

When applied the original broken sample will change like this:

@@ -1,6 +1,6 @@
 
 @article{fardouly_social_2015,
-	title = {Social comparisons on social media: the impact of Facebook on young women's body image concerns and mood},
+	title = {Social comparisons on social media: the impact of {Facebook} on young women's body image concerns and mood},
 	volume = {13},
 	issn = {17401445},
 	url = {http://linkinghub.elsevier.com/retrieve/pii/S174014451400148X},

adam-ah commented May 30, 2018

If anyone wants to create & merge the fix, the following patch will bring BibLatex.js exporting to the same escaping functionality as the current Bibtex.js:

@@ -60,6 +60,15 @@
 	PMID: 'pmid',
 	DOI: 'doi'
 };
+//
+// Fields for which upper case letters will be protected on export
+var caseProtectedFields = [
+	"title",
+	"type",
+	"shorttitle",
+	"booktitle",
+	"series"
+];
 
 // Imported by BibTeX. Exported by BibLaTeX only
 var revEprintIds = {
@@ -277,9 +286,8 @@
 
 		// 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$3}"); // only $2 or $3 will have a value, not both
 		}
 
 		// Page ranges should use double dash
@@ -443,7 +451,22 @@
 	return value.replace(encodeFilePathRE, "\\$&");
 }
 
-	function doExport() {
+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 extra 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("% biblatex export generated by Zotero "+Zotero.Utilities.getVersion());
 		// to make sure the BOM gets ignored
 		Zotero.write("\n");

When applied the original broken sample will change like this:

@@ -1,6 +1,6 @@
 
 @article{fardouly_social_2015,
-	title = {Social comparisons on social media: the impact of Facebook on young women's body image concerns and mood},
+	title = {Social comparisons on social media: the impact of {Facebook} on young women's body image concerns and mood},
 	volume = {13},
 	issn = {17401445},
 	url = {http://linkinghub.elsevier.com/retrieve/pii/S174014451400148X},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment