Skip to content

Commit

Permalink
Merge pull request #10376 from JabRef/add-latex-integrity-check
Browse files Browse the repository at this point in the history
Co-authored-by: Zylence <kirsch.julian@yahoo.com>
  • Loading branch information
koppor and Zylence authored Sep 13, 2023
2 parents a1e2283 + 96b5a83 commit 183375e
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We added the possibility to find (and add) papers that cite or are cited by a given paper. [#6187](https://github.com/JabRef/jabref/issues/6187)
- We added an error-specific message for when a download from a URL fails. [#9826](https://github.com/JabRef/jabref/issues/9826)
- We added support for customizating the citation command (e.g., `[@key1,@key2]`) when [pushing to external applications](https://docs.jabref.org/cite/pushtoapplications). [#10133](https://github.com/JabRef/jabref/issues/10133)
- We added an integrity check for more special characters. [#8712](https://github.com/JabRef/jabref/issues/8712)
- We added protected terms described as "Computer science". [#10222](https://github.com/JabRef/jabref/pull/10222)

### Changed
Expand Down
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ dependencies {
exclude module: 'fastparse_2.13'
}

implementation "de.rototor.snuggletex:snuggletex:1.3.0"
implementation ("de.rototor.snuggletex:snuggletex-jeuclid:1.3.0") {
exclude group: "org.apache.xmlgraphics"
}

implementation group: 'com.microsoft.azure', name: 'applicationinsights-core', version: '2.4.1'
implementation (group: 'com.microsoft.azure', name: 'applicationinsights-logging-log4j2', version: '2.4.1') {
exclude module: "log4j-core"
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
requires jbibtex;
requires citeproc.java;

requires snuggletex.core;

requires org.apache.pdfbox;
requires org.apache.xmpbox;
requires com.ibm.icu;
Expand Down Expand Up @@ -141,5 +143,4 @@
requires org.antlr.antlr4.runtime;
requires org.libreoffice.uno;
requires de.saxsys.mvvmfx.validation;

}
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/logic/integrity/IntegrityCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public IntegrityCheck(BibDatabaseContext bibDatabaseContext,
new EntryLinkChecker(bibDatabaseContext.getDatabase()),
new CitationKeyDeviationChecker(bibDatabaseContext, citationKeyPatternPreferences),
new CitationKeyDuplicationChecker(bibDatabaseContext.getDatabase()),
new AmpersandChecker()
new AmpersandChecker(),
new LatexIntegrityChecker()
));
if (bibDatabaseContext.isBiblatexMode()) {
entryCheckers.addAll(List.of(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.jabref.logic.integrity;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.ac.ed.ph.snuggletex.ErrorCode;
import uk.ac.ed.ph.snuggletex.InputError;
import uk.ac.ed.ph.snuggletex.SnuggleEngine;
import uk.ac.ed.ph.snuggletex.SnuggleInput;
import uk.ac.ed.ph.snuggletex.SnuggleSession;
import uk.ac.ed.ph.snuggletex.definitions.CoreErrorCode;
import uk.ac.ed.ph.snuggletex.definitions.CoreErrorGroup;

public class LatexIntegrityChecker implements EntryChecker {

private static final Logger LOGGER = LoggerFactory.getLogger(SnuggleSession.class);
private static final SnuggleEngine ENGINE = new SnuggleEngine();
private static final SnuggleSession SESSION = ENGINE.createSession();
private static final ResourceBundle ERROR_MESSAGES = ENGINE.getPackages().get(0).getErrorMessageBundle();
private static final Set<ErrorCode> EXCLUDED_ERRORS = new HashSet<>();

static {
// Static configuration.
SESSION.getConfiguration().setFailingFast(true);

// '#' only allowed inside and command/environment definitions.
EXCLUDED_ERRORS.add(CoreErrorCode.TTEG04);
}

@Override
public List<IntegrityMessage> check(BibEntry entry) {
List<IntegrityMessage> results = new ArrayList<>();

for (Map.Entry<Field, String> field : entry.getFieldMap().entrySet()) {
SnuggleInput input = new SnuggleInput(field.getValue());
try {
SESSION.parseInput(input);
} catch (IOException e) {
LOGGER.error("Error at parsing", e);
}
if (!SESSION.getErrors().isEmpty()) {
// Retrieve the first error only because it is likely to be more meaningful.
// Displaying all (subsequent) faults may lead to confusion.
// We further get a slight performance benefit from failing fast (see static config in class header).
InputError error = SESSION.getErrors().get(0);
ErrorCode errorCode = error.getErrorCode();
// Exclude all DOM building errors as this functionality is not used.
// Further, exclude individual errors.
if (!errorCode.getErrorGroup().equals(CoreErrorGroup.TDE) && !EXCLUDED_ERRORS.contains(errorCode)) {
String jabrefMessageWrapper = errorMessageFormatHelper(errorCode, error.getArguments());
results.add(new IntegrityMessage(jabrefMessageWrapper, entry, field.getKey()));
}
}
SESSION.reset();
}
return results;
}

public static String errorMessageFormatHelper(ErrorCode snuggleTexErrorCode, Object... arguments) {
String snuggletexMessagePattern = LatexIntegrityChecker.ERROR_MESSAGES.getString(snuggleTexErrorCode.getName());
String snuggletexErrorMessage = MessageFormat.format(snuggletexMessagePattern, arguments);
return Localization.lang("LaTeX Parsing Error: %0", snuggletexErrorMessage);
}
}
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,7 @@ changes\ all\ letters\ to\ lower\ case.=changes all letters to lower case.
CHANGES\ ALL\ LETTERS\ TO\ UPPER\ CASE.=CHANGES ALL LETTERS TO UPPER CASE.
Changes\ The\ First\ Letter\ Of\ All\ Words\ To\ Capital\ Case\ And\ The\ Remaining\ Letters\ To\ Lower\ Case.=Changes The First Letter Of All Words To Capital Case And The Remaining Letters To Lower Case.
Cleans\ up\ LaTeX\ code.=Cleans up LaTeX code.
LaTeX\ Parsing\ Error\:\ %0=LaTeX Parsing Error: %0
Converts\ HTML\ code\ to\ LaTeX\ code.=Converts HTML code to LaTeX code.
HTML\ to\ Unicode=HTML to Unicode
Converts\ HTML\ code\ to\ Unicode.=Converts HTML code to Unicode.
Expand Down
Loading

0 comments on commit 183375e

Please sign in to comment.