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

[WIP] LaTeX Integration project #5011

Merged
merged 8 commits into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
181 changes: 181 additions & 0 deletions src/main/java/org/jabref/logic/texparser/DefaultTexParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package org.jabref.logic.texparser;

import java.io.IOException;
import java.io.LineNumberReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.FieldName;
import org.jabref.model.texparser.Citation;
import org.jabref.model.texparser.TexParser;
import org.jabref.model.texparser.TexParserResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultTexParser implements TexParser {

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTexParser.class);

/**
* It is allowed to add new cite commands for pattern matching.
*/
private static final String[] CITE_COMMANDS = new String[] {"cite", "citep", "citet"};
davidemdot marked this conversation as resolved.
Show resolved Hide resolved
private static final String CITE_REGEX = String.format("\\\\(?:%s)\\{([^\\}]*)\\}",
String.join("|", CITE_COMMANDS));

private static final String[] INCLUDE_COMMANDS = new String[] {"include", "includeonly", "input"};
private static final String INCLUDE_REGEX = String.format("\\\\(?:%s)\\{([^\\}]*)\\}",
String.join("|", INCLUDE_COMMANDS));

private final BibDatabase masterDatabase;

public DefaultTexParser(BibDatabase database) {
masterDatabase = database;
}

@Override
public TexParserResult parse(Path texFile) {
return parseTexFiles(new ArrayList<>(Collections.singletonList(texFile)));
davidemdot marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public TexParserResult parse(List<Path> texFiles) {
return parseTexFiles(texFiles);
}

private TexParserResult parseTexFiles(List<Path> texFiles) {
davidemdot marked this conversation as resolved.
Show resolved Hide resolved
TexParserResult result = new TexParserResult(masterDatabase);
int fileIndex = 0;

while (fileIndex < texFiles.size()) {
davidemdot marked this conversation as resolved.
Show resolved Hide resolved
Path file = texFiles.get(fileIndex++);

try (LineNumberReader lnr = new LineNumberReader(Files.newBufferedReader(file))) {
for (String line = lnr.readLine(); line != null; line = lnr.readLine()) {
// Skip comment lines.
if (line.startsWith("%")) {
continue;
}

matchCitation(result, file, lnr.getLineNumber(), line);
matchNestedFile(result, file, texFiles, line);
}
} catch (IOException e) {
LOGGER.warn("Error opening the TEX file", e);
}
}

resolveTags(result);
return result;
}

/*
* Find cites along a specific line and store them.
*/
private void matchCitation(TexParserResult result, Path file, int lineNumber, String line) {
Matcher citeMatch = Pattern.compile(CITE_REGEX).matcher(line);
davidemdot marked this conversation as resolved.
Show resolved Hide resolved

while (citeMatch.find()) {
String[] keys = citeMatch.group(1).split(",");

for (String key : keys) {
addKey(result, key.trim(), new Citation(file, lineNumber, citeMatch.start(), citeMatch.end(), line));
}
}
}

private void matchNestedFile(TexParserResult result, Path file, List<Path> fileList, String line) {
Matcher includeMatch = Pattern.compile(INCLUDE_REGEX).matcher(line);

while (includeMatch.find()) {
String[] includes = includeMatch.group(1).split(",");

for (String include : includes) {
Path inputFile = (file.getParent() != null)
? file.getParent().resolve(include)
: Paths.get(include);

if (!fileList.contains(inputFile)) {
fileList.add(inputFile);
result.increaseNestedFilesCounter();
}
}
}
}

/*
* Add a citation to the uniqueKeys map.
*/
private void addKey(TexParserResult result, String key, Citation citation) {
Map<String, List<Citation>> uniqueKeys = result.getUniqueKeys();

if (!uniqueKeys.containsKey(key)) {
uniqueKeys.put(key, new ArrayList<>());
}

if (!uniqueKeys.get(key).contains(citation)) {
uniqueKeys.get(key).add(citation);
}
}

/*
* Look for an equivalent BibTeX entry within the reference database for all keys inside of the TEX files.
*/
private void resolveTags(TexParserResult result) {
davidemdot marked this conversation as resolved.
Show resolved Hide resolved
Set<String> keySet = result.getUniqueKeys().keySet();

for (String key : keySet) {
if (!result.getGeneratedBibDatabase().getEntryByKey(key).isPresent()) {
Optional<BibEntry> entry = masterDatabase.getEntryByKey(key);

if (entry.isPresent()) {
insertEntry(result, entry.get());
resolveCrossReferences(result, entry.get());
} else {
result.getUnresolvedKeys().add(key);
}
}
}

// Copy database definitions
if (result.getGeneratedBibDatabase().hasEntries()) {
result.getGeneratedBibDatabase().copyPreamble(masterDatabase);
result.insertStrings(masterDatabase.getUsedStrings(result.getGeneratedBibDatabase().getEntries()));
}
}

private void resolveCrossReferences(TexParserResult result, BibEntry entry) {
entry.getField(FieldName.CROSSREF).ifPresent(crossRef -> {
if (!result.getGeneratedBibDatabase().getEntryByKey(crossRef).isPresent()) {
Optional<BibEntry> refEntry = masterDatabase.getEntryByKey(crossRef);

if (refEntry.isPresent()) {
insertEntry(result, refEntry.get());
result.increaseCrossRefEntriesCounter();
} else {
result.getUnresolvedKeys().add(crossRef);
}
}
});
}

/*
* Insert into the database a clone of the given entry. The cloned entry has a new unique ID.
*/
private void insertEntry(TexParserResult result, BibEntry entry) {
BibEntry clonedEntry = (BibEntry) entry.clone();
result.getGeneratedBibDatabase().insertEntry(clonedEntry);
}
}
90 changes: 90 additions & 0 deletions src/main/java/org/jabref/model/texparser/Citation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.jabref.model.texparser;

