Specific ZotFile workflow with Zotero7 file renaming rules
Taking in consideration the suggestion of @AbeJellinek in https://forums.zotero.org/discussion/comment/470158/#Comment_470158, I ask here for how to migrate to the Zotero File Renaming syntax my ZotFile workflow.
I summarize the main characteristics and copy below the preferences:
Format for all item types: "{%a}{-%y}{-%t}", so generally, authors-year-title
Marked additional settings:
-Delimiter between multiple authors: and
-Replace blanks (no blanks)
-Truncate title after . or : or ?
-Maximum length of title 80
-Maximum number of authors 2
-Number of authors to display when authors are omitted 1
-Add suffix when authors are omitted: etal
-Remove special characters (diacritics) from filename
-Only work with pdf,doc,docx,txt,rtf,djvu,epub
Thanks for your help!
I summarize the main characteristics and copy below the preferences:
Format for all item types: "{%a}{-%y}{-%t}", so generally, authors-year-title
Marked additional settings:
-Delimiter between multiple authors: and
-Replace blanks (no blanks)
-Truncate title after . or : or ?
-Maximum length of title 80
-Maximum number of authors 2
-Number of authors to display when authors are omitted 1
-Add suffix when authors are omitted: etal
-Remove special characters (diacritics) from filename
-Only work with pdf,doc,docx,txt,rtf,djvu,epub
Thanks for your help!
user_pref("extensions.zotfile.authors_delimiter", "and");
user_pref("extensions.zotfile.etal", "etal");
user_pref("extensions.zotfile.import", false);
user_pref("extensions.zotfile.removeDiacritics", true);
user_pref("extensions.zotfile.renameFormat", "{%a}{-%y}{-%t}");
user_pref("extensions.zotfile.renameFormat_patent", "{%a}{-%y}{-%t}");
user_pref("extensions.zotfile.replace_blanks", true);
user_pref("extensions.zotfile.source_dir_ff", false);
Upgrade Storage
{{ if {{ authors match="[^,]+,[^,]+,[^,]+" }} }}
{{ authors max="1" suffix="-etal" }}
{{ else }}
{{ authors join=" and " }}
{{ endif }}
{{ year prefix="-"}}
{{ title truncate="80" replaceFrom="(\.|:|\?)(.*?)$" prefix="-" }}
File renaming template currently does not support counting authors, but we can use a regular expression to check if there are at least three authors. If that's the case, the first branch executes, producing
author-etal. If there are fewer than three authors, the second branch executes, producingauthor and author.Next, we display the year, prefixing it with a hyphen ("-").
Finally, we add the title, which uses regex to remove everything after a period (.), colon (:), or question mark (?). The title is truncated at 80 characters.
Zotero will always produce a valid filename, as special characters that are not allowed in filenames are removed in the final step. This can be specified using a hidden pref
extensions.zotero.autoRenameFiles.fileTypesas explained on the file renaming page.It only lacks replacing blanks with, for example, no character or "_" (in particular I would use {{authors join="_and_"}}). How should I proceed to get this?
replace_blanksconverts spaces to underscores in the final file name.In the new Zotero templating system, you would need to specify this as
casefor every field where spaces might occur, something like:{{ if {{ authors match="[^,]+,[^,]+,[^,]+" }} }}{{ authors max="1" suffix="-etal" case="snake" }}
{{ else }}
{{ authors join=" and " case="snake" }}
{{ endif }}
{{ year prefix="-" }}
{{ title truncate="80" replaceFrom="(\.|:|\?)(.*?)$" prefix="-" case="snake" }}
I see that specifying
`case = "snake"`also makes all to be lower case.Instead, could I do
replaceFrom="\s+" replaceTo="_" regexOpts="g"?https://github.com/zotero/zotero/blob/b1b06cedde9fe09fea1e95162c51ef27b45a8e2d/chrome/content/zotero/xpcom/attachments.js#L2329-L2353
switch (textCase) {
case 'upper':
value = value.toUpperCase();
break;
case 'lower':
value = value.toLowerCase();
break;
case 'sentence':
value = value.slice(0, 1).toUpperCase() + value.slice(1);
break;
case 'title':
value = Zotero.Utilities.capitalizeTitle(value, true);
break;
case 'hyphen':
value = value.replace(/\s+-/g, '-').replace(/-\s+/g, '-');
value = value.toLowerCase().replace(/\s+/g, '-');
break;
case 'snake':
value = value.replace(/\s+_/g, '_').replace(/_\s+/g, '_');
value = value.toLowerCase().replace(/\s+/g, '_');
break;
case 'camel':
value = value.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
break;
}
replaceFromfor the title, and there is currently no support for specifying more than one replacement for a value.I'm afraid going with all lowercase 'snake_case' is the closest that the current Zotero template-based file renaming can do to match your naming pattern.
{{ title truncate="80" replaceFrom="(\.|:|\?)(.*?)$" prefix="-" case="snake" }}
{{ title replaceFrom="\s+" replaceTo="_" regexOpts="g" }}
wouldn't work (as in the code you suggests there are multiple lines for authors)?
titletwice, formatted in two different ways. My earlier template includesif/elseswitch to pick one or the other way of formatting theauthorsfield.This is my setting:
{{ if {{ authors match="[^,]+,[^,]+,[^,]+" }} }}
{{ authors max="1" suffix=" et al" }}
{{ else }}
{{ authors join="_" }}
{{ endif }}
https://s3.amazonaws.com/zotero.org/images/forums/u9553762/o8bqhxmlrh8x1agn0icy.png
https://s3.amazonaws.com/zotero.org/images/forums/u3978561/qy40i86hukcejrdpo4dk.png
@iagogv This template may be helpful for you.
```
{{ if {{ authors match="[^,]+,[^,]+,[^,]+" }} }}
{{ authors max="1" suffix="_etal" }}
{{ else }}
{{ authors join=" and " replaceFrom="\s+" replaceTo="_" regexOpts="g" }}
{{ endif }}
{{ year prefix="-" }}
{{ title truncate="80" replaceFrom="(\.|:|\?)(.*?)$" prefix="-" replaceFrom="\s+" replaceTo="_" regexOpts="g" }}
```
Unfortunately, the last part of your template won't work, as you can only specify one set of
replaceFrom/replaceToper variable. As I explained above, usingcase="snake"is the best the current system can do. The forum supports a small subset of HTML tags. For code blocks, I'm using<code>.replaceFromis allowed per variable.Thanks for trying to implement nesting in the template engine (https://github.com/zotero/zotero/issues/4555#issuecomment-2765839417). I've just seen your last comment in the Github issue. The initial idea for that was originated in this discussion due to it was impossible to implement two times replaceFrom in
{{ if {{ authors match="[^,]+,[^,]+,[^,]+" }} }}{{ authors max="1" suffix="-etal" case="snake" }}
{{ else }}
{{ authors join=" and " case="snake" }}
{{ endif }}
{{ year prefix="-" }}
{{ title truncate="80" replaceFrom="(\.|:|\?)(.*?)$" prefix="-" case="snake" }}
{{ title replaceFrom="\s+" replaceTo="_" regexOpts="g" }}
Actually, I wouldn't need to do that if there was implemented a Pascal_Camel_Snake case option for the template as suggested here https://forums.zotero.org/discussion/120430/fr-add-camel-snake-cases-to-renaming-case-options, after https://github.com/zotero/zotero/pull/4876.
Adding other case options such as Pascal_Camel_Snake wouldn't be so powerful as the template nesting, but probably much easier to implement. Please, add it to the PR.
This is for me the main issue with Zotero 7 and why I have to use yet Zotero 6.
Another is diacritics, which I know you are conscious about it (https://github.com/zotero/zotero/issues/4632). It would be great that Zotero allow to use a function that already exists in a straightforward way.