-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove ampersand escape when writing to bib file (#5869)
* Remove ampersand escape when writing to bib file Instead use save action to handle this case. Includes also a bit of refactoring (mostly renames). * Add missing localization * Update JabRef_en.properties
- Loading branch information
1 parent
24420e7
commit 0407d9a
Showing
25 changed files
with
217 additions
and
150 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
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
95 changes: 95 additions & 0 deletions
95
src/main/java/org/jabref/logic/formatter/bibtexfields/EscapeAmpersandsFormatter.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,95 @@ | ||
package org.jabref.logic.formatter.bibtexfields; | ||
|
||
import java.util.Objects; | ||
|
||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.model.cleanup.Formatter; | ||
|
||
public class EscapeAmpersandsFormatter extends Formatter { | ||
|
||
@Override | ||
public String getName() { | ||
return Localization.lang("Escape ampersands"); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return "escapeAmpersands"; | ||
} | ||
|
||
@Override | ||
public String format(String value) { | ||
Objects.requireNonNull(value); | ||
|
||
StringBuilder result = new StringBuilder(); | ||
|
||
boolean escape = false; | ||
boolean inCommandName = false; | ||
boolean inCommand = false; | ||
boolean inCommandOption = false; | ||
int nestedEnvironments = 0; | ||
StringBuilder commandName = new StringBuilder(); | ||
for (int i = 0; i < value.length(); i++) { | ||
char c = value.charAt(i); | ||
|
||
// Track whether we are in a LaTeX command of some sort. | ||
if (Character.isLetter(c) && (escape || inCommandName)) { | ||
inCommandName = true; | ||
if (!inCommandOption) { | ||
commandName.append(c); | ||
} | ||
} else if (Character.isWhitespace(c) && (inCommand || inCommandOption)) { | ||
// Whitespace | ||
} else if (inCommandName) { | ||
// This means the command name is ended. | ||
// Perhaps the beginning of an argument: | ||
if (c == '[') { | ||
inCommandOption = true; | ||
} else if (inCommandOption && (c == ']')) { | ||
// Or the end of an argument: | ||
inCommandOption = false; | ||
} else if (!inCommandOption && (c == '{')) { | ||
inCommandName = false; | ||
inCommand = true; | ||
} else { | ||
// Or simply the end of this command alltogether: | ||
commandName.delete(0, commandName.length()); | ||
inCommandName = false; | ||
} | ||
} | ||
// If we are in a command body, see if it has ended: | ||
if (inCommand && (c == '}')) { | ||
if ("begin".equals(commandName.toString())) { | ||
nestedEnvironments++; | ||
} | ||
if ((nestedEnvironments > 0) && "end".equals(commandName.toString())) { | ||
nestedEnvironments--; | ||
} | ||
|
||
commandName.delete(0, commandName.length()); | ||
inCommand = false; | ||
} | ||
|
||
// We add a backslash before any ampersand characters, with one exception: if | ||
// we are inside an \\url{...} command, we should write it as it is. Maybe. | ||
if ((c == '&') && !escape && !(inCommand && "url".equals(commandName.toString())) | ||
&& (nestedEnvironments == 0)) { | ||
result.append("\\&"); | ||
} else { | ||
result.append(c); | ||
} | ||
escape = c == '\\'; | ||
} | ||
return result.toString(); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return Localization.lang("Escape ampersands"); | ||
} | ||
|
||
@Override | ||
public String getExampleInput() { | ||
return "Text & with &ersands"; | ||
} | ||
} |
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.