Skip to content

Commit

Permalink
Improved LaTeX to Unicode/HTML formatters to output more sensible val…
Browse files Browse the repository at this point in the history
…ues for unknown commands (#1622)
  • Loading branch information
oscargus authored and simonharrer committed Jul 25, 2016
1 parent fc603b4 commit f1e471f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Fixed [#636](https://github.com/JabRef/jabref/issues/636): DOI in export filters
- Fixed [#1527](https://github.com/JabRef/jabref/issues/1527): 'Get BibTeX data from DOI' Removes Marking
- Fixed [#1592](https://github.com/JabRef/jabref/issues/1592): LibreOffice: wrong numbers in citation labels
- Fixed [#1321](https://github.com/JabRef/jabref/issues/1321): LaTeX commands in fields not displayed in the list of references

### Removed
- It is not longer possible to choose to convert HTML sub- and superscripts to equations
Expand Down
31 changes: 20 additions & 11 deletions src/main/java/net/sf/jabref/logic/layout/format/HTMLChars.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,20 @@ public String format(String inField) {
String command = currentCommand.toString();
i++;
c = field.charAt(i);
String combody;
String commandBody;
if (c == '{') {
String part = StringUtil.getPart(field, i, false);
i += part.length();
combody = part;
commandBody = part;
} else {
combody = field.substring(i, i + 1);
commandBody = field.substring(i, i + 1);
}
Object result = HTML_CHARS.get(command + combody);
String result = HTML_CHARS.get(command + commandBody);

if (result != null) {
sb.append((String) result);
if (result == null) {
sb.append(commandBody);
} else {
sb.append(result);
}

incommand = false;
Expand All @@ -105,15 +107,15 @@ public String format(String inField) {
// Are we already at the end of the string?
if ((i + 1) == field.length()) {
String command = currentCommand.toString();
Object result = HTML_CHARS.get(command);
String result = HTML_CHARS.get(command);
/* If found, then use translated version. If not,
* then keep
* the text of the parameter intact.
*/
if (result == null) {
sb.append(command);
} else {
sb.append((String) result);
sb.append(result);
}

}
Expand Down Expand Up @@ -141,14 +143,21 @@ public String format(String inField) {
argument = part;
if (argument != null) {
// handle common case of general latex command
Object result = HTML_CHARS.get(command + argument);
String result = HTML_CHARS.get(command + argument);
// If found, then use translated version. If not, then keep
// the
// text of the parameter intact.

if (result == null) {
sb.append(argument);
if (argument.length() == 0) {
// Maybe a separator, such as in \LaTeX{}, so use command
sb.append(command);
} else {
// Otherwise, use argument
sb.append(argument);
}
} else {
sb.append((String) result);
sb.append(result);
}
}
} else if (c == '}') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,21 @@ public String format(String inField) {
} else {
commandBody = field.substring(i, i + 1);
}
Object result = LatexToUnicodeFormatter.CHARS.get(command + commandBody);
String result = LatexToUnicodeFormatter.CHARS.get(command + commandBody);

if (result == null) {
// Use combining accents if argument is single character or empty
if (commandBody.length() <= 1) {
String accent = LatexToUnicodeFormatter.ACCENTS.get(command);
if (accent != null) {
if (accent == null) {
// Shouldn't happen
sb.append(commandBody);
} else {
sb.append(commandBody).append(accent);
}
}
} else {
sb.append((String) result);
sb.append(result);
}

incommand = false;
Expand All @@ -127,15 +130,15 @@ public String format(String inField) {
// Are we already at the end of the string?
if ((i + 1) == field.length()) {
String command = currentCommand.toString();
Object result = LatexToUnicodeFormatter.CHARS.get(command);
String result = LatexToUnicodeFormatter.CHARS.get(command);
/* If found, then use translated version. If not,
* then keep
* the text of the parameter intact.
*/
if (result == null) {
sb.append(command);
} else {
sb.append((String) result);
sb.append(result);
}

}
Expand Down Expand Up @@ -168,10 +171,15 @@ public String format(String inField) {
// Use combining accents if argument is single character or empty
if (argument.length() <= 1) {
String accent = LatexToUnicodeFormatter.ACCENTS.get(command);
if (accent != null) {
sb.append(argument).append(accent);
if (accent == null) {
if (argument.length() == 0) {
// Empty argument, may be used as separator as in \LaTeX{}, so keep the command
sb.append(command);
} else {
sb.append(argument);
}
} else {
sb.append(argument);
sb.append(argument).append(accent);
}
} else {
sb.append(argument);
Expand Down
33 changes: 25 additions & 8 deletions src/test/java/net/sf/jabref/logic/layout/format/HTMLCharsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
import net.sf.jabref.logic.layout.LayoutFormatter;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class HTMLCharsTest {

private LayoutFormatter layout;

@Before
public void setUp() {
layout = new HTMLChars();
}

@Test
public void testBasicFormat() {

LayoutFormatter layout = new HTMLChars();

Assert.assertEquals("", layout.format(""));

Assert.assertEquals("hallo", layout.format("hallo"));
Expand Down Expand Up @@ -39,9 +47,6 @@ public void testBasicFormat() {

@Test
public void testLaTeXHighlighting() {

LayoutFormatter layout = new HTMLChars();

Assert.assertEquals("<em>hallo</em>", layout.format("\\emph{hallo}"));
Assert.assertEquals("<em>hallo</em>", layout.format("{\\emph hallo}"));
Assert.assertEquals("<em>hallo</em>", layout.format("{\\em hallo}"));
Expand All @@ -65,8 +70,6 @@ public void testLaTeXHighlighting() {

@Test
public void testEquations() {
LayoutFormatter layout = new HTMLChars();

Assert.assertEquals("&dollar;", layout.format("\\$"));
Assert.assertEquals("&sigma;", layout.format("$\\sigma$"));
Assert.assertEquals("A 32&nbsp;mA &Sigma;&Delta;-modulator",
Expand All @@ -75,11 +78,25 @@ public void testEquations() {

@Test
public void testNewLine() {
LayoutFormatter layout = new HTMLChars();
Assert.assertEquals("a<br>b", layout.format("a\nb"));
Assert.assertEquals("a<p>b", layout.format("a\n\nb"));
}
/*
* Is missing a lot of test cases for the individual chars...
*/

@Test
public void unknownCommandIsKept() {
assertEquals("aaaa", layout.format("\\aaaa"));
}

@Test
public void unknownCommandKeepsArgument() {
assertEquals("bbbb", layout.format("\\aaaa{bbbb}"));
}

@Test
public void unknownCommandWithEmptyArgumentIsKept() {
assertEquals("aaaa", layout.format("\\aaaa{}"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,19 @@ public void testCombiningAccentsCase1() {
public void testCombiningAccentsCase2() {
assertEquals("a͍", formatter.format("\\spreadlips{a}"));
}

@Test
public void unknownCommandIsKept() {
assertEquals("aaaa", formatter.format("\\aaaa"));
}

@Test
public void unknownCommandKeepsArgument() {
assertEquals("bbbb", formatter.format("\\aaaa{bbbb}"));
}

@Test
public void unknownCommandWithEmptyArgumentIsKept() {
assertEquals("aaaa", formatter.format("\\aaaa{}"));
}
}

0 comments on commit f1e471f

Please sign in to comment.