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

Remove newlines in PDF annotations #3291

Merged
merged 15 commits into from
Nov 20, 2017
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 @@ -23,6 +23,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Crossreferenced entries are now used when a BibTex key is generated for an entry with empty fields. [#2811](https://github.com/JabRef/jabref/issues/2811)
- We now set the WM_CLASS of the UI to org-jabref-JabRefMain to allow certain Un*x window managers to properly identify its windows
- We changed the default paths for the OpenOffice/LibreOffice binaries to the default path for LibreOffice
- File annotation tab now removes newlines and hyphens before newlines from content and displays an empty String instead of N/A if no contents are found. [#3280](https://github.com/JabRef/jabref/issues/3280)
- We moved the groups field from the "Other fields" tab to "General" (you may have to reset your editor preferences under Options > Set up general fields)
- We no longer create a new entry editor when selecting a new entry to increase performance. [#3187](https://github.com/JabRef/jabref/pull/3187)
- We added the possibility to copy linked files from entries to a single output folder [#2539](https://github.com/JabRef/jabref/pull/2593)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import org.jabref.logic.formatter.bibtexfields.RemoveHyphenatedNewlinesFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveNewlinesFormatter;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.pdf.FileAnnotation;
import org.jabref.model.pdf.FileAnnotationType;
Expand Down Expand Up @@ -31,7 +33,11 @@ private void setupContentProperties(FileAnnotation annotation) {
String illegibleTextMessage = Localization.lang("The marked area does not contain any legible text!");
this.marking.set(annotationContent.isEmpty() ? illegibleTextMessage : annotationContent);
} else {
this.content.set(annotation.getContent());
String content = annotation.getContent();
// remove newlines && hyphens before linebreaks
content = new RemoveHyphenatedNewlinesFormatter().format(content);
content = new RemoveNewlinesFormatter().format(content);
this.content.set(content);
this.marking.set("");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.jabref.logic.formatter.bibtexfields;

import java.util.Objects;
import java.util.regex.Pattern;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.cleanup.Formatter;

/**
* Removes all hyphenated line breaks in the string.
*/
public class RemoveHyphenatedNewlinesFormatter implements Formatter {
private static final Pattern HYPHENATED_WORDS = Pattern.compile("(-\r\n|-\n|-\r)");

@Override
public String getName() {
return Localization.lang("Remove hyphenated line breaks");
}

@Override
public String getKey() {
return "remove_hyphenated_newlines";
}

@Override
public String format(String value) {
Objects.requireNonNull(value);

value = HYPHENATED_WORDS.matcher(value).replaceAll("");
return value.trim();
}

@Override
public String getDescription() {
return Localization.lang("Removes all hyphenated line breaks in the field content.");
}

@Override
public String getExampleInput() {
return "Gimme shel-\nter";
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.logic.formatter.bibtexfields;

import java.util.Objects;
import java.util.regex.Pattern;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.cleanup.Formatter;
Expand All @@ -9,6 +10,7 @@
* Removes all line breaks in the string.
*/
public class RemoveNewlinesFormatter implements Formatter {
private static final Pattern LINEBREAKS = Pattern.compile("(\r?\n|\r)");

@Override
public String getName() {
Expand All @@ -24,7 +26,8 @@ public String getKey() {
public String format(String value) {
Objects.requireNonNull(value);

return value.replace("\r\n", " ").replace("\n", " ").trim();
value = LINEBREAKS.matcher(value).replaceAll(" ");
return value.trim();
}

@Override
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/jabref/model/pdf/FileAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;

public class FileAnnotation {

private static final Log LOGGER = LogFactory.getLog(FileAnnotation.class);

private final static int ABBREVIATED_ANNOTATION_NAME_LENGTH = 45;
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_da.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=Entfernen_der_Zeilenumbrüche
Removes_all_line_breaks_in_the_field_content.=Entfernen_aller_Zeilenumbrüche_im_Inhalt_des_Feldes.
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=JabRef_kann_nicht_mit_Java_9_verwendet_werden.
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=Die_verwendete_Java_Installation_(%0)_wird_nicht_unterstützt._Bitte_installieren_Sie_Version_%1_oder_neuer.
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_el.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=Remove_line_breaks
Removes_all_line_breaks_in_the_field_content.=Removes_all_line_breaks_in_the_field_content.
Checking_integrity...=Checking_integrity...

Remove_hyphenated_line_breaks=Remove_hyphenated_line_breaks
Removes_all_hyphenated_line_breaks_in_the_field_content.=Removes_all_hyphenated_line_breaks_in_the_field_content.
Note_that_currently,_JabRef_does_not_run_with_Java_9.=Note_that_currently,_JabRef_does_not_run_with_Java_9.
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_fa.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=Supprimer_les_sauts_de_ligne
Removes_all_line_breaks_in_the_field_content.=Supprime_tous_les_sauts_de_ligne_du_contenu_d'un_champ
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_in.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_it.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_nl.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_no.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_pt_BR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_ru.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_sv.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_tr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_vi.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_zh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2351,5 +2351,7 @@ Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Remove_hyphenated_line_breaks=
Removes_all_hyphenated_line_breaks_in_the_field_content.=
Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jabref.logic.formatter.bibtexfields;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class RemoveHyphenatedNewlinesFormatterTest {
private RemoveHyphenatedNewlinesFormatter formatter;

@Before
public void setUp() {
formatter = new RemoveHyphenatedNewlinesFormatter();
}

@Test
public void removeHyphensBeforeNewlines() {
assertEquals("water", formatter.format("wa-\nter"));
assertEquals("water", formatter.format("wa-\r\nter"));
assertEquals("water", formatter.format("wa-\rter"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.jabref.logic.formatter.bibtexfields;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class RemoveNewlinesFormatterTest {
private RemoveNewlinesFormatter formatter;

@Before
public void setUp() {
formatter = new RemoveNewlinesFormatter();
}

@Test
public void removeCarriageReturnLineFeed() {
assertEquals("rn linebreak", formatter.format("rn\r\nlinebreak"));
}

@Test
public void removeCarriageReturn() {
assertEquals("r linebreak", formatter.format("r\rlinebreak"));
}

@Test
public void removeLineFeed() {
assertEquals("n linebreak", formatter.format("n\nlinebreak"));
}
}