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

Minor improvements to TexParser #5212

Merged
merged 1 commit into from
Aug 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public void init(BibEntry entry) {

currentEntry = entry;
Optional<String> citeKey = entry.getCiteKeyOptional();

if (citeKey.isPresent()) {
startSearch(citeKey.get());
} else {
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/org/jabref/logic/texparser/DefaultTexParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.nio.channels.ClosedChannelException;
import java.nio.file.Files;
Expand All @@ -27,8 +28,8 @@ public class DefaultTexParser implements TexParser {
private static final String TEX_EXT = ".tex";

/**
* It is allowed to add new cite commands for pattern matching.
* Some valid examples: "citep", "[cC]ite", and "[cC]ite(author|title|year|t|p)?".
* It is allowed to add new cite commands for pattern matching. Some valid examples: "citep", "[cC]ite", and
* "[cC]ite(author|title|year|t|p)?".
*/
private static final String[] CITE_COMMANDS = {
"[cC]ite(alt|alp|author|authorfull|date|num|p|t|text|title|url|year|yearpar)?",
Expand Down Expand Up @@ -76,7 +77,9 @@ public TexParserResult parse(List<Path> texFiles) {
continue;
}

try (LineNumberReader lineNumberReader = new LineNumberReader(new CharsetDetector().setText(Files.readAllBytes(file)).detect().getReader())) {
try (
Reader reader = new CharsetDetector().setText(Files.readAllBytes(file)).detect().getReader();
LineNumberReader lineNumberReader = new LineNumberReader(reader)) {
for (String line = lineNumberReader.readLine(); line != null; line = lineNumberReader.readLine()) {
// Skip comments and blank lines.
if (line.trim().isEmpty() || line.trim().charAt(0) == '%') {
Expand All @@ -86,15 +89,19 @@ public TexParserResult parse(List<Path> texFiles) {
matchNestedFile(file, texFiles, referencedFiles, line);
}
} catch (ClosedChannelException e) {
LOGGER.error("Parsing has been interrupted");
return null;
// User changed the underlying LaTeX file
// We ignore this error and just continue with parsing
LOGGER.info("Parsing has been interrupted");
} catch (IOException | UncheckedIOException e) {
LOGGER.error("Error while parsing file {}", file, e);
// Some weired error during reading
// We ignore this error and just continue with parsing
LOGGER.info("Error while parsing file {}", file, e);
}
}

// Parse all files referenced by TEX files, recursively.
if (!referencedFiles.isEmpty()) {
// modifies class variable texParserResult
parse(referencedFiles);
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/model/texparser/TexParserResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class TexParserResult {

private final List<Path> fileList;
private final List<Path> nestedFiles;

// BibTeXKey --> set of citations
private final Multimap<String, Citation> citations;

public TexParserResult() {
Expand Down
47 changes: 43 additions & 4 deletions src/test/java/org/jabref/logic/texparser/DefaultTexParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,46 @@ public void testTwoCitationsSameLine() {
}

@Test
public void testFileEncoding() throws URISyntaxException {
public void testFileEncodingUtf8() throws URISyntaxException {
Path texFile = Paths.get(DefaultTexParserTest.class.getResource("utf-8.tex").toURI());

TexParserResult parserResult = new DefaultTexParser().parse(texFile);
TexParserResult expectedParserResult = new TexParserResult();

expectedParserResult.getFileList().add(texFile);
expectedParserResult.addKey("anykey", texFile, 1, 32, 45, "Danach wir anschließend mittels \\cite{anykey}.");

assertEquals(expectedParserResult, parserResult);
}

@Test
public void testFileEncodingIso88591() throws URISyntaxException {
Path texFile = Paths.get(DefaultTexParserTest.class.getResource("iso-8859-1.tex").toURI());

TexParserResult parserResult = new DefaultTexParser().parse(texFile);
TexParserResult expectedParserResult = new TexParserResult();

expectedParserResult.getFileList().add(texFile);
expectedParserResult.addKey("anykey", texFile, 1, 32, 45, "Danach wir anschließend mittels \\cite{anykey}.");

assertEquals(expectedParserResult, parserResult);
}

@Test
public void testFileEncodingIso885915() throws URISyntaxException {
Path texFile = Paths.get(DefaultTexParserTest.class.getResource("iso-8859-15.tex").toURI());

TexParserResult parserResult = new DefaultTexParser().parse(texFile);
TexParserResult expectedParserResult = new TexParserResult();

expectedParserResult.getFileList().add(texFile);
expectedParserResult.addKey("anykey", texFile, 1, 32, 45, "Danach wir anschließend mittels \\cite{anykey}.");

assertEquals(expectedParserResult, parserResult);
}

@Test
public void testFileEncodingForThreeFiles() throws URISyntaxException {
Path texFile = Paths.get(DefaultTexParserTest.class.getResource("utf-8.tex").toURI());
Path texFile2 = Paths.get(DefaultTexParserTest.class.getResource("iso-8859-1.tex").toURI());
Path texFile3 = Paths.get(DefaultTexParserTest.class.getResource("iso-8859-15.tex").toURI());
Expand All @@ -79,9 +118,9 @@ public void testFileEncoding() throws URISyntaxException {
TexParserResult expectedParserResult = new TexParserResult();

expectedParserResult.getFileList().addAll(Arrays.asList(texFile, texFile2, texFile3));
expectedParserResult.addKey("anschließend", texFile, 1, 11, 30, "Danach wir \\cite{anschließend} mittels.");
expectedParserResult.addKey("Lässt", texFile2, 1, 4, 16, "Man \\cite{Lässt} auf verweisen.");
expectedParserResult.addKey("Läste", texFile3, 1, 13, 25, "Man einfache \\cite{Läste}.");
expectedParserResult.addKey("anykey", texFile, 1, 32, 45, "Danach wir anschließend mittels \\cite{anykey}.");
expectedParserResult.addKey("anykey", texFile2, 1, 32, 45, "Danach wir anschließend mittels \\cite{anykey}.");
expectedParserResult.addKey("anykey", texFile3, 1, 32, 45, "Danach wir anschließend mittels \\cite{anykey}.");

assertEquals(expectedParserResult, parserResult);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Man \cite{L�sst} auf verweisen.
Danach wir anschlie�end mittels \cite{anykey}.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Man einfache \cite{L�ste}.
Danach wir anschlie�end mittels \cite{anykey}.
2 changes: 1 addition & 1 deletion src/test/resources/org/jabref/logic/texparser/utf-8.tex
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Danach wir \cite{anschließend} mittels.
Danach wir anschließend mittels \cite{anykey}.