Refactoring

This commit is contained in:
M66B 2024-02-19 16:06:44 +01:00
parent 11f404a3a2
commit d1eb3f6a27
3 changed files with 44 additions and 20 deletions

View File

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

View File

@ -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");
}
}

View File

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