-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Normalize pages formatter not replacing dashes (#7243)
Co-authored-by: Oliver Kopp <kopp.dev@gmail.com>
- Loading branch information
1 parent
5ba38da
commit eca13dd
Showing
8 changed files
with
225 additions
and
118 deletions.
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
63 changes: 63 additions & 0 deletions
63
src/main/java/org/jabref/logic/formatter/casechanger/UnprotectTermsFormatter.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,63 @@ | ||
package org.jabref.logic.formatter.casechanger; | ||
|
||
import java.util.Objects; | ||
|
||
import org.jabref.logic.cleanup.Formatter; | ||
import org.jabref.logic.l10n.Localization; | ||
|
||
/** | ||
* Remove {} braces around words in case they appear balanced | ||
* | ||
* Related formatter: {@link ProtectTermsFormatter} | ||
*/ | ||
public class UnprotectTermsFormatter extends Formatter { | ||
|
||
@Override | ||
public String format(String text) { | ||
// similar implementation at {@link org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter.hasNegativeBraceCount} | ||
Objects.requireNonNull(text); | ||
if (text.isEmpty()) { | ||
return text; | ||
} | ||
StringBuilder result = new StringBuilder(); | ||
int level = 0; | ||
int index = 0; | ||
do { | ||
char charAtIndex = text.charAt(index); | ||
if (charAtIndex == '{') { | ||
level++; | ||
} else if (charAtIndex == '}') { | ||
level--; | ||
} else { | ||
result.append(charAtIndex); | ||
} | ||
index++; | ||
} while (index < text.length() && level >= 0); | ||
if (level != 0) { | ||
// in case of unbalanced braces, the original text is returned unmodified | ||
return text; | ||
} | ||
return result.toString(); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return Localization.lang( | ||
"Removes all balanced {} braces around words."); | ||
} | ||
|
||
@Override | ||
public String getExampleInput() { | ||
return "{In} {CDMA}"; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return Localization.lang("Unprotect terms"); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return "unprotect_terms"; | ||
} | ||
} |
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.