[gnome-builder] gui: properly render markdown for hover documentation
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] gui: properly render markdown for hover documentation
- Date: Sat, 31 Jul 2021 16:50:22 +0000 (UTC)
commit c8e91c344061dd00de39439719477300b104f39d
Author: Tom A. Wagner <tom a wagner protonmail com>
Date: Wed Jul 21 12:01:22 2021 +0200
gui: properly render markdown for hover documentation
This replaced the very incomplete gs_markdown parser with a new markdown parser/renderer
based on cmark, which supports all features commonly used in markdown source code documentation.
build-aux/flatpak/org.gnome.Builder.json | 18 ++
src/libide/gui/ide-marked-view.c | 304 ++++++++++++++++++++++++++++++-
src/libide/gui/meson.build | 1 +
src/libide/io/ide-marked-content.c | 23 ++-
src/libide/io/ide-marked-content.h | 3 +-
5 files changed, 332 insertions(+), 17 deletions(-)
---
diff --git a/build-aux/flatpak/org.gnome.Builder.json b/build-aux/flatpak/org.gnome.Builder.json
index e4965ca8e..0f2ef82c6 100644
--- a/build-aux/flatpak/org.gnome.Builder.json
+++ b/build-aux/flatpak/org.gnome.Builder.json
@@ -621,6 +621,24 @@
}
]
},
+ {
+ "name" : "cmark",
+ "buildsystem" : "cmake-ninja",
+ "builddir" : true,
+ "config-opts" : [
+ "-DCMARK_TESTS=OFF",
+ "-DCMARK_SHARED=OFF"
+ ],
+ "cleanup" : [
+ "/bin/cmark"
+ ],
+ "sources" : [
+ {
+ "type" : "git",
+ "url" : "https://github.com/commonmark/cmark"
+ }
+ ]
+ },
{
"name" : "gnome-builder",
"buildsystem" : "meson",
diff --git a/src/libide/gui/ide-marked-view.c b/src/libide/gui/ide-marked-view.c
index 7fb911670..f29977043 100644
--- a/src/libide/gui/ide-marked-view.c
+++ b/src/libide/gui/ide-marked-view.c
@@ -28,9 +28,299 @@
# include <webkit2/webkit2.h>
#endif
-#include "gs-markdown-private.h"
+#include <cmark.h>
+
#include "ide-marked-view.h"
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (cmark_node, cmark_node_free);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (cmark_iter, cmark_iter_free);
+
+// Keeps track of a markdown list we are currently rendering in.
+struct list_context {
+ cmark_list_type list_type;
+ guint next_elem_number;
+};
+
+/**
+ * node_is_leaf:
+ * @node: (transfer none): The markdown node that will be checked
+ *
+ * Check whether the provided markdown node is a leaf node
+ */
+static gboolean
+node_is_leaf(cmark_node *node)
+{
+ g_assert (node != NULL);
+
+ return cmark_node_first_child(node) == NULL;
+}
+
+/**
+ * render_node:
+ * @out: (transfer none): The #GString that the markdown is renderer into
+ * @list_stack: (transfer none): A stack used to track all lists currently rendered into, must
+ * be empty for the first #render_node call
+ * @node: (transfer none): The node that will be rendererd
+ * @ev_type: The event that occurred when iterating to the provided none.
+ * Either CMARK_EVENT_ENTER or CMARK_EVENT_EXIT
+ *
+ * Returns: FALSE if parsing failed somehow, otherwise TRUE
+ *
+ * Render a single markdown node
+ */
+static gboolean
+render_node(GString *out,
+ GQueue *list_stack,
+ cmark_node *node,
+ cmark_event_type ev_type)
+{
+ gboolean entering;
+
+ g_assert (out != NULL);
+ g_assert (list_stack != NULL);
+ g_assert (node != NULL);
+
+ entering = (ev_type == CMARK_EVENT_ENTER);
+
+ switch (cmark_node_get_type (node))
+ {
+ case CMARK_NODE_NONE:
+ return FALSE;
+
+ case CMARK_NODE_DOCUMENT:
+ break;
+
+ // Leaf nodes, these will never have an exit event.
+ case CMARK_NODE_THEMATIC_BREAK:
+ case CMARK_NODE_LINEBREAK:
+ g_string_append (out, "\n");
+ break;
+
+ case CMARK_NODE_SOFTBREAK:
+ g_string_append (out, " ");
+ break;
+
+ case CMARK_NODE_CODE_BLOCK:
+ case CMARK_NODE_CODE:
+ g_string_append (out, "<tt>");
+ g_string_append (out, cmark_node_get_literal (node));
+ g_string_append (out, "</tt>");
+ break;
+
+ case CMARK_NODE_TEXT:
+ g_string_append (out, cmark_node_get_literal (node));
+ break;
+
+ // Normal nodes, these have exit events if they are not leaf nodes
+ case CMARK_NODE_EMPH:
+ if (entering)
+ {
+ g_string_append (out, "<i>");
+ g_string_append (out, cmark_node_get_literal (node));
+ }
+ if (!entering || node_is_leaf (node))
+ {
+ g_string_append (out, "</i>");
+ }
+ break;
+
+ case CMARK_NODE_STRONG:
+ if (entering)
+ {
+ g_string_append (out, "<b>");
+ g_string_append (out, cmark_node_get_literal (node));
+ }
+ if (!entering || node_is_leaf (node))
+ {
+ g_string_append (out, "</b>");
+ }
+ break;
+
+ case CMARK_NODE_LINK:
+ if (entering)
+ {
+ g_string_append_printf (out,
+ "<a href=\"%s\">",
+ cmark_node_get_url (node)
+ );
+ g_string_append (out, cmark_node_get_title (node));
+ }
+ if (!entering || node_is_leaf (node))
+ g_string_append (out, "</a>");
+ break;
+
+ case CMARK_NODE_HEADING:
+ if (entering)
+ {
+ const gchar *level;
+
+ switch (cmark_node_get_heading_level (node))
+ {
+ case 1:
+ level = "xx-large";
+ break;
+
+ case 2:
+ level = "x-large";
+ break;
+
+ case 3:
+ level = "large";
+ break;
+
+ case 4:
+ level = "medium";
+ break;
+
+ case 5:
+ level = "small";
+ break;
+
+ case 6:
+ level = "x-small";
+ break;
+
+ default:
+ g_return_val_if_reached(FALSE);
+
+ }
+ g_string_append_printf (out, "<span size=\"%s\">", level);
+ }
+ if (!entering || node_is_leaf (node))
+ {
+ g_string_append (out, "</span>\n");
+ }
+ break;
+
+ case CMARK_NODE_PARAGRAPH:
+ if (!entering)
+ {
+ g_string_append (out, "\n");
+
+ // When not in a list, append another newline to create vertical space
+ // between paragraphs.
+ if (g_queue_is_empty (list_stack))
+ g_string_append (out, "\n");
+ }
+ break;
+
+ case CMARK_NODE_LIST:
+ if (entering)
+ {
+ g_autofree struct list_context *list = NULL;
+
+ list = g_malloc (sizeof (struct list_context));
+
+ list->list_type = cmark_node_get_list_type (node);
+
+ g_return_val_if_fail (list->list_type != CMARK_NO_LIST, FALSE);
+
+ list->next_elem_number = cmark_node_get_list_start (node);
+ g_queue_push_tail (list_stack, g_steal_pointer (&list));
+ }
+ else
+ {
+ g_free (g_queue_pop_tail (list_stack));
+
+ // If this was the outermost list, add a newline to create vertical spacing.
+ if (g_queue_is_empty (list_stack))
+ g_string_append (out, "\n");
+ }
+ break;
+
+ case CMARK_NODE_ITEM:
+ if (entering)
+ {
+ struct list_context *list;
+
+ list = g_queue_peek_tail (list_stack);
+
+ g_return_val_if_fail (list != NULL, FALSE);
+
+ // Indent sublists by four spaces per level
+ for (gint i = 0; i < g_queue_get_length (list_stack) - 1; i++)
+ g_string_append (out, " ");
+
+ if (list->list_type == CMARK_ORDERED_LIST)
+ {
+ g_string_append_printf (out, "%u. ", list->next_elem_number);
+ list->next_elem_number += 1;
+ }
+ else
+ {
+ g_string_append (out, "• ");
+ }
+ }
+ break;
+
+ // Not properly implemented (yet), falls back to default implementation
+ case CMARK_NODE_BLOCK_QUOTE:
+ case CMARK_NODE_HTML_BLOCK:
+ case CMARK_NODE_CUSTOM_BLOCK:
+ case CMARK_NODE_HTML_INLINE:
+ case CMARK_NODE_CUSTOM_INLINE:
+ case CMARK_NODE_IMAGE:
+ default:
+ if (entering)
+ {
+ const gchar* literal;
+ literal = cmark_node_get_literal (node);
+
+ if (literal != NULL)
+ g_string_append (out, literal);
+ }
+ break;
+ }
+
+ return TRUE;
+}
+
+/**
+ * parse_markdown:
+ * @markdown: (transfer none): The markdown that will be parsed to pango markup
+ * @len: The length of the markdown in bytes, or -1 if the size is not known
+ *
+ * Parse the provided document and returns it converted to pango markup for use in a GtkLabel.
+ * This will also render links as html <a> tags so GtkLabel can make them clickable.
+ *
+ * Returns: (transfer full) (nullable): The parsed document as pango markup, or %NULL on parsing errors
+ */
+static gchar *
+parse_markdown (const gchar *markdown,
+ gssize len)
+{
+ g_autoptr(GString) result = NULL;
+ g_autoqueue(GQueue) list_stack = NULL;
+ g_autoptr(cmark_node) root_node = NULL;
+ cmark_node *current_node;
+ g_autoptr(cmark_iter) iter = NULL;
+ cmark_event_type ev_type;
+
+ IDE_ENTRY;
+
+ g_assert (markdown != NULL);
+
+ result = g_string_new (NULL);
+ list_stack = g_queue_new();
+
+ if (len < 0)
+ len = strlen (markdown);
+
+ root_node = cmark_parse_document (markdown, len, 0);
+
+ iter = cmark_iter_new (root_node);
+
+ while ((ev_type = cmark_iter_next (iter)) != CMARK_EVENT_DONE)
+ {
+ g_return_val_if_fail (ev_type == CMARK_EVENT_ENTER || ev_type == CMARK_EVENT_EXIT, NULL);
+
+ current_node = cmark_iter_get_node (iter);
+ g_return_val_if_fail (render_node (result, list_stack, current_node, ev_type), NULL);
+ }
+
+ IDE_RETURN (g_string_free (g_steal_pointer (&result), FALSE));
+}
+
struct _IdeMarkedView
{
GtkBin parent_instance;
@@ -54,7 +344,8 @@ ide_marked_view_init (IdeMarkedView *self)
GtkWidget *
ide_marked_view_new (IdeMarkedContent *content)
{
- g_autofree gchar *markup = NULL;
+ const gchar* markup;
+ gsize markup_len;
GtkWidget *child = NULL;
IdeMarkedView *self;
IdeMarkedKind kind;
@@ -63,7 +354,7 @@ ide_marked_view_new (IdeMarkedContent *content)
self = g_object_new (IDE_TYPE_MARKED_VIEW, NULL);
kind = ide_marked_content_get_kind (content);
- markup = ide_marked_content_as_string (content);
+ markup = ide_marked_content_as_string (content, &markup_len);
switch (kind)
{
@@ -97,14 +388,11 @@ ide_marked_view_new (IdeMarkedContent *content)
case IDE_MARKED_KIND_MARKDOWN:
{
- g_autoptr(GsMarkdown) md = gs_markdown_new (GS_MARKDOWN_OUTPUT_PANGO);
g_autofree gchar *parsed = NULL;
- gs_markdown_set_smart_quoting (md, TRUE);
- gs_markdown_set_autocode (md, TRUE);
- gs_markdown_set_autolinkify (md, TRUE);
+ parsed = parse_markdown (markup, markup_len);
- if ((parsed = gs_markdown_parse (md, markup)))
+ if (parsed != NULL)
child = g_object_new (GTK_TYPE_LABEL,
"max-width-chars", 80,
"selectable", TRUE,
diff --git a/src/libide/gui/meson.build b/src/libide/gui/meson.build
index 94311282f..42d24555f 100644
--- a/src/libide/gui/meson.build
+++ b/src/libide/gui/meson.build
@@ -181,6 +181,7 @@ libide_gui_deps = [
libdazzle_dep,
libpeas_dep,
libwebkit_dep,
+ dependency('libcmark', version: '>= 0.29.0'),
libide_core_dep,
libide_io_dep,
diff --git a/src/libide/io/ide-marked-content.c b/src/libide/io/ide-marked-content.c
index 19f5ebf15..221e99440 100644
--- a/src/libide/io/ide-marked-content.c
+++ b/src/libide/io/ide-marked-content.c
@@ -209,15 +209,17 @@ ide_marked_content_get_bytes (IdeMarkedContent *self)
/**
* ide_marked_content_as_string:
* @self: a #IdeMarkedContent
+ * @len: (out) (optional): Location to store the length of the returned strings in bytes, or %NULL
*
- * Gets the contents of the marked content as a newly allcoated C string.
+ * Gets the contents of the marked content as a C string.
*
- * Returns: (nullable): a newly allocated string or %NULL
+ * Returns: (transfer none) (nullable): the content as a string or %NULL
*
* Since: 3.32
*/
-gchar *
-ide_marked_content_as_string (IdeMarkedContent *self)
+const gchar *
+ide_marked_content_as_string (IdeMarkedContent *self,
+ gsize *len)
{
g_return_val_if_fail (self != NULL, NULL);
g_return_val_if_fail (self->magic == IDE_MARKED_CONTENT_MAGIC, NULL);
@@ -225,11 +227,16 @@ ide_marked_content_as_string (IdeMarkedContent *self)
if (self->data != NULL)
{
- const gchar *buf;
- gsize len;
+ const gchar *result;
+ gsize length;
- if ((buf = g_bytes_get_data (self->data, &len)))
- return g_strndup (buf, len);
+ if ((result = g_bytes_get_data (self->data, &length)))
+ {
+ if (len != NULL)
+ *len = length;
+
+ return result;
+ }
}
return NULL;
diff --git a/src/libide/io/ide-marked-content.h b/src/libide/io/ide-marked-content.h
index 0a5ecdba9..479148af0 100644
--- a/src/libide/io/ide-marked-content.h
+++ b/src/libide/io/ide-marked-content.h
@@ -56,7 +56,8 @@ GBytes *ide_marked_content_get_bytes (IdeMarkedContent *self);
IDE_AVAILABLE_IN_3_32
IdeMarkedKind ide_marked_content_get_kind (IdeMarkedContent *self);
IDE_AVAILABLE_IN_3_32
-gchar *ide_marked_content_as_string (IdeMarkedContent *self);
+const gchar *ide_marked_content_as_string (IdeMarkedContent *self,
+ gsize *len);
IDE_AVAILABLE_IN_3_32
IdeMarkedContent *ide_marked_content_ref (IdeMarkedContent *self);
IDE_AVAILABLE_IN_3_32
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]