-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Perform a regex substitution in the substitute plugin #5357
base: master
Are you sure you want to change the base?
Conversation
Thank you for the PR! The changelog has not been updated, so here is a friendly reminder to check if you need to add an entry. |
Hi! Thanks for the PR. Are you comfortable adding tests for this enhancement? Anything with regex should be heavily tested to make sure it behaves exactly as expected. |
0c77263
to
3403a05
Compare
Thanks for the feedback, and sorry it's taken me so long to respond. I've added some testing now, although I've intentionally left one failing. I had assumed that the order of properties would be preserved when loading the configuration, but this does not seem to be the case. This means it's quite unpredictable what rule will be triggered if the input matches several rules. This would also be the case in the current version of the plugin. Perhaps the configuration should be refactored into a list of rules, rather than an object, so order is preserved? Maybe you have thoughts, as the creator of the plugin @fdaniele85? |
Hi! I agree that it should be a list of rules and order should be preserved. I never had an overlap of two rules, therefore I did not think about this case. |
0adacb0
to
664a5d6
Compare
I've updated the configuration to use a list of rules, while deprecating the previous syntax. Old syntax: substitute:
a: b
c: d New syntax: substitute:
- a: b
- c: d |
664a5d6
to
9bc586d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added several comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great!!
Description
This utilises regex substitution in the substitute plugin. The previous approach only used regex to match the pattern, then replaced it with a static string. This change allows more complex substitutions, where the output depends on the input.
Example use case
Say we want to keep only the first artist of a multi-artist credit, as in the following list:
This would previously have required three separate rules, one for each resulting artist. By using a regex substitution, we can get the desired behaviour in a single rule:
(Capture the text until the first
,
&
orand
, then use that capture group as the output)Notes
I've kept the previous behaviour of only applying the first matching rule, but I'm not 100% sure it's the ideal approach.
I can imagine both cases where you want to apply several rules in sequence and cases where you want to stop after the first match.
To Do
docs/
to describe it.)docs/changelog.rst
to the bottom of one of the lists near the top of the document.)