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

Allow negation (!, NOT operator) in \\begin{...} conditionals #5138

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We added a context menu to the bib(la)tex-source-editor to copy'n'paste. [#5007](https://github.com/JabRef/jabref/pull/5007)
- We added a bibliographic references search, for finding references in several LaTeX files. This tool scans directories and shows which entries are used, how many times and where.
- We added an option in preferences to allow for integers in field "edition" when running database in bibtex mode. [#4680](https://github.com/JabRef/jabref/issues/4680)
- We added the ability to use negation in export filter layouts. [#5138](https://github.com/JabRef/jabref/pull/5138)


### Fixed
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/org/jabref/logic/layout/LayoutEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,17 @@ private String handleOptionField(BibEntry bibtex, BibDatabase database) {

private String handleFieldOrGroupStart(BibEntry bibtex, BibDatabase database) {
Optional<String> field;
boolean negated = false;
if (type == LayoutHelper.IS_GROUP_START) {
field = bibtex.getResolvedFieldOrAlias(FieldFactory.parseField(text), database);
} else if (text.matches(".*(;|(\\&+)).*")) {
// split the strings along &, && or ; for AND formatter
String[] parts = text.split("\\s*(;|(\\&+))\\s*");
field = Optional.empty();
for (String part : parts) {
field = bibtex.getResolvedFieldOrAlias(FieldFactory.parseField(part), database);
if (!field.isPresent()) {
negated = part.startsWith("!");
field = bibtex.getResolvedFieldOrAlias(FieldFactory.parseField(negated ? part.substring(1).trim() : part), database);
if (!(field.isPresent() ^ negated)) {
break;
}
}
Expand All @@ -268,14 +270,15 @@ private String handleFieldOrGroupStart(BibEntry bibtex, BibDatabase database) {
String[] parts = text.split("\\s*(\\|+)\\s*");
field = Optional.empty();
for (String part : parts) {
field = bibtex.getResolvedFieldOrAlias(FieldFactory.parseField(part), database);
if (field.isPresent()) {
negated = part.startsWith("!");
field = bibtex.getResolvedFieldOrAlias(FieldFactory.parseField(negated ? part.substring(1).trim() : part), database);
if (field.isPresent() ^ negated) {
break;
}
}
}

if ((!field.isPresent()) || ((type == LayoutHelper.IS_GROUP_START)
if ((!(field.isPresent() ^ negated)) || ((type == LayoutHelper.IS_GROUP_START)
&& field.get().equalsIgnoreCase(LayoutHelper.getCurrentGroup()))) {
return null;
} else {
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/org/jabref/logic/layout/LayoutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,35 @@ void HTMLCharsWithDotlessIAndTiled() throws IOException {
layoutText);
}

@Test
public void beginConditionals() throws IOException {
BibEntry entry = new BibEntry(StandardEntryType.Misc)
.withField(StandardField.AUTHOR, "Author");

// || (OR)
String layoutText = layout("\\begin{editor||author}\\format[HTMLChars]{\\author}\\end{editor||author}", entry);

assertEquals("Author", layoutText);

// && (AND)
layoutText = layout("\\begin{editor&&author}\\format[HTMLChars]{\\author}\\end{editor&&author}", entry);

assertEquals("", layoutText);

// ! (NOT)
layoutText = layout("\\begin{!year}\\format[HTMLChars]{(no year)}\\end{!year}", entry);

assertEquals("(no year)", layoutText);

// combined (!a&&b)
layoutText = layout(
"\\begin{!editor&&author}\\format[HTMLChars]{\\author}\\end{!editor&&author}" +
"\\begin{editor&&!author}\\format[HTMLChars]{\\editor} (eds.)\\end{editor&&!author}", entry);

assertEquals("Author", layoutText);

}

/**
* Test for http://discourse.jabref.org/t/the-wrapfilelinks-formatter/172 (the example in the help files)
*/
Expand Down