[evolution] Have a single function to convert markdown into HTML
- From: Milan Crha <mcrha src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [evolution] Have a single function to convert markdown into HTML
- Date: Fri, 28 Jan 2022 08:29:41 +0000 (UTC)
commit cb6aa4eb184348ae8580f6c0bd2ffd5ea3b26b2b
Author: Milan Crha <mcrha redhat com>
Date: Fri Jan 28 09:28:11 2022 +0100
Have a single function to convert markdown into HTML
To have a single place for any tweaks on the generated HTML.
src/e-util/e-markdown-editor.c | 43 +++++++++++++++++++++++---
src/e-util/e-markdown-editor.h | 3 ++
src/em-format/e-mail-formatter-text-markdown.c | 7 ++---
3 files changed, 45 insertions(+), 8 deletions(-)
---
diff --git a/src/e-util/e-markdown-editor.c b/src/e-util/e-markdown-editor.c
index 3325ea6395..dfefde101a 100644
--- a/src/e-util/e-markdown-editor.c
+++ b/src/e-util/e-markdown-editor.c
@@ -562,21 +562,56 @@ gchar *
e_markdown_editor_dup_html (EMarkdownEditor *self)
{
#ifdef HAVE_MARKDOWN
- GString *html;
- gchar *text, *converted;
+ gchar *text, *html;
#endif
g_return_val_if_fail (E_IS_MARKDOWN_EDITOR (self), NULL);
#ifdef HAVE_MARKDOWN
text = e_markdown_editor_dup_text (self);
- converted = cmark_markdown_to_html (text ? text : "", text ? strlen (text) : 0,
+ html = e_markdown_util_text_to_html (text, -1);
+
+ g_free (text);
+
+ return html;
+ #else
+ return NULL;
+ #endif
+}
+
+/**
+ * e_markdown_util_text_to_html:
+ * @plain_text: plain text with markdown to convert to HTML
+ * @length: length of the @plain_text, or -1 when it's nul-terminated
+ *
+ * Convert @plain_text, possibly with markdown, into the HTML.
+ *
+ * Note: The function can return %NULL when was not built
+ * with the markdown support.
+ *
+ * Returns: (transfer full) (nullable): text converted into HTML,
+ * or %NULL, when was not built with the markdown support.
+ * Free the string with g_free(), when no longer needed.
+ *
+ * Since: 3.44
+ **/
+gchar *
+e_markdown_util_text_to_html (const gchar *plain_text,
+ gssize length)
+{
+ #ifdef HAVE_MARKDOWN
+ GString *html;
+ gchar *converted;
+
+ if (length == -1)
+ length = plain_text ? strlen (plain_text) : 0;
+
+ converted = cmark_markdown_to_html (plain_text ? plain_text : "", length,
CMARK_OPT_VALIDATE_UTF8 | CMARK_OPT_UNSAFE);
html = e_str_replace_string (converted, "<blockquote>", "<blockquote type=\"cite\">");
g_free (converted);
- g_free (text);
return g_string_free (html, FALSE);
#else
diff --git a/src/e-util/e-markdown-editor.h b/src/e-util/e-markdown-editor.h
index bdc429c8b9..4369e7c25e 100644
--- a/src/e-util/e-markdown-editor.h
+++ b/src/e-util/e-markdown-editor.h
@@ -52,6 +52,9 @@ GtkWidget * e_markdown_editor_new (void);
gchar * e_markdown_editor_dup_text (EMarkdownEditor *self);
gchar * e_markdown_editor_dup_html (EMarkdownEditor *self);
+gchar * e_markdown_util_text_to_html (const gchar *plain_text,
+ gssize length);
+
G_END_DECLS
#endif /* E_MARKDOWN_EDITOR_H */
diff --git a/src/em-format/e-mail-formatter-text-markdown.c b/src/em-format/e-mail-formatter-text-markdown.c
index c73b90ecca..1d0fb906dc 100644
--- a/src/em-format/e-mail-formatter-text-markdown.c
+++ b/src/em-format/e-mail-formatter-text-markdown.c
@@ -15,7 +15,6 @@
#include "evolution-config.h"
-#include <cmark.h>
#include <glib/gi18n-lib.h>
#include <e-util/e-util.h>
@@ -66,9 +65,9 @@ emfe_text_markdown_format (EMailFormatterExtension *extension,
e_mail_formatter_format_text (formatter, part, output_stream, cancellable);
g_output_stream_flush (output_stream, cancellable, NULL);
- html = cmark_markdown_to_html ((const gchar *) g_memory_output_stream_get_data
(G_MEMORY_OUTPUT_STREAM (output_stream)),
- g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM
(output_stream)),
- CMARK_OPT_VALIDATE_UTF8 | CMARK_OPT_UNSAFE);
+ html = e_markdown_util_text_to_html ((const gchar *) g_memory_output_stream_get_data
(G_MEMORY_OUTPUT_STREAM (output_stream)),
+ g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (output_stream)));
+
g_object_unref (output_stream);
g_object_unref (mime_part);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]