-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Introduce formatter to remove word-enclosing braces #11253
Merged
Merged
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
9d67d15
#11222 fix
rohit-garga 07ba27f
checkstyle fixes
rohit-garga 5ee6946
fixes for unit tests
rohit-garga e5a0a9a
Make new RemoveWordEnclosingAndOuterEnclosingBracesFormatter available
koppor 2ee4699
Add more braces to the example input
koppor 882d31b
Keep original string when formatting empty string
koppor 3d7fa97
Fix RemoveWordEnclosingAndOuterEnclosingBracesFormatter in the case o…
koppor b5ba46b
Fix constant (and empty line)
koppor ac21b4a
Reorder methods
koppor 087efdf
Add CHANGELOG.md entry
koppor 518de66
Merge pull request #1 from koppor/improve-fix-for-issue-11251
rohit-garga 94c425c
Merge branch 'JabRef:main' into fix-for-issue-11251
rohit-garga 67fc88f
markdown issue fix
rohit-garga e446efe
checkstyle fix
rohit-garga 1013d9a
duplicate keys issue fix
rohit-garga 27551c4
author class not being initialized fix
rohit-garga aa0ebf3
reverting key name change for old formatter
rohit-garga 62cfea1
reverting commit- Keep original string when formatting empty string s…
rohit-garga afae4cd
minor fixes
rohit-garga 6083b96
minor fixes
rohit-garga 3e2d6bc
Merge branch 'main' into fix-for-issue-11251
rohit-garga 9ef10c8
minor fixes
rohit-garga 3ee101d
fix: rewrite
rohit-garga 3f85ac1
formatting fix
rohit-garga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...ref/logic/formatter/bibtexfields/RemoveWordEnclosingAndOuterEnclosingBracesFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package org.jabref.logic.formatter.bibtexfields; | ||
|
||
import java.util.Objects; | ||
import java.util.StringJoiner; | ||
|
||
import org.jabref.logic.cleanup.Formatter; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.model.strings.StringUtil; | ||
|
||
import org.jspecify.annotations.NullMarked; | ||
|
||
/** | ||
* Removes start and end brace both at the complete string and at beginning/end of a word | ||
* <p> | ||
* E.g., | ||
* <ul> | ||
* <li>{Vall{\'e}e Poussin} -> Vall{\'e}e Poussin</li> | ||
* <li>{Vall{\'e}e} {Poussin} -> Vall{\'e}e Poussin</li> | ||
* <li>Vall{\'e}e Poussin -> Vall{\'e}e Poussin</li> | ||
* </ul> | ||
*/ | ||
@NullMarked | ||
public class RemoveWordEnclosingAndOuterEnclosingBracesFormatter extends Formatter { | ||
|
||
private static final RemoveEnclosingBracesFormatter REMOVE_ENCLOSING_BRACES_FORMATTER = new RemoveEnclosingBracesFormatter(); | ||
|
||
@Override | ||
public String getName() { | ||
return Localization.lang("Remove word enclosing braces"); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return "remove_enclosing_and_outer_enclosing_braces"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return Localization.lang("Removes braces encapsulating a complete word and the complete field content."); | ||
} | ||
|
||
@Override | ||
public String getExampleInput() { | ||
return "{In {CDMA}}"; | ||
} | ||
|
||
@Override | ||
public String format(String input) { | ||
Objects.requireNonNull(input); | ||
if (StringUtil.isBlank(input)) { | ||
return input; | ||
} | ||
|
||
if (!input.contains("{")) { | ||
return input; | ||
} | ||
|
||
// We need to first remove the outer braces to have double braces at the last word working (e.g., {In {CDMA}}) | ||
input = REMOVE_ENCLOSING_BRACES_FORMATTER.format(input); | ||
|
||
String[] split = input.split(" "); | ||
StringJoiner result = new StringJoiner(" "); | ||
for (String s : split) { | ||
if ((s.length() > 2) && s.startsWith("{") && s.endsWith("}")) { | ||
// quick solution (which we don't do): just remove first "{" and last "}" | ||
// however, it might be that s is like {A}bbb{c}, where braces may not be removed | ||
|
||
String inner = s.substring(1, s.length() - 1); | ||
|
||
if (inner.contains("}")) { | ||
if (properBrackets(inner)) { | ||
s = inner; | ||
} | ||
} else { | ||
// no inner curly brackets found, no check needed, inner can just be used as s | ||
s = inner; | ||
} | ||
} | ||
result.add(s); | ||
} | ||
return result.toString(); | ||
} | ||
|
||
/** | ||
* @return true iff the brackets in s are properly paired | ||
*/ | ||
private boolean properBrackets(String s) { | ||
// nested construct is there, check for "proper" nesting | ||
int i = 0; | ||
int level = 0; | ||
while (i < s.length()) { | ||
char c = s.charAt(i); | ||
switch (c) { | ||
case '{': | ||
level++; | ||
break; | ||
case '}': | ||
level--; | ||
if (level == -1) { // improper nesting | ||
return false; | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
i++; | ||
} | ||
return level == 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,15 @@ | |
import java.util.Objects; | ||
|
||
import org.jabref.logic.cleanup.Formatter; | ||
import org.jabref.logic.formatter.bibtexfields.RemoveEnclosingBracesFormatter; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.logic.protectedterms.ProtectedTermsLoader; | ||
import org.jabref.logic.util.strings.StringLengthComparator; | ||
|
||
/** | ||
* Adds {} brackets around acronyms, month names and countries to preserve their case. | ||
* | ||
* Related formatter: {@link org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter} | ||
* Related formatter: {@link RemoveEnclosingBracesFormatter} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No import for JavaDoc classes please There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not fixed, but checkstyle did not complain. Thus, I let it go. |
||
*/ | ||
public class ProtectTermsFormatter extends Formatter { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think, this is the "old" formatter?
Since it is used in
.layout
files, please don't change the name. Otherwise, we need to add support for alias names....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.
Oh, I could not find references to this key anywhere in the code so I made this more descriptive. Reverting this