Skip to content

Commit

Permalink
Add HTML2MD conversion to abstract and comment fields (#10896)
Browse files Browse the repository at this point in the history
* Add HTML2MD conversion to abstract and comment fields

Co-authored-by: Carl Christian Snethlage <[email protected]>
Co-authored-by: Amanda Anderson <[email protected]>
Co-authored-by: Paisley Lewis <[email protected]>
Co-authored-by: Luca Bolzonello <[email protected]>
Co-authored-by: Ashwin2397 <[email protected]>

* Add ClipBoardManager bridge

---------

Co-authored-by: Carl Christian Snethlage <[email protected]>
Co-authored-by: Amanda Anderson <[email protected]>
Co-authored-by: Paisley Lewis <[email protected]>
Co-authored-by: Luca Bolzonello <[email protected]>
Co-authored-by: Ashwin2397 <[email protected]>
Co-authored-by: Christoph <[email protected]>
  • Loading branch information
7 people authored Feb 22, 2024
1 parent 9e5e5a1 commit f570a2d
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We added a new group icon column to the main table showing the icons of the entry's groups. [#10801](https://github.com/JabRef/jabref/pull/10801)
- We added ability to jump to an entry in the command line using `-j CITATIONKEY`. [koppor#540](https://github.com/koppor/jabref/issues/540)
- We added a new boolean to the style files for Openoffice/Libreoffice integration to switch between ZERO_WIDTH_SPACE (default) and no space. [#10843](https://github.com/JabRef/jabref/pull/10843)
- When pasting HTML into the abstract or a comment field, the hypertext is automatically converted to Markdown. [#10558](https://github.com/JabRef/jabref/issues/10558)
- We added the possibility to redownload files that had been present but are no longer in the specified location. [#10848](https://github.com/JabRef/jabref/issues/10848)
- We added the citation key pattern `[camelN]`. Equivalent to the first N words of the `[camel]` pattern.

Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ dependencies {
}

implementation 'com.vladsch.flexmark:flexmark:0.64.8'
implementation 'com.vladsch.flexmark:flexmark-html2md-converter:0.64.8'

implementation group: 'net.harawata', name: 'appdirs', version: '1.2.2'

Expand Down
8 changes: 8 additions & 0 deletions external-libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ URL: https://github.com/vsch/flexmark-java
License: BSD-2-Clause
```
```yaml
Id: com.vladsch.flexmark:flexmark-html2md-converter
Project: flexmark-java
URL: https://github.com/vsch/flexmark-java
License: BSD-2-Clause
```
```yaml
Id: commons-beanutils:commons-beanutils
Project: Apache Commons Beanutils
Expand Down Expand Up @@ -708,6 +715,7 @@ com.squareup.okio:okio:1.15.0
com.squareup.retrofit2:retrofit:2.6.1
com.sun.istack:istack-commons-runtime:4.1.2
com.tobiasdiez:easybind:2.2.1-SNAPSHOT
com.vladsch.flexmark:flexmark-html2md-converter:0.64.8
com.vladsch.flexmark:flexmark-util-ast:0.64.8
com.vladsch.flexmark:flexmark-util-builder:0.64.8
com.vladsch.flexmark:flexmark-util-collection:0.64.8
Expand Down
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
requires com.ibm.icu;

requires flexmark;
requires flexmark.html2md.converter;
requires flexmark.util.ast;
requires flexmark.util.data;

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/jabref/gui/ClipBoardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ public static String getContents() {
return result;
}

public static String getHtmlContents() {
String result = clipboard.getHtml();
if (result == null) {
return "";
}
return result;
}

public static boolean hasHtml() {
return clipboard.hasHtml();
}

/**
* Get the String residing on the primary clipboard (if it exists).
*
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public static FieldEditorFX getForField(final Field field,
return new KeywordsEditor(field, suggestionProvider, fieldCheckers, preferences, undoManager);
} else if (field == InternalField.KEY_FIELD) {
return new CitationKeyEditor(field, suggestionProvider, fieldCheckers, databaseContext);
} else if (fieldProperties.contains(FieldProperty.MARKDOWN)) {
return new MarkdownEditor(field, suggestionProvider, fieldCheckers, preferences, undoManager);
} else {
// default
return new SimpleEditor(field, suggestionProvider, fieldCheckers, preferences, isMultiLine, undoManager);
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/MarkdownEditor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.jabref.gui.fieldeditors;

import javax.swing.undo.UndoManager;

import javafx.scene.control.TextInputControl;

import org.jabref.gui.ClipBoardManager;
import org.jabref.gui.autocompleter.SuggestionProvider;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.model.entry.field.Field;
import org.jabref.preferences.PreferencesService;

import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;

public class MarkdownEditor extends SimpleEditor {

private final FlexmarkHtmlConverter flexmarkHtmlConverter = FlexmarkHtmlConverter.builder().build();

public MarkdownEditor(Field field, SuggestionProvider<?> suggestionProvider, FieldCheckers fieldCheckers, PreferencesService preferences, UndoManager undoManager) {
super(field, suggestionProvider, fieldCheckers, preferences, true, undoManager);
}

@Override
protected TextInputControl createTextInputControl() {
return new EditorTextArea() {
@Override
public void paste() {
if (ClipBoardManager.hasHtml()) {
String htmlText = ClipBoardManager.getHtmlContents();
String mdText = flexmarkHtmlConverter.convert(htmlText);
super.replaceSelection(mdText);
} else {
super.paste();
}
}
};
}
}
8 changes: 7 additions & 1 deletion src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class SimpleEditor extends HBox implements FieldEditorFX {

private final SimpleEditorViewModel viewModel;
private final TextInputControl textInput;
private final boolean isMultiLine;

public SimpleEditor(final Field field,
final SuggestionProvider<?> suggestionProvider,
Expand All @@ -28,8 +29,9 @@ public SimpleEditor(final Field field,
final boolean isMultiLine,
final UndoManager undoManager) {
this.viewModel = new SimpleEditorViewModel(field, suggestionProvider, fieldCheckers, undoManager);
this.isMultiLine = isMultiLine;

textInput = isMultiLine ? new EditorTextArea() : new EditorTextField();
textInput = createTextInputControl();
HBox.setHgrow(textInput, Priority.ALWAYS);

textInput.textProperty().bindBidirectional(viewModel.textProperty());
Expand All @@ -55,6 +57,10 @@ public SimpleEditor(final Field field,
this(field, suggestionProvider, fieldCheckers, preferences, false, undoManager);
}

protected TextInputControl createTextInputControl() {
return isMultiLine ? new EditorTextArea() : new EditorTextField();
}

@Override
public void bindToEntry(BibEntry entry) {
viewModel.bindToEntry(entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum FieldProperty {
ISSN,
JOURNAL_NAME,
LANGUAGE,
MARKDOWN, // Field content is text, but should be interpreted as markdown
MONTH,
MULTIPLE_ENTRY_LINK,
MULTILINE_TEXT,
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/model/entry/field/StandardField.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
public enum StandardField implements Field {

ABSTRACT("abstract", FieldProperty.MULTILINE_TEXT),
ABSTRACT("abstract", FieldProperty.MULTILINE_TEXT, FieldProperty.MARKDOWN),
ADDENDUM("addendum"),
ADDRESS("address"),
AFTERWORD("afterword", FieldProperty.PERSON_NAMES),
Expand All @@ -32,7 +32,7 @@ public enum StandardField implements Field {
CHAPTER("chapter"),
COMMENTATOR("commentator", FieldProperty.PERSON_NAMES),
// Comments of users are handled at {@link org.jabref.model.entry.field.UserSpecificCommentField}
COMMENT("comment", FieldProperty.COMMENT, FieldProperty.MULTILINE_TEXT, FieldProperty.VERBATIM),
COMMENT("comment", FieldProperty.COMMENT, FieldProperty.MULTILINE_TEXT, FieldProperty.VERBATIM, FieldProperty.MARKDOWN),
CROSSREF("crossref", FieldProperty.SINGLE_ENTRY_LINK),
CITES("cites", FieldProperty.MULTIPLE_ENTRY_LINK),
DATE("date", FieldProperty.DATE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.Set;

public class UserSpecificCommentField implements Field {
private static final Set<FieldProperty> PROPERTIES = EnumSet.of(FieldProperty.COMMENT, FieldProperty.MULTILINE_TEXT, FieldProperty.VERBATIM);
private static final Set<FieldProperty> PROPERTIES = EnumSet.of(FieldProperty.COMMENT, FieldProperty.MULTILINE_TEXT, FieldProperty.VERBATIM, FieldProperty.MARKDOWN);
private final String name;

public UserSpecificCommentField(String username) {
Expand Down

0 comments on commit f570a2d

Please sign in to comment.