jlegewie / zotfile

Zotero plugin to manage your attachments: automatically rename, move, and attach PDFs (or other files) to Zotero items, sync PDFs from your Zotero library to your (mobile) PDF reader (e.g. an iPad, Android tablet, etc.), and extract PDF annotations.
4.05k stars 282 forks source link

Adding default wildcards for Date (ISO 8601), month and day #644

Open mikkovedru opened 1 year ago

mikkovedru commented 1 year ago

Currently, Zotfile has different wildcards including the wildcard for a year (%y). It's natural and expected that such a field would exist.

But sadly many other natural and expected wildcards don't exist:

To get those one has to work with the unsupported user-defined wildcards using replace functions, regex, etc. This is a very hard and tedious thing to do for those who know how to do it and it's basically impossible for those who don't.

Could you please add those fields? Are patches welcome or going to be ignored?

joshleitzel commented 1 year ago

In case this is helpful for anyone in the future, I needed this functionality as well so I designed a custom wildcard to output the ISO format. Here it is:

{
    "z": {
        "field": "date",
        "operations": [
            {
                "function": "replace",
                "regex": "([^\\d]*)(\\d{1,2})([^\\d]*)(\\d{1,2})([^\\d]*)(\\d{4})",
                "replacement": "$6-0$2-0$4",
                "flags": "g"
            },
            {
                "function": "replace",
                "regex": "(\\d{4})-\\d?(\\d{2})-\\d?(\\d{2})",
                "replacement": "$1-$2-$3",
                "flags": "g"
            }
        ]
    }
}

You can set this up by following the instructions in the docs:

  1. Open your Zotero settings
  2. Go to Advanced > Config Editor
  3. Search for "wildcard" and double-click the extensions.zotfile.wildcards.user item
  4. In the popup-box that appears, paste the code snippet I posted above

Now you can use the wildcard %z anywhere you want the date to appear.

For the programming-minded who may be curious about the strange regexes, I discovered that Zotero seems to use some invisible characters in between the digits in the dates. So it's necessary to isolate them using the ^\\d sequences. The second replace operation removes the leading 0 from the month and day that was added in the first operation, in case it doesn't need one.