From 7de6d597d119a2fd0455fa8b838cfda12e646e27 Mon Sep 17 00:00:00 2001 From: Oliver Neill <7759984+odneill@users.noreply.github.com> Date: Tue, 16 Jul 2024 11:13:14 +0100 Subject: [PATCH] Add minimal support for biblatex data annotations (#11506) * Add minimal support for biblatex data annotations Fixes #11505 * update changelog --- CHANGELOG.md | 1 + .../org/jabref/logic/layout/LayoutHelper.java | 17 +++++++++++++++-- .../org/jabref/logic/layout/LayoutTest.java | 13 +++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6a2d0baf60..972ca5c0142 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv ### Added - We added support for selecting and using CSL Styles in JabRef's OpenOffice/LibreOffice integration for inserting bibliographic and in-text citations into a document. [#2146](https://github.com/JabRef/jabref/issues/2146), [#8893](https://github.com/JabRef/jabref/issues/8893) +- Added minimal support for [biblatex data annotation](https://mirrors.ctan.org/macros/latex/contrib/biblatex/doc/biblatex.pdf#subsection.3.7) fields in .layout files. [#11505](https://github.com/JabRef/jabref/issues/11505) ### Changed diff --git a/src/main/java/org/jabref/logic/layout/LayoutHelper.java b/src/main/java/org/jabref/logic/layout/LayoutHelper.java index 0a01c49dac5..1a923f17b64 100644 --- a/src/main/java/org/jabref/logic/layout/LayoutHelper.java +++ b/src/main/java/org/jabref/logic/layout/LayoutHelper.java @@ -48,7 +48,7 @@ public LayoutHelper(Reader in, List fileDirForDatabase, LayoutFormatterPreferences preferences, JournalAbbreviationRepository abbreviationRepository) { - this.in = new PushbackReader(Objects.requireNonNull(in)); + this.in = new PushbackReader(Objects.requireNonNull(in), 2); this.preferences = Objects.requireNonNull(preferences); this.abbreviationRepository = abbreviationRepository; this.fileDirForDatabase = fileDirForDatabase; @@ -262,7 +262,7 @@ private void parseField() throws IOException { endOfFile = true; } - if (!Character.isLetter((char) c) && (c != '_')) { + if (!validChar(c) && !validAnnotation(c)) { unread(c); name = buffer == null ? "" : buffer.toString(); @@ -345,6 +345,19 @@ private void parseField() throws IOException { } } + private boolean validAnnotation(int c) throws IOException { + // Only accept annotations that are followed by a valid character + boolean annotation = ((c == '+') || (c == ':')) && validChar(peek()); + + return annotation; + } + + private boolean validChar(int c) throws IOException { + boolean character = Character.isLetter((char) c) || (c == '_'); + + return character; + } + private int peek() throws IOException { int c = read(); unread(c); diff --git a/src/test/java/org/jabref/logic/layout/LayoutTest.java b/src/test/java/org/jabref/logic/layout/LayoutTest.java index 0e9134e4f52..e27e75380b2 100644 --- a/src/test/java/org/jabref/logic/layout/LayoutTest.java +++ b/src/test/java/org/jabref/logic/layout/LayoutTest.java @@ -12,6 +12,7 @@ import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.LinkedFile; import org.jabref.model.entry.field.StandardField; +import org.jabref.model.entry.field.UnknownField; import org.jabref.model.entry.types.StandardEntryType; import org.jabref.model.entry.types.UnknownEntryType; @@ -173,4 +174,16 @@ void customNameFormatter() throws IOException { assertEquals("JoeDoe and MaryJ", layoutText); } + + @Test + void annotatedField() throws IOException { + UnknownField annotatedField = new UnknownField("author+an"); + BibEntry entry = new BibEntry(StandardEntryType.Article) + .withField(annotatedField, "1:corresponding,2:highlight") + .withField(StandardField.AUTHOR, "Joe Doe and Mary Jane"); + + String layoutText = layout("\\author: \\author \\author+an", entry); + + assertEquals("Joe Doe and Mary Jane: Joe Doe and Mary Jane 1:corresponding,2:highlight", layoutText); + } }