-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Display PDF annotations in entry preview #16074
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
Open
Gustavotakehara
wants to merge
16
commits into
JabRef:main
Choose a base branch
from
Gustavotakehara:issue4257_PdfAnotation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9eef5c1
Implement safe HTML rendering for PDF file annotations
Gustavotakehara 7fc8ee4
Integrate PDF annotations preview asynchronously into PreviewViewer
Gustavotakehara 9530cc1
fix catch in generatePreviewWithAnnotations
Gustavotakehara 86793aa
Add unit tests for FileAnnotationPreview
Gustavotakehara 84d2648
fix test
Gustavotakehara a4e16f9
changes
Gustavotakehara ca7c401
address code review feedback on preview throttling, localization and …
Gustavotakehara 0126e6a
Fix localization consistency by reusing existing keys and remove unus…
Gustavotakehara 7364ef5
Fix string formatting using String.formatted() and register missing l…
Gustavotakehara cca9e96
Fix string formatting using String.formatted()
Gustavotakehara 7d2494e
remove checkstyle blank line violation in PreviewViewer
Gustavotakehara dc49e45
fix: address static analysis, i18n, and concurrency review findings
Gustavotakehara 16ae8bc
fix: resolve file annotation preview test failures and static analysi…
Gustavotakehara 1ee3794
Merge branch 'main' into issue4257_PdfAnotation
Gustavotakehara a0147f9
Merge branch 'main' into issue4257_PdfAnotation
Gustavotakehara 6f4e2d2
Merge branch 'main' into issue4257_PdfAnotation
Gustavotakehara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
jablib/src/main/java/org/jabref/logic/pdf/FileAnnotationPreview.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package org.jabref.logic.pdf; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| import org.jabref.logic.l10n.Localization; | ||
| import org.jabref.logic.util.strings.StringUtil; | ||
| import org.jabref.model.pdf.FileAnnotation; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import static org.jabref.logic.util.strings.StringUtil.quoteForHTML; | ||
|
|
||
| public class FileAnnotationPreview { | ||
|
|
||
| public static String render(@Nullable Map<Path, List<FileAnnotation>> annotations) { | ||
| if (annotations == null || annotations.isEmpty()) { | ||
| return ""; | ||
| } | ||
|
|
||
| StringBuilder html = new StringBuilder(); | ||
| html.append("<br><br><b>") | ||
| .append(Localization.lang("PDF Annotations")) | ||
| .append("</b><br>"); | ||
|
|
||
| annotations.entrySet().stream() | ||
| .filter(entry -> entry.getKey() != null && entry.getKey().getFileName() != null) | ||
| .forEach(entry -> { | ||
| List<FileAnnotation> fileAnnotations = entry.getValue(); | ||
| if (fileAnnotations == null || fileAnnotations.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| String fileName = entry.getKey().getFileName().toString(); | ||
| html.append("<br><i>").append(quoteForHTML(fileName)).append("</i><br>"); | ||
|
|
||
| fileAnnotations.stream() | ||
| .filter(Objects::nonNull) | ||
| .filter(annotation -> StringUtil.isNotBlank(annotation.getContent())) | ||
| .sorted(Comparator.comparingInt(FileAnnotation::getPage)) | ||
| .forEach(annotation -> renderAnnotation(html, annotation)); | ||
| }); | ||
|
|
||
| return html.toString(); | ||
| } | ||
|
|
||
| private static void renderAnnotation(StringBuilder html, FileAnnotation annotation) { | ||
| String typeStr; | ||
|
|
||
| if (annotation.getAnnotationType() != null) { | ||
| String typeName = annotation.getAnnotationType().toString(); | ||
|
|
||
| if ("HIGHLIGHT".equalsIgnoreCase(typeName)) { | ||
| typeStr = Localization.lang("highlight"); | ||
| } else if ("UNDERLINE".equalsIgnoreCase(typeName)) { | ||
| typeStr = Localization.lang("underline"); | ||
| } else if ("STRIKEOUT".equalsIgnoreCase(typeName)) { | ||
| typeStr = Localization.lang("strikeout"); | ||
| } else { | ||
| typeStr = typeName.toLowerCase(); | ||
| } | ||
| } else { | ||
| typeStr = Localization.lang("unknown"); | ||
| } | ||
|
|
||
| int page = annotation.getPage(); | ||
| String content = annotation.getContent(); | ||
|
qodo-free-for-open-source-projects[bot] marked this conversation as resolved.
|
||
| String headerLabel = Localization.lang("%0 (page %1)", typeStr, String.valueOf(page)); | ||
|
|
||
| html.append("<b>") | ||
| .append(quoteForHTML(headerLabel)) | ||
| .append("</b> ") | ||
| .append(quoteForHTML(content)); | ||
|
|
||
| if (annotation.hasLinkedAnnotation() && annotation.getLinkedFileAnnotation() != null) { | ||
| String noteContent = annotation.getLinkedFileAnnotation().getContent(); | ||
|
|
||
| if (StringUtil.isNotBlank(noteContent)) { | ||
| String noteLabel = Localization.lang("Note"); | ||
| String formattedNote = " — " + noteLabel + ": " + noteContent; | ||
|
|
||
| html.append("<i>") | ||
| .append(quoteForHTML(formattedNote)) | ||
| .append("</i>"); | ||
| } | ||
| } | ||
|
|
||
| html.append("<br>"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
jablib/src/test/java/org/jabref/logic/pdf/FileAnnotationPreviewTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package org.jabref.logic.pdf; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.util.Arrays; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.jabref.logic.l10n.Language; | ||
| import org.jabref.logic.l10n.Localization; | ||
| import org.jabref.model.pdf.FileAnnotation; | ||
| import org.jabref.model.pdf.FileAnnotationType; | ||
|
|
||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.jabref.logic.util.strings.StringUtil.quoteForHTML; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| class FileAnnotationPreviewTest { | ||
|
|
||
| @BeforeAll | ||
| static void setUp() { | ||
| Localization.setLanguage(Language.ENGLISH); | ||
| } | ||
|
|
||
| private FileAnnotation createMockAnnotation(FileAnnotationType type, int page, String content, boolean hasLinked) { | ||
| FileAnnotation annotation = mock(FileAnnotation.class); | ||
| when(annotation.getAnnotationType()).thenReturn(type); | ||
| when(annotation.getPage()).thenReturn(page); | ||
| when(annotation.getContent()).thenReturn(content); | ||
| when(annotation.hasLinkedAnnotation()).thenReturn(hasLinked); | ||
| return annotation; | ||
| } | ||
|
|
||
| private String escape(String text) { | ||
| return quoteForHTML(text); | ||
| } | ||
|
|
||
| @Nested | ||
| class EdgeCasesAndFilteringTests { | ||
|
|
||
| @Test | ||
| void renderReturnsEmptyStringWhenMapIsEmpty() { | ||
|
qodo-free-for-open-source-projects[bot] marked this conversation as resolved.
|
||
| assertEquals("", FileAnnotationPreview.render(Map.of()), | ||
| "An empty map must produce an empty string output"); | ||
| } | ||
|
|
||
| @Test | ||
| void renderFiltersOutNullAnnotationsAndEmptyContent() { | ||
| Path path = Path.of("test.pdf"); | ||
| FileAnnotation blankAnnotation = createMockAnnotation(FileAnnotationType.TEXT, 1, "", false); | ||
|
|
||
| Map<Path, List<FileAnnotation>> annotations = Map.of(path, Arrays.asList((FileAnnotation) null, blankAnnotation)); | ||
|
|
||
| String expectedHtml = "<br><br><b>PDF Annotations</b><br><br><i>" + escape("test.pdf") + "</i><br>"; | ||
|
|
||
| assertEquals(expectedHtml, FileAnnotationPreview.render(annotations)); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| class HtmlContentFormattingAndOrderingTests { | ||
|
|
||
| @Test | ||
| void renderFormatsValidAnnotationsCorrectlyWithHtmlEscaping() { | ||
| Path path = Path.of("article.pdf"); | ||
| FileAnnotation annotation = createMockAnnotation(FileAnnotationType.HIGHLIGHT, 3, "This & That", false); | ||
| Map<Path, List<FileAnnotation>> annotations = Map.of(path, List.of(annotation)); | ||
|
|
||
| String expectedHeader = "highlight (page 3)"; | ||
| String expectedHtml = "<br><br><b>PDF Annotations</b><br><br><i>" + escape("article.pdf") + "</i><br>" | ||
| + "<b>" + escape(expectedHeader) + "</b> " + escape("This & That") + "<br>"; | ||
|
|
||
| assertEquals(expectedHtml, FileAnnotationPreview.render(annotations)); | ||
| } | ||
|
|
||
| @Test | ||
| void renderOrdersAnnotationsByPageNumberInAscendingOrder() { | ||
| Path path = Path.of("book.pdf"); | ||
| FileAnnotation page10 = createMockAnnotation(FileAnnotationType.TEXT, 10, "Late", false); | ||
| FileAnnotation page2 = createMockAnnotation(FileAnnotationType.TEXT, 2, "Early", false); | ||
| Map<Path, List<FileAnnotation>> annotations = new LinkedHashMap<>(); | ||
| annotations.put(path, List.of(page10, page2)); | ||
|
|
||
| String headerPage2 = "text (page 2)"; | ||
| String headerPage10 = "text (page 10)"; | ||
|
|
||
| String expectedHtml = "<br><br><b>PDF Annotations</b><br><br><i>" + escape("book.pdf") + "</i><br>" | ||
| + "<b>" + escape(headerPage2) + "</b> " + escape("Early") + "<br>" | ||
| + "<b>" + escape(headerPage10) + "</b> " + escape("Late") + "<br>"; | ||
|
|
||
| assertEquals(expectedHtml, FileAnnotationPreview.render(annotations)); | ||
| } | ||
|
|
||
| @Test | ||
| void renderAppendsSecondaryLinkedNoteCommentsWhenPresent() { | ||
| Path path = Path.of("document.pdf"); | ||
| FileAnnotation mainAnnotation = createMockAnnotation(FileAnnotationType.TEXT, 1, "Main", true); | ||
| FileAnnotation linkedAnnotation = createMockAnnotation(FileAnnotationType.TEXT, 1, "Comment", false); | ||
| when(mainAnnotation.getLinkedFileAnnotation()).thenReturn(linkedAnnotation); | ||
| Map<Path, List<FileAnnotation>> annotations = Map.of(path, List.of(mainAnnotation)); | ||
|
|
||
| String expectedHeader = "text (page 1)"; | ||
| String expectedNote = " — " + Localization.lang("Note") + ": Comment"; | ||
|
|
||
| String expectedHtml = "<br><br><b>PDF Annotations</b><br><br><i>" + escape("document.pdf") + "</i><br>" | ||
| + "<b>" + escape(expectedHeader) + "</b> " + escape("Main") + "<i>" + escape(expectedNote) + "</i><br>"; | ||
|
|
||
| assertEquals(expectedHtml, FileAnnotationPreview.render(annotations)); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.