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

[FR] Regex Failure gives Does Not Exist condition #608

Open Servinjesus1 opened 2 years ago

Servinjesus1 commented 2 years ago

Currently, failing a regex check in a custom wildcard returns the input field unaltered. I think this is odd behavior since optional wildcards exist. These allow for users to do e.g. {%1} | {%t} if their custom wildcard %1 tries to regex the title field, and they really want the title field back undoctored if their regex fails, but only if the custom wildcard returns a "does not exist" condition rather than its current behavior. I'm not sure what DNE looks like in a Zotfile wildcard (an empty string?), but I could imagine it feasible to make regex evaluations return this if there's no match.

For reference, the naming scheme I'm striving for will do something like: Scientist1993-JJ~Mitochondr...house_Cell

by default, but if the field Name exists: Scientist1993-JJ~Mitochondria_Powers_Cells

I've got it down to: {%b~}{ {%1} | { {%2} { %3 | {...%4} } } }/

where %1 would be the custom field Name. If that does not yet exist, default to %2, a regex for the first 10 "interesting" characters of the title2, plus %3, a regex for the last 10 "interesting" characters of the title, if the title is 20 "interesting" characters long, otherwise ...%4, roll up the middle part and keep the last 10 "interesting" characters. To get this done, I would need this Regex DNE feature and this feature.

gabrielkulp commented 2 years ago

I agree pretty strongly with this. In my case, I want a naming scheme that behaves differently based on if the BBT citation key is pinned. You can easily check if the citation key is pinned by checking the extra field, but i think it might be impossible because:

"If the match fails, this operation returns the input data"

Here's my current custom wildcard attempt that doesn't work as expected:

{"1": {"field": "extra", "operations": [{"function": "exec", "regex": "(Citation Key: )(\\w*|\\d*)", "group": 2}] } }

And here is an example naming rule that relies on the expected behavior: {%1|%t}{ - %a}{ - %y}

In this specific case, an alternative solution would be a first-class "pinned citationkey" wildcard that's blank when unpinned, but I imagine that wouldn't be so nice because of the BBT dependency. I think a lightweight solution is to make failed matches count as empty fields.

Servinjesus1 commented 1 year ago

Workaround is to use regex replacement, not exec. This more powerful tool does multiple matching, so you can define something like:

    "1":
    {
        "field": "title",
        "operations":
        [
            {
                "function": "replace",
                "regex": "(^.{10}).*(.{14}$)",
                "replacement": "$1..$2"
            }
        ]
    }

as one of your custom replacement rules. In this case, it truncates anything longer than 24 characters to <first 10>..<last 14>.