Addresses #417, Preference to keep files in Zotero directory named based on the attachment title
Implemented auto-renaming of PDFs and other files based on the parent item's metadata. Format is customizable via 'attachmentRenameFormatString' pref, though options are currently rather limited. Default is '%c - %y - %t{50}', meaning "FirstCreator - 2007 - Title Truncated to 50 Characters" Also: - Removed |forceFileName| from Attachments.importFromDocument() -- I don't think it'll be necessary? - Modified WebPageDump not to save HTML wrapper files around non-HTML documents
This commit is contained in:
parent
3dc5a21f4a
commit
2de3ed40c5
|
@ -1566,7 +1566,7 @@ var ZoteroPane = new function()
|
|||
Zotero.Attachments.linkFromDocument(window.content.document, itemID, false, parentCollectionID);
|
||||
}
|
||||
else {
|
||||
Zotero.Attachments.importFromDocument(window.content.document, itemID, false, false, parentCollectionID);
|
||||
Zotero.Attachments.importFromDocument(window.content.document, itemID, false, parentCollectionID);
|
||||
}
|
||||
}
|
||||
// Give progress window time to appear
|
||||
|
|
|
@ -873,8 +873,11 @@ var wpdDOMSaver = {
|
|||
// wrapper HTML File which references "aDocument"
|
||||
// ("aFileName" is the filename without(!) extension)
|
||||
saveDocumentFile : function(aDocument,aFileName)
|
||||
{
|
||||
dump("[wpdDOMSaver.saveDocumentFile]: "+aFileName+"\n");
|
||||
{
|
||||
dump("[wpdDOMSaver.saveDocumentFile]: "+aFileName+"\n");
|
||||
|
||||
return this.download(this.currentURL,true)
|
||||
/* Wrapper file disabled by Dan S. for Zotero
|
||||
var aFileURL = aDocument.location.href;
|
||||
|
||||
if ( !aFileName ) aFileName = "file" + Math.random().toString();
|
||||
|
@ -887,11 +890,13 @@ var wpdDOMSaver = {
|
|||
var HTMLText = '<html><head><meta http-equiv="refresh" content="0;URL=' + newFileName + '"></head><body></body></html>';
|
||||
}
|
||||
|
||||
var HTMLFile = this.currentDir + aFileName + ".html";
|
||||
var HTMLFile = this.currentDir + aFileName + ".html";
|
||||
|
||||
if (!wpdCommon.writeFile(HTMLText,HTMLFile))
|
||||
wpdCommon.addError("[wpdDOMSaver.saveDocumentFile]: could not write HTML wrapper for "+aFileName+"\n");
|
||||
|
||||
return aFileName + ".html";
|
||||
|
||||
return aFileName + ".html";
|
||||
*/
|
||||
},
|
||||
|
||||
// save the CSS Stylesheets of "aDocument" as "aFileName" and
|
||||
|
|
|
@ -33,6 +33,7 @@ Zotero.Attachments = new function(){
|
|||
this.linkFromURL = linkFromURL;
|
||||
this.linkFromDocument = linkFromDocument;
|
||||
this.importFromDocument = importFromDocument;
|
||||
this.getFileBaseNameFromItem = getFileBaseNameFromItem;
|
||||
this.createDirectoryForItem = createDirectoryForItem;
|
||||
this.getPath = getPath;
|
||||
|
||||
|
@ -168,7 +169,7 @@ Zotero.Attachments = new function(){
|
|||
}
|
||||
|
||||
|
||||
function importFromURL(url, sourceItemID, forceTitle, forceFileName, parentCollectionIDs){
|
||||
function importFromURL(url, sourceItemID, forceTitle, forceFileBaseName, parentCollectionIDs){
|
||||
Zotero.debug('Importing attachment from URL');
|
||||
|
||||
Zotero.Utilities.HTTP.doHead(url, function(obj){
|
||||
|
@ -193,7 +194,7 @@ Zotero.Attachments = new function(){
|
|||
browser.addEventListener("pageshow", function(){
|
||||
try {
|
||||
Zotero.Attachments.importFromDocument(browser.contentDocument,
|
||||
sourceItemID, forceTitle, forceFileName, parentCollectionIDs);
|
||||
sourceItemID, forceTitle, parentCollectionIDs);
|
||||
}
|
||||
finally {
|
||||
browser.removeEventListener("pageshow", arguments.callee, true);
|
||||
|
@ -216,7 +217,14 @@ Zotero.Attachments = new function(){
|
|||
|
||||
// Otherwise use a remote web page persist
|
||||
else {
|
||||
var fileName = _getFileNameFromURL(url, mimeType);
|
||||
if (forceFileBaseName) {
|
||||
var ext = _getExtensionFromURL(url, mimeType);
|
||||
var fileName = forceFileBaseName + (ext != '' ? '.' + ext : '');
|
||||
}
|
||||
else {
|
||||
var fileName = _getFileNameFromURL(url, mimeType);
|
||||
}
|
||||
|
||||
var title = forceTitle ? forceTitle : fileName;
|
||||
|
||||
const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
|
||||
|
@ -405,7 +413,7 @@ Zotero.Attachments = new function(){
|
|||
*
|
||||
* Returns itemID of attachment
|
||||
*/
|
||||
function importFromDocument(document, sourceItemID, forceTitle, forceFileName, parentCollectionIDs) {
|
||||
function importFromDocument(document, sourceItemID, forceTitle, parentCollectionIDs) {
|
||||
Zotero.debug('Importing attachment from document');
|
||||
|
||||
var url = document.location.href;
|
||||
|
@ -643,6 +651,75 @@ Zotero.Attachments = new function(){
|
|||
*/
|
||||
|
||||
|
||||
/*
|
||||
* Returns a formatted string to use as the basename of an attachment
|
||||
* based on the metadata of the specified item and a format string
|
||||
*
|
||||
* (Optional) |formatString| specifies the format string -- otherwise
|
||||
* the 'attachmentRenameFormatString' pref is used
|
||||
*
|
||||
* Valid substitution markers:
|
||||
*
|
||||
* %c -- firstCreator
|
||||
* %y -- year (extracted from Date field)
|
||||
* %t -- title
|
||||
*
|
||||
* Fields can be truncated to a certain length by appending an integer
|
||||
* within curly brackets -- e.g. %t{50} truncates the title to 50 characters
|
||||
*/
|
||||
function getFileBaseNameFromItem(itemID, formatString) {
|
||||
if (!formatString) {
|
||||
formatString = Zotero.Prefs.get('attachmentRenameFormatString');
|
||||
}
|
||||
|
||||
var item = Zotero.Items.get(itemID);
|
||||
if (!item) {
|
||||
throw ('Invalid itemID ' + itemID + ' in Zotero.Attachments.getFileBaseNameFromItem()');
|
||||
}
|
||||
|
||||
// Replaces the substitution marker with the field value,
|
||||
// truncating based on the {[0-9]+} modifier if applicable
|
||||
function rpl(field, str) {
|
||||
switch (field) {
|
||||
case 'creator':
|
||||
field = 'firstCreator';
|
||||
var rpl = '%c';
|
||||
break;
|
||||
|
||||
case 'title':
|
||||
var rpl = '%t';
|
||||
break;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
return formatString;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Create directory for attachment files within storage directory
|
||||
*/
|
||||
|
|
|
@ -1223,7 +1223,8 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) {
|
|||
attachment.url = Zotero.Ingester.ProxyMonitor.properToProxy(attachment.url);
|
||||
}
|
||||
|
||||
Zotero.Attachments.importFromURL(attachment.url, myID, title);
|
||||
var fileBaseName = Zotero.Attachments.getFileBaseNameFromItem(myID);
|
||||
Zotero.Attachments.importFromURL(attachment.url, myID, title, fileBaseName);
|
||||
}
|
||||
}
|
||||
// links no longer exist, so just don't save them
|
||||
|
|
|
@ -18,6 +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.lastCreatorFieldMode",0);
|
||||
pref("extensions.zotero.lastAbstractExpand",0);
|
||||
|
|
Loading…
Reference in New Issue
Block a user