Skip to content
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 24 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9d67d15
#11222 fix
rohit-garga Apr 26, 2024
07ba27f
checkstyle fixes
rohit-garga Apr 26, 2024
5ee6946
fixes for unit tests
rohit-garga Apr 26, 2024
e5a0a9a
Make new RemoveWordEnclosingAndOuterEnclosingBracesFormatter available
koppor Apr 29, 2024
2ee4699
Add more braces to the example input
koppor Apr 29, 2024
882d31b
Keep original string when formatting empty string
koppor Apr 29, 2024
3d7fa97
Fix RemoveWordEnclosingAndOuterEnclosingBracesFormatter in the case o…
koppor Apr 29, 2024
b5ba46b
Fix constant (and empty line)
koppor Apr 29, 2024
ac21b4a
Reorder methods
koppor Apr 29, 2024
087efdf
Add CHANGELOG.md entry
koppor Apr 29, 2024
518de66
Merge pull request #1 from koppor/improve-fix-for-issue-11251
rohit-garga Apr 29, 2024
94c425c
Merge branch 'JabRef:main' into fix-for-issue-11251
rohit-garga May 1, 2024
67fc88f
markdown issue fix
rohit-garga May 1, 2024
e446efe
checkstyle fix
rohit-garga May 1, 2024
1013d9a
duplicate keys issue fix
rohit-garga May 1, 2024
27551c4
author class not being initialized fix
rohit-garga May 1, 2024
aa0ebf3
reverting key name change for old formatter
rohit-garga May 1, 2024
62cfea1
reverting commit- Keep original string when formatting empty string s…
rohit-garga May 1, 2024
afae4cd
minor fixes
rohit-garga May 4, 2024
6083b96
minor fixes
rohit-garga May 7, 2024
3e2d6bc
Merge branch 'main' into fix-for-issue-11251
rohit-garga May 7, 2024
9ef10c8
minor fixes
rohit-garga May 7, 2024
3ee101d
fix: rewrite
rohit-garga May 7, 2024
3f85ac1
formatting fix
rohit-garga May 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We added value selection (such as for month) for content selectors in custom entry types. [#11109](https://github.com/JabRef/jabref/issues/11109)
- We added a duplicate checker for the Citation Relations tab. [#10414](https://github.com/JabRef/jabref/issues/10414)
- We added tooltip on main table cells that shows cell content or cell content and entry preview if set in preferences. [10925](https://github.com/JabRef/jabref/issues/10925)
- Added a formatter to remove word enclosing braces. [#11222](https://github.com/JabRef/jabref/issues/11222)
- We added the ability to add a keyword/crossref when typing the separator character (e.g., comma) in the keywords/crossref fields. [#11178](https://github.com/JabRef/jabref/issues/11178)
- We added an exporter and improved the importer for Endnote XML format. [#11137](https://github.com/JabRef/jabref/issues/11137)
- We added support for automatically update LaTeX citations when a LaTeX file is created, removed, or modified. [#10585](https://github.com/JabRef/jabref/issues/10585)
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/jabref/logic/formatter/Formatters.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
import org.jabref.logic.formatter.bibtexfields.NormalizeUnicodeFormatter;
import org.jabref.logic.formatter.bibtexfields.OrdinalsToSuperscriptFormatter;
import org.jabref.logic.formatter.bibtexfields.RegexFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveEnclosingBracesFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveWordEnclosingAndOuterEnclosingBracesFormatter;
import org.jabref.logic.formatter.bibtexfields.ShortenDOIFormatter;
import org.jabref.logic.formatter.bibtexfields.UnicodeToLatexFormatter;
import org.jabref.logic.formatter.bibtexfields.UnitsToLatexFormatter;
Expand Down Expand Up @@ -82,7 +83,8 @@ public static List<Formatter> getOthers() {
new NormalizeNamesFormatter(),
new NormalizePagesFormatter(),
new OrdinalsToSuperscriptFormatter(),
new RemoveBracesFormatter(),
new RemoveEnclosingBracesFormatter(),
new RemoveWordEnclosingAndOuterEnclosingBracesFormatter(),
new UnitsToLatexFormatter(),
new EscapeUnderscoresFormatter(),
new EscapeAmpersandsFormatter(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.jspecify.annotations.NullMarked;

@NullMarked
public class RemoveBracesFormatter extends Formatter {
public class RemoveEnclosingBracesFormatter extends Formatter {

@Override
public String getName() {
Expand All @@ -18,6 +18,16 @@ public String getKey() {
return "remove_braces";
Copy link
Member

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....

Copy link
Contributor Author

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

}

@Override
public String getDescription() {
return Localization.lang("Removes braces encapsulating the complete field content.");
}

@Override
public String getExampleInput() {
return "{In CDMA}";
}

@Override
public String format(String value) {
String formatted = value;
Expand All @@ -37,16 +47,6 @@ public String format(String value) {
return formatted;
}

@Override
public String getDescription() {
return Localization.lang("Removes braces encapsulating the complete field content.");
}

@Override
public String getExampleInput() {
return "{In CDMA}";
}

/**
* Check if a string at any point has had more ending } braces than opening { ones.
* Will e.g. return true for the string "DNA} text {EPA"
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No import for JavaDoc classes please

Copy link
Member

Choose a reason for hiding this comment

The 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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.jabref.logic.formatter.bibtexfields.ClearFormatter;
import org.jabref.logic.formatter.bibtexfields.NormalizeMonthFormatter;
import org.jabref.logic.formatter.bibtexfields.NormalizeNamesFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveEnclosingBracesFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveNewlinesFormatter;
import org.jabref.logic.help.HelpFile;
import org.jabref.logic.importer.EntryBasedParserFetcher;
Expand Down Expand Up @@ -149,9 +149,9 @@ public Parser getParser() {

@Override
public void doPostCleanup(BibEntry entry) {
new FieldFormatterCleanup(StandardField.ABSTRACT, new RemoveBracesFormatter()).cleanup(entry);
new FieldFormatterCleanup(StandardField.ABSTRACT, new RemoveEnclosingBracesFormatter()).cleanup(entry);
new FieldFormatterCleanup(StandardField.ABSTRACT, new RemoveNewlinesFormatter()).cleanup(entry);
new FieldFormatterCleanup(StandardField.TITLE, new RemoveBracesFormatter()).cleanup(entry);
new FieldFormatterCleanup(StandardField.TITLE, new RemoveEnclosingBracesFormatter()).cleanup(entry);
new FieldFormatterCleanup(StandardField.AUTHOR, new NormalizeNamesFormatter()).cleanup(entry);
new FieldFormatterCleanup(StandardField.MONTH, new NormalizeMonthFormatter()).cleanup(entry);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/logic/importer/fetcher/CrossRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import org.jabref.logic.cleanup.FieldFormatterCleanup;
import org.jabref.logic.formatter.bibtexfields.ClearFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveEnclosingBracesFormatter;
import org.jabref.logic.importer.EntryBasedParserFetcher;
import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.IdBasedParserFetcher;
Expand Down Expand Up @@ -46,7 +46,7 @@ public class CrossRef implements IdParserFetcher<DOI>, EntryBasedParserFetcher,

private static final String API_URL = "https://api.crossref.org/works";

private static final RemoveBracesFormatter REMOVE_BRACES_FORMATTER = new RemoveBracesFormatter();
private static final RemoveEnclosingBracesFormatter REMOVE_BRACES_FORMATTER = new RemoveEnclosingBracesFormatter();

@Override
public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import org.jabref.logic.cleanup.FieldFormatterCleanup;
import org.jabref.logic.formatter.bibtexfields.ClearFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveEnclosingBracesFormatter;
import org.jabref.logic.help.HelpFile;
import org.jabref.logic.importer.EntryBasedFetcher;
import org.jabref.logic.importer.FetcherException;
Expand Down Expand Up @@ -76,7 +76,7 @@ public void doPostCleanup(BibEntry entry) {
new FieldFormatterCleanup(new UnknownField("SLACcitation"), new ClearFormatter()).cleanup(entry);

// Remove braces around content of "title" field
new FieldFormatterCleanup(StandardField.TITLE, new RemoveBracesFormatter()).cleanup(entry);
new FieldFormatterCleanup(StandardField.TITLE, new RemoveEnclosingBracesFormatter()).cleanup(entry);

new FieldFormatterCleanup(StandardField.TITLE, new LatexToUnicodeFormatter()).cleanup(entry);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/logic/importer/fetcher/ZbMATH.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import org.jabref.logic.cleanup.FieldFormatterCleanup;
import org.jabref.logic.cleanup.MoveFieldCleanup;
import org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveEnclosingBracesFormatter;
import org.jabref.logic.importer.EntryBasedParserFetcher;
import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.IdBasedParserFetcher;
Expand Down Expand Up @@ -131,7 +131,7 @@ public Parser getParser() {
public void doPostCleanup(BibEntry entry) {
new MoveFieldCleanup(new UnknownField("msc2010"), StandardField.KEYWORDS).cleanup(entry);
new MoveFieldCleanup(AMSField.FJOURNAL, StandardField.JOURNAL).cleanup(entry);
new FieldFormatterCleanup(StandardField.JOURNAL, new RemoveBracesFormatter()).cleanup(entry);
new FieldFormatterCleanup(StandardField.TITLE, new RemoveBracesFormatter()).cleanup(entry);
new FieldFormatterCleanup(StandardField.JOURNAL, new RemoveEnclosingBracesFormatter()).cleanup(entry);
new FieldFormatterCleanup(StandardField.TITLE, new RemoveEnclosingBracesFormatter()).cleanup(entry);
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/logic/msbib/MSBibConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.List;
import java.util.Optional;

import org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveEnclosingBracesFormatter;
import org.jabref.model.entry.AuthorList;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.Month;
Expand All @@ -18,7 +18,7 @@ public class MSBibConverter {

private static final String MSBIB_PREFIX = "msbib-";
private static final String BIBTEX_PREFIX = "BIBTEX_";
private static final RemoveBracesFormatter REMOVE_BRACES_FORMATTER = new RemoveBracesFormatter();
private static final RemoveEnclosingBracesFormatter REMOVE_BRACES_FORMATTER = new RemoveEnclosingBracesFormatter();

private MSBibConverter() {
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/logic/msbib/MsBibAuthor.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.jabref.logic.msbib;

import org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveEnclosingBracesFormatter;
import org.jabref.model.entry.Author;

public class MsBibAuthor {

private static final RemoveBracesFormatter REMOVE_BRACES_FORMATTER = new RemoveBracesFormatter();
private static final RemoveEnclosingBracesFormatter REMOVE_BRACES_FORMATTER = new RemoveEnclosingBracesFormatter();

private String firstName;
private String middleName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;
import java.util.Optional;

import org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveEnclosingBracesFormatter;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.Author;
import org.jabref.model.entry.AuthorList;
Expand All @@ -24,7 +24,7 @@

class OOBibStyleGetCitationMarker {

private static final RemoveBracesFormatter REMOVE_BRACES_FORMATTER = new RemoveBracesFormatter();
private static final RemoveEnclosingBracesFormatter REMOVE_BRACES_FORMATTER = new RemoveEnclosingBracesFormatter();

private OOBibStyleGetCitationMarker() {
}
Expand Down
Loading
Loading