[gnome-builder/document-manager] markdown: bring back markdown live preview
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/document-manager] markdown: bring back markdown live preview
- Date: Thu, 11 Dec 2014 00:03:29 +0000 (UTC)
commit f369bf644fa2c6b39ee8b466617727c23a66ec0a
Author: Christian Hergert <christian hergert me>
Date: Wed Dec 10 16:02:45 2014 -0800
markdown: bring back markdown live preview
src/editor/gb-editor-view.c | 22 +++-
src/gnome-builder.mk | 2 -
src/html/gb-html-document.c | 85 +++++++++++-
src/html/gb-html-document.h | 12 ++-
src/html/gb-html-view.c | 8 +-
src/markdown/gb-markdown-preview.c | 263 ------------------------------------
src/markdown/gb-markdown-preview.h | 60 --------
7 files changed, 116 insertions(+), 336 deletions(-)
---
diff --git a/src/editor/gb-editor-view.c b/src/editor/gb-editor-view.c
index 5d99031..9173596 100644
--- a/src/editor/gb-editor-view.c
+++ b/src/editor/gb-editor-view.c
@@ -151,13 +151,33 @@ gb_editor_view_create_preview (GbDocumentView *view)
{
GbEditorView *self = (GbEditorView *)view;
GbDocument *document;
+ GbHtmlDocumentTransform transform = NULL;
+ GtkSourceBuffer *buffer;
+ GtkSourceLanguage *language;
g_return_val_if_fail (GB_IS_EDITOR_VIEW (self), NULL);
+ buffer = GTK_SOURCE_BUFFER (self->priv->document);
+ language = gtk_source_buffer_get_language (buffer);
+
+ if (language)
+ {
+ const gchar *lang_id;
+
+ lang_id = gtk_source_language_get_id (language);
+
+ if (g_strcmp0 (lang_id, "markdown") == 0)
+ transform = gb_html_markdown_transform;
+ }
+
document = g_object_new (GB_TYPE_HTML_DOCUMENT,
- "buffer", self->priv->document,
+ "buffer", buffer,
NULL);
+ if (transform)
+ gb_html_document_set_transform_func (GB_HTML_DOCUMENT (document),
+ transform);
+
return document;
}
diff --git a/src/gnome-builder.mk b/src/gnome-builder.mk
index ab9ae15..41b0552 100644
--- a/src/gnome-builder.mk
+++ b/src/gnome-builder.mk
@@ -105,8 +105,6 @@ libgnome_builder_la_SOURCES = \
src/html/gb-html-view.h \
src/markdown/gs-markdown.c \
src/markdown/gs-markdown.h \
- src/markdown/gb-markdown-preview.c \
- src/markdown/gb-markdown-preview.h \
src/navigation/gb-navigation-list.h \
src/navigation/gb-navigation-list.c \
src/navigation/gb-navigation-item.h \
diff --git a/src/html/gb-html-document.c b/src/html/gb-html-document.c
index c823c59..9a560af 100644
--- a/src/html/gb-html-document.c
+++ b/src/html/gb-html-document.c
@@ -24,11 +24,13 @@
#include "gb-editor-document.h"
#include "gb-html-document.h"
#include "gb-html-view.h"
+#include "gs-markdown.h"
struct _GbHtmlDocumentPrivate
{
- GtkTextBuffer *buffer;
- gchar *title;
+ GtkTextBuffer *buffer;
+ gchar *title;
+ GbHtmlDocumentTransform transform;
};
static void gb_html_document_init_document (GbDocumentInterface *iface);
@@ -54,6 +56,41 @@ gb_html_document_new (void)
return g_object_new (GB_TYPE_HTML_DOCUMENT, NULL);
}
+void
+gb_html_document_set_transform_func (GbHtmlDocument *document,
+ GbHtmlDocumentTransform transform)
+{
+ g_return_if_fail (GB_IS_HTML_DOCUMENT (document));
+
+ document->priv->transform = transform;
+}
+
+gchar *
+gb_html_document_get_content (GbHtmlDocument *document)
+{
+ GtkTextIter begin;
+ GtkTextIter end;
+ gchar *tmp;
+ gchar *str;
+
+ g_return_val_if_fail (GB_IS_HTML_DOCUMENT (document), NULL);
+
+ if (!document->priv->buffer)
+ return NULL;
+
+ gtk_text_buffer_get_bounds (document->priv->buffer, &begin, &end);
+ str = gtk_text_iter_get_slice (&begin, &end);
+
+ if (document->priv->transform)
+ {
+ tmp = document->priv->transform (document, str);
+ g_free (str);
+ str = tmp;
+ }
+
+ return str;
+}
+
static const gchar *
gb_html_document_get_title (GbDocument *document)
{
@@ -281,3 +318,47 @@ gb_html_document_init_document (GbDocumentInterface *iface)
iface->get_modified = gb_html_document_get_modified;
iface->create_view = gb_html_document_create_view;
}
+
+gchar *
+gb_html_markdown_transform (GbHtmlDocument *document,
+ const gchar *content)
+{
+ GsMarkdown *md;
+ gchar *str;
+
+ g_return_if_fail (GB_IS_HTML_DOCUMENT (document));
+ g_return_if_fail (content);
+
+ md = gs_markdown_new (GS_MARKDOWN_OUTPUT_HTML);
+ gs_markdown_set_autolinkify (md, TRUE);
+ gs_markdown_set_escape (md, TRUE);
+
+ str = gs_markdown_parse (md, content);
+
+ if (str)
+ {
+ GBytes *css;
+ const guint8 *css_data;
+ gchar *tmp;
+
+ css = g_resources_lookup_data ("/org/gnome/builder/css/markdown.css",
+ 0, NULL);
+ css_data = g_bytes_get_data (css, NULL);
+ tmp = g_strdup_printf ("<html>\n"
+ " <style>%s</style>\n"
+ " <body>\n"
+ " <div class=\"markdown-body\">\n"
+ " %s\n"
+ " </div>\n"
+ " </body>\n"
+ "</html>",
+ (gchar *)css_data, str);
+ g_free (str);
+
+ str = tmp;
+ }
+
+ g_clear_object (&md);
+
+ return str;
+}
diff --git a/src/html/gb-html-document.h b/src/html/gb-html-document.h
index 17fe731..a3fd37f 100644
--- a/src/html/gb-html-document.h
+++ b/src/html/gb-html-document.h
@@ -48,8 +48,16 @@ struct _GbHtmlDocumentClass
GObjectClass parent;
};
-GType gb_html_document_get_type (void);
-GtkTextBuffer *gb_html_document_get_buffer (GbHtmlDocument *document);
+typedef gchar *(*GbHtmlDocumentTransform) (GbHtmlDocument *document,
+ const gchar *content);
+
+GType gb_html_document_get_type (void);
+GtkTextBuffer *gb_html_document_get_buffer (GbHtmlDocument *document);
+void gb_html_document_set_transform_func (GbHtmlDocument *document,
+ GbHtmlDocumentTransform transform);
+gchar *gb_html_document_get_content (GbHtmlDocument *document);
+gchar *gb_html_markdown_transform (GbHtmlDocument *document,
+ const gchar *content);
G_END_DECLS
diff --git a/src/html/gb-html-view.c b/src/html/gb-html-view.c
index 6968fd6..88d1dda 100644
--- a/src/html/gb-html-view.c
+++ b/src/html/gb-html-view.c
@@ -58,9 +58,7 @@ gb_html_view_changed (GbHtmlView *view,
GtkTextBuffer *buffer)
{
GbHtmlViewPrivate *priv;
- GtkTextIter begin;
- GtkTextIter end;
- gchar *content = NULL;
+ gchar *content;
gchar *base_uri = NULL;
ENTRY;
@@ -87,9 +85,7 @@ gb_html_view_changed (GbHtmlView *view,
}
}
- gtk_text_buffer_get_bounds (buffer, &begin, &end);
- content = gtk_text_buffer_get_text (buffer, &begin, &end, TRUE);
-
+ content = gb_html_document_get_content (view->priv->document);
webkit_web_view_load_html (view->priv->web_view, content, base_uri);
g_free (content);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]