feat: add --entry-type-pattern option to citationkeys generate command#15072
Conversation
* Add typePatterns field * Add prepareKeyPatterns method * Add prepareKeyPatterns method call in CitationKeyPatternPreferences constructor call in getCitationKeyGenerator method --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com>
Review Summary by QodoAdd --entry-type-pattern option for per-type citation key configuration
WalkthroughsDescription• Add --entry-type-pattern CLI option to configure citation key patterns per entry type • Allow users to override default patterns for specific entry types (article, book, etc.) • Implement prepareKeyPatterns() method to merge CLI patterns with existing preferences • Support multiple entry type pattern configurations in single command invocation Diagramflowchart LR
CLI["CLI --entry-type-pattern option"] -- "parsed as Map" --> typePatterns["typePatterns field"]
typePatterns -- "prepareKeyPatterns method" --> keyPatterns["GlobalCitationKeyPatterns"]
keyPatterns -- "merged with existing" --> preferences["CitationKeyPatternPreferences"]
preferences -- "used by" --> generator["CitationKeyGenerator"]
File Changes1. jabkit/src/main/java/org/jabref/toolkit/commands/GenerateCitationKeys.java
|
Code Review by Qodo
1.
|
This comment has been minimized.
This comment has been minimized.
|
Please add a changelog entry |
* Add description of the feature in CHANGELOG.md --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com>
|
|
||
| private GlobalCitationKeyPatterns prepareKeyPatterns(CitationKeyPatternPreferences existingPreferences) { | ||
| GlobalCitationKeyPatterns keyPatterns = existingPreferences.getKeyPatterns(); | ||
| typePatterns.forEach((type, pattern) -> { |
| return new CitationKeyGenerator(databaseContext, preferencesToUse); | ||
| } | ||
|
|
||
| private GlobalCitationKeyPatterns prepareKeyPatterns(CitationKeyPatternPreferences existingPreferences) { |
There was a problem hiding this comment.
Pass GlobalCitationKeyPatterns keyPatterns as parameter - not the whole preferences.
Alternatively, you can do
prepareKeyPatterns(typePatterns, existingPreferences.getKeyPatterns())
and do @Nullable Map<...> typePatterns
and do and if and return erly.
Also Add JavaDoc -- Reason: I cannot see at the code why pattern is different from typePatterns
| private Boolean generateBeforeSaving; | ||
|
|
||
| @Option(names = "--entry-type-pattern", description = "Set a key pattern for a specific entry type") | ||
| private Map<String, String> typePatterns; |
There was a problem hiding this comment.
Why did you opt for typePatterns and not for keyPatterns as the preference getter suggests?
https://docs.jabref.org/setup/citationkeypatterns also talks about "key pattern"
| @Option(names = "--generate-before-saving", description = "Generate citation keys before saving") | ||
| private Boolean generateBeforeSaving; | ||
|
|
||
| @Option(names = "--entry-type-pattern", description = "Set a key pattern for a specific entry type") |
There was a problem hiding this comment.
| @Option(names = "--entry-type-pattern", description = "Set a key pattern for a specific entry type") | |
| @Option(names = "--key-patterns", description = "Key patterns for specific entry types") |
* Refactor prepareKeyPatterns method by replacing existingPreferences parameter with two other parameters * Add null check of keyPatternsOption parameter * Add Javadoc for prepareKeyPatterns method * Perlace ternary expression in getCitationKeyGenerator method by prepareKeyPatterns method call * Perlace typePatterns field's name for keyPatterns, and --entry-type-pattern option name for --key-patterns --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com>
| - We added the option to enable/disable the HTTP-Server for the browser extension to the Quick Settings on the Welcome screen [#14902](https://github.com/JabRef/jabref/issues/14902) | ||
| - We added the ability to update bibliographic information based on the existing entry data. [#14185](https://github.com/JabRef/jabref/issues/14185) | ||
| - We added an option to clear [groups with explicitly selected entries](https://docs.jabref.org/finding-sorting-and-cleaning-entries/groups#explicit-selection). [#15001](https://github.com/JabRef/jabref/issues/15001) | ||
| - We added --entry-type-pattern option to CLI parameters to allows users to set a citation key's pattern for a specific entry type. [#14707](https://github.com/JabRef/jabref/issues/14707) |
There was a problem hiding this comment.
Consistency.
| - We added --entry-type-pattern option to CLI parameters to allows users to set a citation key's pattern for a specific entry type. [#14707](https://github.com/JabRef/jabref/issues/14707) | |
| - We added `--key-patterns` option to CLI parameters to allows users to set a citation key's pattern for a specific entry type. [#14707](https://github.com/JabRef/jabref/issues/14707) |
|
Your pull request conflicts with the target branch. Please merge with your code. For a step-by-step guide to resolve merge conflicts, see https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/addressing-merge-conflicts/resolving-a-merge-conflict-using-the-command-line. |
* Refactor changelog entry to match changed option name --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com>
| /// @param keyPatternsOption patterns submitted by a user via --key-patterns option | ||
| /// @param keyPatternsPreferences patterns from preferences | ||
| /// @return keyPatterns from preferences or overridden by user-supplied patterns | ||
| private GlobalCitationKeyPatterns prepareKeyPatterns(@Nullable Map<String, String> keyPatternsOption, GlobalCitationKeyPatterns keyPatternsPreferences) { |
There was a problem hiding this comment.
"prepare" doesn't hint that something is being returned. Can you think of a better name for this method?
There was a problem hiding this comment.
There was a problem hiding this comment.
getX by convention doesn't take parameters, but I am okay with it. It's more of a nitpick.
* Change prepareKeyPatterns method's name to getKeyPatterns to highlight that something is being returned by this method --------- Co-authored-by: Subhramit Basu <subhramit.bb@live.in>
This comment has been minimized.
This comment has been minimized.
* Move changelog entry the Unreleased section ---------
This comment has been minimized.
This comment has been minimized.
|
Can you please comment on qodos comments? #15072 (comment) |
|
@calixtus, yes. I'll leave a comment on the first required action here since cannot leave a reply to the bot's code review message.
As the bot suggested, --suffix CLI option doesn't work with lowercase arguments. It makes sense to add the converter class to this option. |
|
Do I need to provide solutions for these 3 required actions? |
|
See if you can make a simple Converter for 1 and a copy of the keypatterns for 3 to avoid future side effects by uknowing contributors. |
…defensive copy * Add converter to '--suffix' CLI option for lowercase arguments * Add inform message if a user passed invalid entry type * Add defensive copy for the keyPatternsPreferences variable --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
|
@DawydowGerman please address the failing unit tests on localization |
* Add localization key in JabRef_en.properties file for getKeyPatterns method ---------
This comment has been minimized.
This comment has been minimized.
✅ All tests passed ✅🏷️ Commit: 369954e Learn more about TestLens at testlens.app. |
|
|
||
| ## [Unreleased] | ||
|
|
||
| - We added `--key-patterns` option to CLI parameters to allows users to set a citation key's pattern for a specific entry type. [#14707](https://github.com/JabRef/jabref/issues/14707) |
There was a problem hiding this comment.
Wrong position in CHANGELOG file...
There was a problem hiding this comment.
Overlooked. Will do a cleanup commit
* upstream/main: fix jbang (JabRef#15311) New Crowdin updates (JabRef#15310) Fix exception in ExtractReferences from pdf (JabRef#15308) feat: add --entry-type-pattern option to citationkeys generate command (JabRef#15072) Fix reflection excpetion, add missing (JabRef#15307)
JabRef#15072) * feat: add --entry-type-pattern option to citationkeys generate command * Add typePatterns field * Add prepareKeyPatterns method * Add prepareKeyPatterns method call in CitationKeyPatternPreferences constructor call in getCitationKeyGenerator method --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com> * docs: add description in CHANGELOG.md * Add description of the feature in CHANGELOG.md --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com> * refactor: change prepareKeyPatterns method, add JavaDoc * Refactor prepareKeyPatterns method by replacing existingPreferences parameter with two other parameters * Add null check of keyPatternsOption parameter * Add Javadoc for prepareKeyPatterns method * Perlace ternary expression in getCitationKeyGenerator method by prepareKeyPatterns method call * Perlace typePatterns field's name for keyPatterns, and --entry-type-pattern option name for --key-patterns --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com> * docs: minor changelog entry edit * Refactor changelog entry to match changed option name --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com> * refactor: change method's name * Change prepareKeyPatterns method's name to getKeyPatterns to highlight that something is being returned by this method --------- Co-authored-by: Subhramit Basu <subhramit.bb@live.in> * docs: minor changelog entry edit * Move changelog entry the Unreleased section --------- * refactor: add converter to --suffix, inform message for invalid key, defensive copy * Add converter to '--suffix' CLI option for lowercase arguments * Add inform message if a user passed invalid entry type * Add defensive copy for the keyPatternsPreferences variable --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> * docs: add localization key * Add localization key in JabRef_en.properties file for getKeyPatterns method --------- --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com> Co-authored-by: Subhramit Basu <subhramit.bb@live.in> Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>
JabRef#15072) * feat: add --entry-type-pattern option to citationkeys generate command * Add typePatterns field * Add prepareKeyPatterns method * Add prepareKeyPatterns method call in CitationKeyPatternPreferences constructor call in getCitationKeyGenerator method --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com> * docs: add description in CHANGELOG.md * Add description of the feature in CHANGELOG.md --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com> * refactor: change prepareKeyPatterns method, add JavaDoc * Refactor prepareKeyPatterns method by replacing existingPreferences parameter with two other parameters * Add null check of keyPatternsOption parameter * Add Javadoc for prepareKeyPatterns method * Perlace ternary expression in getCitationKeyGenerator method by prepareKeyPatterns method call * Perlace typePatterns field's name for keyPatterns, and --entry-type-pattern option name for --key-patterns --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com> * docs: minor changelog entry edit * Refactor changelog entry to match changed option name --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com> * refactor: change method's name * Change prepareKeyPatterns method's name to getKeyPatterns to highlight that something is being returned by this method --------- Co-authored-by: Subhramit Basu <subhramit.bb@live.in> * docs: minor changelog entry edit * Move changelog entry the Unreleased section --------- * refactor: add converter to --suffix, inform message for invalid key, defensive copy * Add converter to '--suffix' CLI option for lowercase arguments * Add inform message if a user passed invalid entry type * Add defensive copy for the keyPatternsPreferences variable --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> * docs: add localization key * Add localization key in JabRef_en.properties file for getKeyPatterns method --------- --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com> Co-authored-by: Subhramit Basu <subhramit.bb@live.in> Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>
JabRef#15072) * feat: add --entry-type-pattern option to citationkeys generate command * Add typePatterns field * Add prepareKeyPatterns method * Add prepareKeyPatterns method call in CitationKeyPatternPreferences constructor call in getCitationKeyGenerator method --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com> * docs: add description in CHANGELOG.md * Add description of the feature in CHANGELOG.md --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com> * refactor: change prepareKeyPatterns method, add JavaDoc * Refactor prepareKeyPatterns method by replacing existingPreferences parameter with two other parameters * Add null check of keyPatternsOption parameter * Add Javadoc for prepareKeyPatterns method * Perlace ternary expression in getCitationKeyGenerator method by prepareKeyPatterns method call * Perlace typePatterns field's name for keyPatterns, and --entry-type-pattern option name for --key-patterns --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com> * docs: minor changelog entry edit * Refactor changelog entry to match changed option name --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com> * refactor: change method's name * Change prepareKeyPatterns method's name to getKeyPatterns to highlight that something is being returned by this method --------- Co-authored-by: Subhramit Basu <subhramit.bb@live.in> * docs: minor changelog entry edit * Move changelog entry the Unreleased section --------- * refactor: add converter to --suffix, inform message for invalid key, defensive copy * Add converter to '--suffix' CLI option for lowercase arguments * Add inform message if a user passed invalid entry type * Add defensive copy for the keyPatternsPreferences variable --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> * docs: add localization key * Add localization key in JabRef_en.properties file for getKeyPatterns method --------- --------- Co-authored-by: Carl Christian Snethlage <calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com> Co-authored-by: Subhramit Basu <subhramit.bb@live.in> Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>
Closes #14707
Added --key-patterns option to the citationkeys generate command which allows users to set a key pattern for a specific entry type.
Steps to test
./gradlew :jabkit:run --args="citationkeys generate --help"Output:

1.1. Create the next bib file:
@Article{,
author = {Author0},
title = {Title0},
year = {2024},
publisher = {Publisher0},
}
@book{,
author = {Author1},
title = {Title1},
year = {2025},
publisher = {Publisher1},
}
@booklet{,
author = {Author2},
title = {Title2},
year = {2026},
publisher = {Publisher2},
}
{ ./gradlew :jabkit:run --args="citationkeys generate \ --input /home/german/IdeaProjects/jabref/test.bib \ --output /home/german/IdeaProjects/jabref/result.bib \ --key-patterns article='[auth:abbr]_[year]' \ --key-patterns book='[year][auth]' \ --key-patterns booklet='[year]_[auth:lower]'" cat /home/german/IdeaProjects/jabref/result.bib }Output:

{ ./gradlew :jabkit:run --args="citationkeys generate \ --input /home/german/IdeaProjects/jabref/test.bib \ --output /home/german/IdeaProjects/jabref/result.bib \ --key-patterns article='[auth:lower]__[year]' \ --key-patterns book='[auth:upper]__[year]' \ --key-patterns booklet='[year]_[auth:upper]'" cat /home/german/IdeaProjects/jabref/result.bib }Output:

Checklist
CHANGELOG.mdin a way that is understandable for the average user (if change is visible to the user)