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

Wip zmarkdown #317

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ build/
doc/build/
doc/rst/.presentation.rst

#Sonar database backup
sonar-database

# Misc
*.log
venv/
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ dependencies {
compile group: 'com.openhtmltopdf', name: 'openhtmltopdf-pdfbox', version: '0.0.1-RC11'
compile group: 'com.openhtmltopdf', name: 'openhtmltopdf-jsoup-dom-converter', version: '0.0.1-RC11'
compile group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.8.0'
compile group: 'com.vladsch.flexmark', name: 'flexmark-all', version: '0.26.4'
}

test {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/zestedesavoir/zestwriter/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Tab;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
Expand All @@ -45,7 +42,6 @@
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;

public class MainApp extends Application{
private static Configuration config;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/zestedesavoir/zestwriter/model/Content.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.zestedesavoir.zestwriter.MainApp;
import com.zestedesavoir.zestwriter.model.markdown.ZMarkdown;
import com.zestedesavoir.zestwriter.utils.ZdsHttp;
import com.zestedesavoir.zestwriter.view.MdTextController;
import com.zestedesavoir.zestwriter.view.com.FunctionTreeFactory;
import com.zestedesavoir.zestwriter.view.com.IconFactory;
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView;
Expand Down Expand Up @@ -159,11 +159,11 @@ public static String normalizeHtml(String htmlValue) {
return htmlValue;
}

public void saveToHtml(File file, MdTextController index) {
public void saveToHtml(File file) {
try (FileOutputStream fos = new FileOutputStream(file)) {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));
String mdValue = exportContentToMarkdown(0, getDepth());
String htmlValue = StringEscapeUtils.unescapeHtml4(index.markdownToHtml(mdValue));
String htmlValue = StringEscapeUtils.unescapeHtml4(ZMarkdown.markdownToHtml(mdValue));
htmlValue = normalizeHtml(htmlValue);
writer.append(MainApp.getMdUtils().addHeaderAndFooterStrict(htmlValue, getTitle()));
writer.flush();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.zestedesavoir.zestwriter.model;

import com.zestedesavoir.zestwriter.utils.Lang;
import com.zestedesavoir.zestwriter.view.dialogs.EditContentDialog;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.zestedesavoir.zestwriter.model.markdown;

import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.ext.anchorlink.AnchorLinkExtension;
import com.vladsch.flexmark.ext.aside.AsideExtension;
import com.vladsch.flexmark.ext.autolink.AutolinkExtension;
import com.vladsch.flexmark.ext.definition.DefinitionExtension;
import com.vladsch.flexmark.ext.emoji.EmojiExtension;
import com.vladsch.flexmark.ext.footnotes.FootnoteExtension;
import com.vladsch.flexmark.ext.gfm.issues.GfmIssuesExtension;
import com.vladsch.flexmark.ext.tables.TablesExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.options.MutableDataSet;
import com.vladsch.flexmark.util.sequence.BasedSequence;

import java.util.Arrays;

/**
* Created by: WinXaito (Kevin Vuilleumier)
*/
public class ZMarkdown{
private static Parser parser;
private static HtmlRenderer renderer;
private static boolean init = false;

private static void init(){
init = true;

MutableDataSet options = new MutableDataSet()
.set(Parser.EXTENSIONS, Arrays.asList(
TablesExtension.create(),
AnchorLinkExtension.create(),
AsideExtension.create(),
AutolinkExtension.create(),
DefinitionExtension.create(),
EmojiExtension.create(),
FootnoteExtension.create(),
GfmIssuesExtension.create()
));


parser = Parser.builder(options).build();
renderer = HtmlRenderer.builder(options).build();
}


public static String markdownToHtml(String markdown){
if(!init)
init();

// You can re-use parser and renderer instances
System.out.println("ZMarkdown: render");
Node document = parser.parse("ZMarkdown" + markdown);
return renderer.render(document);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zestedesavoir.zestwriter.MainApp;
import com.zestedesavoir.zestwriter.view.dialogs.EditContentDialog;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.http.client.fluent.Request;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.zestedesavoir.zestwriter.utils;

import com.zestedesavoir.zestwriter.view.MdTextController;
import com.zestedesavoir.zestwriter.view.MenuController;
import com.zestedesavoir.zestwriter.model.markdown.ZMarkdown;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringEscapeUtils;
import org.jsoup.Jsoup;
Expand Down Expand Up @@ -211,8 +210,8 @@ public String checkHtmlContentToText(String htmlContent, String source) {
return bf.toString();
}

public int countMistakes(MdTextController mdTextController, String markdown) {
String htmlText = StringEscapeUtils.unescapeHtml4(MenuController.markdownToHtml(mdTextController, markdown));
public int countMistakes(String markdown) {
String htmlText = StringEscapeUtils.unescapeHtml4(ZMarkdown.markdownToHtml(markdown));
AnnotatedText markup = makeAnnotatedText(htmlText);

langTool.getAllActiveRules().stream()
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/zestedesavoir/zestwriter/utils/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Arrays;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Arrays;
import java.util.Calendar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.zestedesavoir.zestwriter.MainApp;
import com.zestedesavoir.zestwriter.model.ContentNode;
import com.zestedesavoir.zestwriter.model.Textual;
import com.zestedesavoir.zestwriter.model.markdown.ZMarkdown;
import com.zestedesavoir.zestwriter.utils.Configuration;
import com.zestedesavoir.zestwriter.utils.Corrector;
import com.zestedesavoir.zestwriter.utils.readability.Readability;
Expand Down Expand Up @@ -152,7 +153,7 @@ protected Task<String> createTask() {
return new Task<String>() {
@Override
protected String call() throws Exception {
String html = getMdBox().markdownToHtml(sourceText.getText());
String html = ZMarkdown.markdownToHtml(sourceText.getText());
if (html != null) {
return MainApp.getMdUtils().addHeaderAndFooter(html);
} else {
Expand All @@ -163,9 +164,7 @@ protected String call() throws Exception {
}
};

renderTask.setOnFailed(t -> {
renderTask.restart();
});
renderTask.setOnFailed(t -> renderTask.restart());

renderTask.setOnSucceeded(t -> {
Platform.runLater(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.zestedesavoir.zestwriter.model.Content;
import com.zestedesavoir.zestwriter.model.ContentNode;
import com.zestedesavoir.zestwriter.model.Textual;
import com.zestedesavoir.zestwriter.model.markdown.ZMarkdown;
import com.zestedesavoir.zestwriter.utils.Configuration;
import com.zestedesavoir.zestwriter.utils.Corrector;
import com.zestedesavoir.zestwriter.utils.FlipTable;
Expand Down Expand Up @@ -41,8 +42,6 @@
import org.apache.commons.lang3.StringEscapeUtils;
import org.fxmisc.richtext.StyleClassedTextArea;
import org.fxmisc.wellbehaved.event.Nodes;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
import org.w3c.dom.DOMException;

import java.io.File;
Expand All @@ -67,13 +66,7 @@ public class MdTextController {
public AnchorPane treePane;
public BooleanPropertyBase currentSaved;
@Getter
@Setter
private boolean pythonStarted = false;
@Getter
private MainApp mainApp;
@Getter
@Setter
private PythonInterpreter pyconsole;
@FXML
private VBox contentBox;
@Getter
Expand Down Expand Up @@ -105,9 +98,6 @@ public class MdTextController {

@FXML
private void initialize() {
if (MainApp.getConfig().isEditorRenderView())
loadConsolePython();

loadFonts();
editorList.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> mainApp.getMenuController().setIsOnReadingTab(!(newValue.getContent() instanceof SplitPane))
Expand Down Expand Up @@ -143,18 +133,6 @@ public BooleanPropertyBase currentSavedProperty() {
return currentSaved;
}

public void loadConsolePython() {
new Thread(() -> {
pyconsole = new PythonInterpreter();
pyconsole.exec("from markdown import Markdown");
pyconsole.exec("from markdown.extensions.zds import ZdsExtension");
pyconsole.exec("from smileys_definition import smileys");
pyconsole.exec("mk_instance = Markdown(extensions=(ZdsExtension(inline=False, emoticons=smileys, js_support=False, ping_url=None),), inline=False)");
log.info("PYTHON STARTED");
setPythonStarted(true);
}).start();
}

/**
* Load fonts Merriweather and FiraMono for views
*/
Expand Down Expand Up @@ -308,17 +286,6 @@ public TreeItem<ContentNode> selectItemOnTree(TreeItem<ContentNode> item, Textua
return null;
}

public String markdownToHtml(String chaine) {
if (pyconsole != null) {
pyconsole.set("text", chaine);
pyconsole.exec("render = mk_instance.convert(text)");
PyString render = pyconsole.get("render", PyString.class);
return render.toString();
} else {
return null;
}
}

public void createTabExtract(Textual extract) throws IOException {
log.debug("Tentative de création d'un nouvel onglet pour " + extract.getTitle());
extract.loadMarkdown();
Expand Down Expand Up @@ -845,7 +812,7 @@ private void handleFullScreeenButtonAction(ActionEvent event) {

@FXML
private void handleValidateButtonAction(ActionEvent event) {
String s = StringEscapeUtils.unescapeHtml4(markdownToHtml(currentSourceText.getText()));
String s = StringEscapeUtils.unescapeHtml4(ZMarkdown.markdownToHtml(currentSourceText.getText()));
if (MdConvertController.corrector == null) {
MdConvertController.corrector = new Corrector();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zestedesavoir.zestwriter.MainApp;
import com.zestedesavoir.zestwriter.model.*;
import com.zestedesavoir.zestwriter.model.markdown.ZMarkdown;
import com.zestedesavoir.zestwriter.utils.Configuration;
import com.zestedesavoir.zestwriter.utils.Corrector;
import com.zestedesavoir.zestwriter.utils.ZdsHttp;
import com.zestedesavoir.zestwriter.utils.readability.Readability;
import com.zestedesavoir.zestwriter.view.com.*;
import com.zestedesavoir.zestwriter.view.dialogs.*;
import com.zestedesavoir.zestwriter.view.task.*;
import edu.berkeley.nlp.lm.io.IOUtils;
import javafx.beans.property.BooleanPropertyBase;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
Expand Down Expand Up @@ -46,13 +46,9 @@
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.fxmisc.richtext.StyleClassedTextArea;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -80,14 +76,6 @@ public class MenuController{
@FXML private MenuItem menuFindReplace;
private BooleanPropertyBase isOnReadingTab = new SimpleBooleanProperty(true);

public static String markdownToHtml(MdTextController index, String chaine) {
PythonInterpreter console = index.getPyconsole();
console.set("text", chaine);
console.exec("render = mk_instance.convert(text)");
PyString render = console.get("render", PyString.class);
return render.toString();
}

public BooleanPropertyBase isOnReadingTabProperty() {
return isOnReadingTab;
}
Expand Down Expand Up @@ -135,7 +123,7 @@ private void displayIndex(Map<String, Double> resultIndex, String title, String

@FXML private void handleFleshButtonAction(ActionEvent event){
Function<Textual, Double> calFlesh = (Textual ch) -> {
String htmlText = StringEscapeUtils.unescapeHtml4(markdownToHtml(mainApp.getIndex(), ch.readMarkdown()));
String htmlText = StringEscapeUtils.unescapeHtml4(ZMarkdown.markdownToHtml(ch.readMarkdown()));
String plainText = Corrector.htmlToTextWithoutCode(htmlText);
if("".equals(plainText.trim())){
return 100.0;
Expand All @@ -159,7 +147,7 @@ private void displayIndex(Map<String, Double> resultIndex, String title, String

@FXML private void handleGunningButtonAction(ActionEvent event){
Function<Textual, Double> calGuning = (Textual ch) -> {
String htmlText = StringEscapeUtils.unescapeHtml4(markdownToHtml(mainApp.getIndex(), ch.readMarkdown()));
String htmlText = StringEscapeUtils.unescapeHtml4(ZMarkdown.markdownToHtml(ch.readMarkdown()));
String plainText = Corrector.htmlToTextWithoutCode(htmlText);
if("".equals(plainText.trim())){
return 100.0;
Expand Down Expand Up @@ -632,7 +620,7 @@ private void uploadContents(){
hBottomBox.getChildren().clear();
hBottomBox.add(pb, 0, 0);
hBottomBox.add(labelField, 1, 0);
ExportPdfService exportPdfTask = new ExportPdfService(mainApp.getIndex(), content, selectedFile);
ExportPdfService exportPdfTask = new ExportPdfService(content, selectedFile);
labelField.textProperty().bind(exportPdfTask.messageProperty());
pb.progressProperty().bind(exportPdfTask.progressProperty());
Alert alert = new CustomAlert(AlertType.NONE);
Expand Down
Loading