From 953c94c7fe231d9f7c9b36bb3934cac5cfb4e1f0 Mon Sep 17 00:00:00 2001 From: adam3smith Date: Thu, 24 Oct 2013 20:41:01 -0600 Subject: [PATCH] Change Transform text so that sentence case capitalizes after :, ?, ! This is in line with English usage and this function is principally useful for English titles. also deal with punctuation at the beginning of title Also fix capitalizeTitle to work with quotation marks and Spanish beginning punctuation. Also adds ? and ! as punctuation after which it always capitalizes switch sentence case conversion to a regex; I'm leaving capitalizeTitle as the substring routine, it's the same length and probably slightly more efficient. --- chrome/content/zotero/bindings/itembox.xml | 7 ++++++- chrome/content/zotero/xpcom/utilities.js | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/chrome/content/zotero/bindings/itembox.xml b/chrome/content/zotero/bindings/itembox.xml index f82050e1f1..fdd9a1841b 100644 --- a/chrome/content/zotero/bindings/itembox.xml +++ b/chrome/content/zotero/bindings/itembox.xml @@ -1947,7 +1947,12 @@ var newVal = Zotero.Utilities.capitalizeTitle(val.toLowerCase(), true); break; case 'sentence': - var newVal = val.length ? val[0].toUpperCase()+val.substr(1).toLowerCase() : val; + // capitalize the first letter, including after beginning punctuation + // capitalize after :, ?, ! and remove space(s) before those analogous to capitalizeTitle function + // also deal with initial punctuation here - open quotes and Spanish beginning quotation marks + newVal = val.toLowerCase(); + newVal = newVal.replace(/(([:\?!]\s*|^)([\'\"¡¿“‘„«\s]+)?[^\s])/g, function (x) { + return x.replace(/\s+/m, " ").toUpperCase();}); break; default: throw ("Invalid transform mode '" + mode + "' in zoteroitembox.textTransform()"); diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js index fc4ca851c9..6e0b66bc7d 100644 --- a/chrome/content/zotero/xpcom/utilities.js +++ b/chrome/content/zotero/xpcom/utilities.js @@ -757,13 +757,17 @@ Zotero.Utilities = { // not first or last word && i != 0 && i != lastWordIndex // does not follow a colon - && (previousWordIndex == -1 || words[previousWordIndex][words[previousWordIndex].length-1] != ":") + && (previousWordIndex == -1 || words[previousWordIndex][words[previousWordIndex].length-1].search(/[:\?!]/)==-1) ) { words[i] = lowerCaseVariant; } else { // this is not a skip word or comes after a colon; // we must capitalize - words[i] = upperCaseVariant.substr(0, 1) + lowerCaseVariant.substr(1); + // handle punctuation in the beginning, including multiple, as in "¿Qué pasa?" + var punct = words[i].match(/^[\'\"¡¿“‘„«\s]+/); + punct = punct ? punct[0].length+1 : 1; + words[i] = words[i].length ? words[i].substr(0, punct).toUpperCase() + + words[i].substr(punct).toLowerCase() : words[i]; } }