import java.nio.file.Path;
import java.util.Objects;

public class Citation {

/**
* The total number of characters that are shown around a cite (cite width included).
*/
private static final int CONTEXT_WIDTH = 50;

private final Path path;
private final int line;
private final int colStart;
private final int colEnd;
private final String lineText;

public Citation(Path path, int line, int colStart, int colEnd, String lineText) {
this.path = path;
this.line = line;
this.colStart = colStart;
this.colEnd = colEnd;
this.lineText = lineText;
}

public Path getPath() {
return path;
}

public int getLine() {
return line;
}

public int getColStart() {
return colStart;
}

public int getColEnd() {
return colEnd;
}

public String getLineText() {
return lineText;
}

/**
* @return String that contains a cite and the text that surrounds it along the same line.
*/
public String getContext() {
int center = (colStart + colEnd) / 2;
int lineLength = lineText.length();

int start = Math.max(0, (center + CONTEXT_WIDTH / 2 < lineLength)
? center - CONTEXT_WIDTH / 2
: lineLength - CONTEXT_WIDTH);
int end = Math.min(lineLength, start + CONTEXT_WIDTH);

return lineText.substring(start, end);
}

@Override
public String toString() {
return String.format("%s\t%d:%d-%d\t%s", path.toString(), line, colStart, colEnd, getContext());
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

Citation citation = (Citation) o;

return line == citation.line
&& colStart == citation.colStart
&& colEnd == citation.colEnd
&& Objects.equals(path, citation.path)
&& Objects.equals(lineText, citation.lineText);
}

@Override
public int hashCode() {
return Objects.hash(path, line, colStart, colEnd, lineText);
}
}
21 changes: 21 additions & 0 deletions src/main/java/org/jabref/model/texparser/TexParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jabref.model.texparser;

import java.nio.file.Path;
import java.util.List;

public interface TexParser {

/**
* @param texFile Path to a TEX file
* @return a TexParserResult, which contains the generated BibDatabase and all data related to the bibliographic
* entries
*/
TexParserResult parse(Path texFile);

/**
* @param texFiles List of Path objects linked to a TEX file
* @return a list of TexParserResult objects, which contains the generated BibDatabase and all data related to the
* bibliographic entries
*/
TexParserResult parse(List<Path> texFiles);
}
84 changes: 84 additions & 0 deletions src/main/java/org/jabref/model/texparser/TexParserResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.jabref.model.texparser;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibtexString;

public class TexParserResult {

private final BibDatabase masterDatabase;
private final Map<String, List<Citation>> uniqueKeys = new HashMap<>();
private final List<String> unresolvedKeys = new ArrayList<>();
private final BibDatabase texDatabase = new BibDatabase();
private int insertedStrings = 0;
private int nestedFilesCount = 0;
private int crossRefEntriesCount = 0;

public TexParserResult(BibDatabase masterDatabase) {
this.masterDatabase = masterDatabase;
}

public BibDatabase getMasterDatabase() {
return masterDatabase;
}

public Map<String, List<Citation>> getUniqueKeys() {
return uniqueKeys;
}

public int getFoundKeysInTex() {
return uniqueKeys.size();
}

public int getCitationsCountByKey(String key) {
return uniqueKeys.get(key).size();
}

public List<String> getUnresolvedKeys() {
return unresolvedKeys;
}

public int getUnresolvedKeysCount() {
return unresolvedKeys.size();
}

public BibDatabase getGeneratedBibDatabase() {
return texDatabase;
}

public int getResolvedKeysCount() {
return texDatabase.getEntryCount() - crossRefEntriesCount;
}

public int getInsertedStrings() {
return insertedStrings;
}

public void insertStrings(Collection<BibtexString> usedStrings) {
for (BibtexString string : usedStrings) {
texDatabase.addString(string);
insertedStrings++;
}
}

public int getNestedFilesCount() {
return nestedFilesCount;
}

public void increaseNestedFilesCounter() {
nestedFilesCount++;
}

public int getCrossRefEntriesCount() {
return crossRefEntriesCount;
}

public void increaseCrossRefEntriesCounter() {
crossRefEntriesCount++;
}
}
Loading