Refactoring

pull/214/head
M66B 10 months ago
parent 11f404a3a2
commit d1eb3f6a27

@ -160,8 +160,6 @@ import org.bouncycastle.operator.RuntimeOperatorException;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder; import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder;
import org.bouncycastle.util.Store; import org.bouncycastle.util.Store;
import org.commonmark.node.Node;
import org.commonmark.renderer.html.HtmlRenderer;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
@ -6718,16 +6716,8 @@ public class FragmentCompose extends FragmentBase {
boolean dirty = false; boolean dirty = false;
String body; String body;
if (markdown ^ extras.getBoolean("markdown")) { if (markdown ^ extras.getBoolean("markdown")) {
// Markdown to HTML
String text = spanned.toString().replace('\u00a0', ' '); String text = spanned.toString().replace('\u00a0', ' ');
String html = Markdown.toHtml(text);
Markwon markwon = Markwon.builder(context)
.usePlugin(HtmlPlugin.create())
.build();
Node document = markwon.parse(text);
HtmlRenderer renderer = HtmlRenderer.builder().build();
String html = renderer.render(document);
Document doc = JsoupEx.parse(html); Document doc = JsoupEx.parse(html);
doc.body().attr("markdown", Boolean.toString(markdown)); doc.body().attr("markdown", Boolean.toString(markdown));
@ -7699,8 +7689,8 @@ public class FragmentCompose extends FragmentBase {
Spanned spannedBody; Spanned spannedBody;
if (markdown) { if (markdown) {
// TODO: HTML to Markdown String md = Markdown.fromHtml(doc);
spannedBody = new SpannableStringBuilder(doc.html()); spannedBody = new SpannableStringBuilder(md);
} else { } else {
HtmlHelper.clearAnnotations(doc); // Legacy left-overs HtmlHelper.clearAnnotations(doc); // Legacy left-overs

@ -0,0 +1,39 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2024 by Marcel Bokhorst (M66B)
*/
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.jsoup.nodes.Document;
public class Markdown {
static String toHtml(String markdown) {
Parser p = Parser.builder().build();
Node d = p.parse(markdown);
HtmlRenderer r = HtmlRenderer.builder().build();
return r.render(d);
}
static String fromHtml(Document d) {
// TODO: HTML to Markdown
throw new IllegalArgumentException("Not implemented");
}
}

@ -69,8 +69,6 @@ import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.bouncycastle.asn1.edec.EdECObjectIdentifiers; import org.bouncycastle.asn1.edec.EdECObjectIdentifiers;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier; import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node; import org.jsoup.nodes.Node;
@ -3995,13 +3993,10 @@ public class MessageHelper {
} }
} else if (h.isMarkdown()) { } else if (h.isMarkdown()) {
try { try {
Parser p = Parser.builder().build(); result = Markdown.toHtml(result);
org.commonmark.node.Node d = p.parse(result);
HtmlRenderer r = HtmlRenderer.builder().build();
result = r.render(d);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
result = HtmlHelper.formatPlainText(Log.formatThrowable(ex)); result = HtmlHelper.formatPlainText(result);
} }
} else if (h.isPatch()) { } else if (h.isPatch()) {
result = "<hr>" + result = "<hr>" +

Loading…
Cancel
Save