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 5 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
73 changes: 73 additions & 0 deletions src/main/java/org/jabref/logic/texparser/CrossingKeys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.jabref.logic.texparser;

import java.util.Optional;
import java.util.Set;

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

class CrossingKeys {

private final CrossingKeysResult result;

public CrossingKeys(TexParserResult texParserResult, BibDatabase masterDatabase) {
this.result = new CrossingKeysResult(texParserResult, masterDatabase);
}

/**
* Look for an equivalent BibTeX entry within the reference database for all keys inside of the TEX files.
*/
public CrossingKeysResult resolveKeys() {
davidemdot marked this conversation as resolved.
Show resolved Hide resolved
Set<String> keySet = result.getParserResult().getCitations().keySet();

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

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

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

return result;
}

/**
* Find cross references for inserting into the new database.
*/
private void resolveCrossReferences(BibEntry entry) {
entry.getField(FieldName.CROSSREF).ifPresent(crossRef -> {
if (!result.getNewDatabase().getEntryByKey(crossRef).isPresent()) {
Optional<BibEntry> refEntry = result.getMasterDatabase().getEntryByKey(crossRef);

if (refEntry.isPresent()) {
insertEntry(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(BibEntry entry) {
BibEntry clonedEntry = (BibEntry) entry.clone();
result.getNewDatabase().insertEntry(clonedEntry);
}
}
141 changes: 141 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,141 @@
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.regex.Matcher;
import java.util.regex.Pattern;

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.
*
* <p>Some valid examples: "citep", "[cC]ite", "[cC]ite(author|title|year|t|p)?"
*
* <p>TODO: Add support for multicite commands.
*/
private static final String[] CITE_COMMANDS = new String[] {
"[cC]ite(alt|alp|author|authorfull|date|num|p|t|text|title|url|year|yearpar)?",
"([aA]|fnote|foot|footfull|full|no|[nN]ote|[pP]aren|[pP]note|[tT]ext|[sS]mart|super)cite",
"footcitetext"
};
private static final String CITE_REGEX = String.format("\\\\(?:%s)\\*?(?:\\[(?:[^\\]]*)\\]){0,2}\\{(?<key>[^\\}]*)\\}",
String.join("|", CITE_COMMANDS));

private static final String INCLUDE_REGEX = "\\\\(?:include|input)\\{(?<file>[^\\}]*)\\}";

private final TexParserResult result;

public DefaultTexParser() {
this.result = new TexParserResult();
}

@Override
public TexParserResult parse(String citeString) {
matchCitation(Paths.get("foo/bar"), 1, citeString);
return result;
}

@Override
public TexParserResult parse(Path texFile) {
return parse(Collections.singletonList(texFile));
}

@Override
public TexParserResult parse(List<Path> texFiles) {
List<Path> referencedFiles = new ArrayList<>();

if (result.getFileList().isEmpty()) {
result.getFileList().addAll(texFiles);
} else {
result.getNestedFiles().addAll(texFiles);
}

for (int fileIndex = 0; fileIndex < texFiles.size(); fileIndex++) {
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()) {
if (line.startsWith("%")) {
// Skip comment lines.
continue;
}

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

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

return result;
}

/**
* Find cites along a specific line and add them to a map.
*/
private void matchCitation(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("key").split(",");

for (String key : keys) {
Citation citation = new Citation(file, lineNumber, citeMatch.start(), citeMatch.end(), line);

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

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

/**
* Find inputs and includes along a specific line and store them for parsing later.
*/
private void matchNestedFile(Path file, List<Path> texFiles, List<Path> referencedFiles, String line) {
Matcher includeMatch = Pattern.compile(INCLUDE_REGEX).matcher(line);

while (includeMatch.find()) {
String include = includeMatch.group("file");

if (!include.endsWith(".tex")) {
include += ".tex";
davidemdot marked this conversation as resolved.
Show resolved Hide resolved
}

Path folder = file.getParent();
Path inputFile = (folder != null)
? folder.resolve(include)
: Paths.get(include);

if (!texFiles.contains(inputFile)) {
referencedFiles.add(inputFile);
}
}
}
}
92 changes: 92 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,92 @@
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;
}

/**
* Get a fixed-width string that shows the context of a citation.
*
* @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 (%d:%d-%d) \"%s\"", path, 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 path.equals(citation.path)
&& line == citation.line
&& colStart == citation.colStart
&& colEnd == citation.colEnd
&& lineText.equals(citation.lineText);
}

@Override
public int hashCode() {
return Objects.hash(path, line, colStart, colEnd, lineText);
}
}
Loading