From 7fc56486250622ad8f1e62033984815a56b2d0d3 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 7 May 2007 22:31:32 +0000 Subject: [PATCH] - Updated attachment auto-renaming to automatically strip conditional blocks (in curly braces) if no value for field - Updated default format string to use conditional blocks --- chrome/content/zotero/xpcom/attachments.js | 54 +++++++++++++++------- defaults/preferences/zotero.js | 2 +- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index 2c3181369..fd30be391 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -713,41 +713,61 @@ Zotero.Attachments = new function(){ // Replaces the substitution marker with the field value, // truncating based on the {[0-9]+} modifier if applicable function rpl(field, str) { + if (!str) { + str = formatString; + } + switch (field) { case 'creator': field = 'firstCreator'; var rpl = '%c'; break; + case 'year': + var rpl = '%y'; + break; + case 'title': var rpl = '%t'; break; } + switch (field) { + case 'year': + var value = item.getField('date', true); + if (value) { + value = Zotero.Date.multipartToSQL(value).substr(0, 4); + if (value == '0000') { + value = ''; + } + } + break; + + default: + var value = item.getField(field, false, true); + } + + // If no value for this field, strip entire conditional block + // (within curly braces) + if (!value) { + var re = new RegExp("\{[^%]*" + rpl + "(\{[0-9]+\})?" + "[^%]*\}"); + if (str.match(re)) { + return str.replace(re, '') + } + } + var f = function(match) { - var value = item.getField(field, false, true); var chars = match.match(/{([0-9]+)}/); return (chars) ? value.substr(0, chars[1]) : value; } - return str.replace(new RegExp(rpl + "(\{[0-9]+\})?"), f); + var re = new RegExp("(\{[^%]*)?" + rpl + "(\{[0-9]+\})?" + "([^%]*\})?"); + return str.replace(re, f); } - // Creator - formatString = rpl('creator', formatString); - - // Year - var year = item.getField('date', true); - if (year) { - year = Zotero.Date.multipartToSQL(year).substr(0, 4); - if (year == '0000') { - year = ''; - } - } - formatString = formatString.replace('%y', year); - - // Title - formatString = rpl('title', formatString); + formatString = rpl('creator'); + formatString = rpl('year'); + formatString = rpl('title'); // Strip potentially invalid characters // See http://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words diff --git a/defaults/preferences/zotero.js b/defaults/preferences/zotero.js index b4093c365..10a2d3a2e 100644 --- a/defaults/preferences/zotero.js +++ b/defaults/preferences/zotero.js @@ -18,7 +18,7 @@ pref("extensions.zotero.reportTranslationFailure",true); pref("extensions.zotero.automaticTags",true); pref("extensions.zotero.fontSize", "1.0"); pref("extensions.zotero.recursiveCollections", false); -pref("extensions.zotero.attachmentRenameFormatString", '%c - %y - %t{50}'); +pref("extensions.zotero.attachmentRenameFormatString", '{%c - }{%y - }{%t{50}}'); pref("extensions.zotero.lastCreatorFieldMode",0); pref("extensions.zotero.lastAbstractExpand",0);