Hi,
I commonly use the Replace() export formatter but I can’t get it to find and replace double quotes.
To reproduce:
1-Change the “Customized preview style” to:
\format[Replace(“\”,")]{\author}
2-Add a double quote (") to the AUTHOR field of any entry.
3-The preview show the preview code instead of the string without double quote.
I tried to add backslashes to better escape the double quote in the preview quote without success:
\format[Replace(“\\”,“)]{\author}
\format[Replace(”\\\“,”)]{\author}
…
Any idea?
Thanks,
Pierre
ThiloteE
(ThiloteE)
January 3, 2023, 10:07pm
2
have you tried
\format[Replace(“”,“)]{\author}
or \format[Replace(\“\”,\“)]{\author}
or \format[Replace(\\“\\”,\\“)]{\author}
?
Just tried and no success either.
Hi,
that was a tricky one. I only figured this out while having the debugger opened at the same time to see what comes down to the method in the end. Had to do something with the number of quotes etc.
\format[Replace("\",)]{\author}
Maybe you can add that as an example to the docs for the Replace formatter:
Edit// If you have modified the preview style you need to switch back and forth to the entry. Otherwise, it’s not updated correctly
Thanks,
That works when \" is the only rule in the regex but as soon as you try to add, for example, other matching characters:
\format[Replace("(x|"),)]{\author}
it fails.
I guess the proper solution would be to get Replace to unescape " properly.
Hi,
hm from what I saw there are two separate issues. One is the parsing of the expression itself (seems to fail if you only have a single quote "
and the second one is the parsing of the argument string:
The argument parsing happens here for Replace e.g. For The Replace it needs two parts. It seems like it does check for some escaping stuff, but I did not take a deeper look. Maybe this helps you:
/**
* Parse an argument string and return the parts of the argument. The parts are
* separated by commas, and escaped commas are reduced to literal commas.
*
* @param arg The argument string.
* @return A list of strings representing the parts of the argument.
*/
protected static List<String> parseArgument(String arg) {
I’m not able to test but I think:
if ((arg.charAt(i) != ‘,’) && (arg.charAt(i) != ‘"’)) {
should be replaced by:
if (!((arg.charAt(i) == ‘,’) || ((arg.charAt(i) == ‘"’) && (i == arg.length() - 1)))) {
i.e. Keep the escaped double quotes escaped unless it is the last character.
@pedroracine Thanks for the code, I made a PR with the changes so you can test it
Download version:
JabRef:main
← JabRef:betterlayoutparsing
opened 07:43PM - 04 Jan 23 UTC
Refs https://discourse.jabref.org/t/replace-double-quotes-using-the-replace-expo… rt-formatter/3697/7
<!--
Describe the changes you have made here: what, why, ...
Link issues that are fixed, e.g. "Fixes #333".
If you fixed a koppor issue, link it, e.g. "Fixes https://github.com/koppor/jabref/issues/47".
The title of the PR must not reference an issue, because GitHub does not support autolinking there.
-->
<!--
- Go through the list below. Please don't remove any items.
- [x] done; [ ] not done / not applicable
-->
- [ ] Change in `CHANGELOG.md` described in a way that is understandable for the average user (if applicable)
- [ ] Tests created for changes (if applicable)
- [ ] Manually tested changed features in running JabRef (always required)
- [ ] Screenshots added in PR description (for UI changes)
- [ ] [Checked developer's documentation](https://devdocs.jabref.org/): Is the information available and up to date? If not, I outlined it in this pull request.
- [ ] [Checked documentation](https://docs.jabref.org/): Is the information available and up to date? If not, I created an issue at <https://github.com/JabRef/user-documentation/issues> or, even better, I submitted a pull request to the documentation repository.
(You can open the PR and then just somewhere press the . dot key and github will open an online editor)
Hm, this is bad. Can you give a sample bib entry with how it looks before the Replace and after the replacement (e.g. what is the expected outcome? Then I can try to write a test for this function.