diff --git a/src/mainwindow.c b/src/mainwindow.c index 2a5d6ad..1c50b41 100644 --- a/src/mainwindow.c +++ b/src/mainwindow.c @@ -93,6 +93,8 @@ static gboolean mainWindow_list_entry_visible(GtkTreeModel *model, search_matched = 1; g_free(temp); + g_regex_unref(regex); + if (!search_matched) return 0; } @@ -672,9 +674,26 @@ static void mainWindow_set_text(struct mainWindow *mainWindow, gchar *text, gcha gchar *base_url = NULL; GFileOutputStream *stream = NULL; GFile *file = NULL; + GtkTextIter iter; + GtkTextIter iter_end; if (mainWindow->renderer == RENDERER_TEXT) { gtk_text_buffer_set_text(mainWindow->textBuffer, text, strlen(text)); + } else if (mainWindow->renderer == RENDERER_BASIC) { + cmd_stdout = md_pango_render(text, strlen(text)); + gtk_text_buffer_set_text(mainWindow->textBuffer, "", 0); + gtk_text_buffer_get_start_iter(mainWindow->textBuffer, &iter); + gtk_text_buffer_insert_markup(mainWindow->textBuffer, + &iter, + cmd_stdout, + strlen(cmd_stdout)); + g_free(cmd_stdout); + gtk_text_buffer_get_start_iter(mainWindow->textBuffer, &iter); + gtk_text_buffer_get_end_iter(mainWindow->textBuffer, &iter_end); + if (strlen(gtk_text_buffer_get_text(mainWindow->textBuffer, &iter, &iter_end, 0)) == 0) { + g_warning("Could not render markdown for file. Showing plain text instead.\n"); + gtk_text_buffer_set_text(mainWindow->textBuffer, text, strlen(text)); + } } else { if (text[0] != '\0') { base_url = g_strdup_printf("file://%s", file_url); @@ -689,28 +708,12 @@ static void mainWindow_set_text(struct mainWindow *mainWindow, gchar *text, gcha cmd = g_strdup_printf("markdown %s", tempfile_path); if (g_spawn_command_line_sync(cmd, &cmd_stdout, &cmd_stderr, &exit_status, NULL) && exit_status == 0) { - if (mainWindow->renderer == RENDERER_BASIC) { - /* TODO: This renderer is broken. Pango does not like HTML... */ - GtkTextIter iter; - gtk_text_buffer_set_text(mainWindow->textBuffer, "", 0); - gtk_text_buffer_get_start_iter(mainWindow->textBuffer, &iter); - gtk_text_buffer_insert_markup(mainWindow->textBuffer, - &iter, - cmd_stdout, - strlen(cmd_stdout)); - } else { - webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView), - cmd_stdout, base_url); - } + webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView), + cmd_stdout, base_url); } else { g_warning("Could not render markdown for file. Showing plain text instead.\n"); - if (mainWindow->renderer == RENDERER_BASIC) { - gtk_text_buffer_set_text(mainWindow->textBuffer, - cmd_stdout, strlen(cmd_stdout)); - } else { - webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView), - text, base_url); - } + webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView), + text, base_url); } g_free(base_url); } else { diff --git a/src/mainwindow.h b/src/mainwindow.h index 2853f30..1848b37 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -6,6 +6,7 @@ #include #include #include "mdiary.h" +#include "md_pango.h" #define FILE_LIST_MAX_LEN 5 diff --git a/src/md_pango.c b/src/md_pango.c new file mode 100644 index 0000000..a900f7c --- /dev/null +++ b/src/md_pango.c @@ -0,0 +1,34 @@ +#include "md_pango.h" + +gchar *md_pango_render(gchar *markup, gint length) +{ + gchar *str; + gchar *str2; + GRegex *regex; + + regex = g_regex_new("\\>", 0, 0, NULL); + str = g_regex_replace(regex, markup, -1, 0, ">", 0, NULL); + g_regex_unref(regex); + + regex = g_regex_new("\\<", 0, 0, NULL); + str2 = g_regex_replace(regex, str, -1, 0, "<", 0, NULL); + g_regex_unref(regex); + g_free(str); + + regex = g_regex_new("\\*{2}([^\\*]*)\\*{2}", 0, 0, NULL); + str = g_regex_replace(regex, str2, -1, 0, "\\1", 0, NULL); + g_regex_unref(regex); + g_free(str2); + + regex = g_regex_new("\\*([^\\*]*)\\*", 0, 0, NULL); + str2 = g_regex_replace(regex, str, -1, 0, "\\1", 0, NULL); + g_regex_unref(regex); + g_free(str); + + regex = g_regex_new("\\!\\[([^\\]]*)\\]\\(([^\\)]*)\\)", 0, 0, NULL); + str = g_regex_replace(regex, str2, -1, 0, "-- Image \\1 (\\2) --", 0, NULL); + g_regex_unref(regex); + g_free(str2); + + return str; +} diff --git a/src/md_pango.h b/src/md_pango.h new file mode 100644 index 0000000..83308b6 --- /dev/null +++ b/src/md_pango.h @@ -0,0 +1,7 @@ +#ifndef MD_PANGO_H +#define MD_PANGO_H + +#include +gchar *md_pango_render(gchar *markup, gint length); + +#endif /* MD_PANGO_H */