Bladeren bron

Add markdown compiler based on Discount

Matthias Vogelgesang 7 jaren geleden
bovenliggende
commit
5b590ddfe2
3 gewijzigde bestanden met toevoegingen van 123 en 0 verwijderingen
  1. 89 0
      src/iridium-markdown.c
  2. 24 0
      src/iridium-markdown.h
  3. 10 0
      src/meson.build

+ 89 - 0
src/iridium-markdown.c

@@ -0,0 +1,89 @@
+/* iridium-markdown.c
+ *
+ * Copyright 2018 Matthias Vogelgesang
+ *
+ * This program 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.
+ *
+ * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+#include <mkdio.h>
+#include "iridium-config.h"
+#include "iridium-markdown.h"
+
+struct _IridiumMarkdown
+{
+  GObject parent_instance;
+};
+
+G_DEFINE_TYPE (IridiumMarkdown, iridium_markdown, G_TYPE_OBJECT)
+
+IridiumMarkdown *
+iridium_markdown_new (void)
+{
+  return g_object_new (IRIDIUM_TYPE_MARKDOWN, NULL);
+}
+
+static void
+compile_thread (GTask *task,
+                gpointer source_object,
+                gpointer task_data,
+                GCancellable *cancellable)
+{
+  MMIOT *doc;
+  gchar *markdown;
+  gchar *html = NULL;
+
+  markdown = (gchar *) task_data;
+  doc = mkd_string (markdown, strlen (markdown), MKD_TOC);
+  mkd_compile (doc, MKD_TOC);
+  mkd_document (doc, &html);
+
+  g_task_return_pointer (task, g_strdup (html), g_free);
+  mkd_cleanup (doc);
+}
+
+void
+iridium_markdown_compile_async (IridiumMarkdown *self,
+                                const gchar *markdown,
+                                gint priority,
+                                GCancellable *cancellable,
+                                GAsyncReadyCallback callback,
+                                gpointer user_data)
+{
+  GTask *task;
+
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_run_in_thread (task, compile_thread);
+  g_task_set_task_data (task, g_strdup (markdown), g_free);
+  g_object_unref (task);
+}
+
+gpointer
+iridium_markdown_compile_finish (GObject *object,
+                                 GAsyncResult *result,
+                                 GError **error)
+{
+  g_return_val_if_fail (g_task_is_valid (result, object), NULL);
+  return g_task_propagate_pointer (G_TASK (result), error);
+}
+
+static void
+iridium_markdown_class_init (IridiumMarkdownClass *klass)
+{
+}
+
+static void
+iridium_markdown_init (IridiumMarkdown *self)
+{
+}

+ 24 - 0
src/iridium-markdown.h

@@ -0,0 +1,24 @@
+#pragma once
+
+#include <glib-object.h>
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define IRIDIUM_TYPE_MARKDOWN (iridium_markdown_get_type())
+
+G_DECLARE_FINAL_TYPE (IridiumMarkdown, iridium_markdown, IRIDIUM, MARKDOWN, GObject)
+
+IridiumMarkdown *iridium_markdown_new               (void);
+void             iridium_markdown_compile_async     (IridiumMarkdown    *self,
+                                                     const gchar        *markdown,
+                                                     gint                priority,
+                                                     GCancellable       *cancellable,
+                                                     GAsyncReadyCallback callback,
+                                                     gpointer            user_data);
+gpointer         iridium_markdown_compile_finish    (GObject            *object,
+                                                     GAsyncResult       *result,
+                                                     GError            **error);
+
+G_END_DECLS
+

+ 10 - 0
src/meson.build

@@ -1,3 +1,5 @@
+cc = meson.get_compiler('c')
+
 iridium_sources = [
   'main.c',
   'fuzzy.c',
@@ -6,6 +8,7 @@ iridium_sources = [
   'iridium-tag.c',
   'iridium-tag-row.c',
   'iridium-storage.c',
+  'iridium-markdown.c',
   'iridium-window.c',
 ]
 
@@ -13,8 +16,15 @@ iridium_deps = [
   dependency('gio-2.0', version: '>= 2.48'),
   dependency('gtk+-3.0', version: '>= 3.18'),
   dependency('gtksourceview-3.0', version: '>= 3.18'),
+  declare_dependency(
+    dependencies: cc.find_library('markdown'),
+  ),
 ]
 
+if not cc.has_header('mkdio.h')
+  error('Cannot find mkdio.h')
+endif
+
 gnome = import('gnome')
 
 iridium_sources += gnome.compile_resources('iridium-resources',