-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Added a test case in CitationKeyGeneratorTest.java file #12194
Changes from 35 commits
c407a30
581fb61
8f66b40
df2d656
f2559e5
f845041
dd1f97f
3b1eda2
784f756
19a80c3
a5a37b9
6c4e0a8
beacb03
0368236
b70ad43
8e4a7e1
6fff450
85befed
07064db
c53891f
4868e0d
912e1b2
74dc490
f1c8535
97f968b
4899a18
28f9f2f
ff7925f
d41a9d7
3c00c29
8d9845b
83c5841
89b1106
d95e7e1
83e3c06
6ecdb03
9cc84a1
36a42f3
25ce9a9
0d2bea3
ab938b0
89d2f05
3e69e0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,68 +4,53 @@ | |
import org.jabref.model.strings.StringUtil; | ||
|
||
public class RemoveLatexCommandsFormatter implements LayoutFormatter { | ||
|
||
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. Why was there a need to touch this class? |
||
@Override | ||
public String format(String field) { | ||
StringBuilder cleanedField = new StringBuilder(); | ||
StringBuilder currentCommand = null; | ||
char currentCharacter; | ||
boolean escaped = false; | ||
boolean incommand = false; | ||
int currentFieldPosition; | ||
for (currentFieldPosition = 0; currentFieldPosition < field.length(); currentFieldPosition++) { | ||
currentCharacter = field.charAt(currentFieldPosition); | ||
if (escaped && (currentCharacter == '\\')) { | ||
cleanedField.append('\\'); | ||
escaped = false; | ||
// \\ --> first \ begins the command, second \ ends the command | ||
// \latexommand\\ -> \latexcommand is the command, terminated by \, which begins a new command | ||
incommand = false; | ||
} else if (currentCharacter == '\\') { | ||
|
||
for (int currentFieldPosition = 0; currentFieldPosition < field.length(); currentFieldPosition++) { | ||
char currentCharacter = field.charAt(currentFieldPosition); | ||
|
||
if (currentCharacter == '\\') { | ||
// Start of a LaTeX command | ||
escaped = true; | ||
incommand = true; | ||
currentCommand = new StringBuilder(); | ||
} else if (!incommand && ((currentCharacter == '{') || (currentCharacter == '}'))) { | ||
// Swallow the brace. | ||
} else if (Character.isLetter(currentCharacter) || StringUtil.SPECIAL_COMMAND_CHARS.contains(String.valueOf(currentCharacter))) { | ||
escaped = false; | ||
if (incommand) { | ||
currentCommand.append(currentCharacter); | ||
if ((currentCommand.length() == 1) | ||
&& StringUtil.SPECIAL_COMMAND_CHARS.contains(currentCommand.toString())) { | ||
// This indicates that we are in a command of the type \^o or \~{n} | ||
incommand = false; | ||
escaped = false; | ||
} | ||
} else { | ||
cleanedField.append(currentCharacter); | ||
} | ||
} else if (Character.isLetter(currentCharacter)) { | ||
escaped = false; | ||
if (incommand) { | ||
// We are in a command, and should not keep the letter. | ||
} else if (incommand) { | ||
// Collect characters for the LaTeX command name | ||
if (Character.isLetter(currentCharacter) || | ||
StringUtil.SPECIAL_COMMAND_CHARS.contains(String.valueOf(currentCharacter))) { | ||
currentCommand.append(currentCharacter); | ||
} else { | ||
cleanedField.append(currentCharacter); | ||
} | ||
} else { | ||
if (!incommand || (!Character.isWhitespace(currentCharacter) && (currentCharacter != '{'))) { | ||
cleanedField.append(currentCharacter); | ||
} else { | ||
if (!Character.isWhitespace(currentCharacter) && (currentCharacter != '{')) { | ||
// do not append the opening brace of a command parameter | ||
// do not append the whitespace character | ||
cleanedField.append(currentCharacter); | ||
} | ||
if (incommand) { | ||
// eat up all whitespace characters | ||
while (currentFieldPosition + 1 < field.length() && Character.isWhitespace(field.charAt(currentFieldPosition + 1))) { | ||
currentFieldPosition++; | ||
} else if (currentCharacter == '{') { | ||
// Found the start of the command's parameters; skip the command name | ||
int braceLevel = 1; | ||
currentFieldPosition++; // Move past the opening brace | ||
while (currentFieldPosition < field.length() && braceLevel > 0) { | ||
currentCharacter = field.charAt(currentFieldPosition); | ||
if (currentCharacter == '{') { | ||
braceLevel++; | ||
} else if (currentCharacter == '}') { | ||
braceLevel--; | ||
} | ||
// Append parameter content if we're still inside the braces | ||
if (braceLevel > 0) { | ||
cleanedField.append(currentCharacter); | ||
} | ||
currentFieldPosition++; | ||
} | ||
incommand = false; | ||
escaped = false; | ||
} else { | ||
// End of the command (no parameters) | ||
incommand = false; | ||
escaped = false; | ||
} | ||
incommand = false; | ||
escaped = false; | ||
} else if (currentCharacter != '{' && currentCharacter != '}') { | ||
// Append regular characters (non-command, non-braces) | ||
cleanedField.append(currentCharacter); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,16 @@ void crossrefAndInAuthorNames() { | |
database), DEFAULT_UNWANTED_CHARACTERS)); | ||
} | ||
|
||
|
||
@Test | ||
void latexCommandsAreStrippedFromCitationKey() throws ParseException { | ||
BibEntry entry = new BibEntry() | ||
.withField(StandardField.TITLE, "Building \\mkbibquote{Community}"); | ||
String pattern = "[bibtexkey] - [fulltitle]"; | ||
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. Now I understand where your hacks with |
||
String generatedKey = generateKey(entry, pattern); | ||
assertEquals("BuildingCommunity", generatedKey); | ||
} | ||
|
||
@Test | ||
void andAuthorNames() throws ParseException { | ||
String bibtexString = "@ARTICLE{whatevery, author={Mari D. Herland and Mona-Iren Hauge and Ingeborg M. Helgeland}}"; | ||
|
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.
This is wrong! The entry MUST NOT be modified.
I think, you can just delete these two lines.