Implemented basic formatting mode

newfile
Markus Koch 6 years ago
parent 869cb20eaf
commit aa8a9a003b

@ -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 {

@ -6,6 +6,7 @@
#include <gtk/gtk.h>
#include <webkit2/webkit2.h>
#include "mdiary.h"
#include "md_pango.h"
#define FILE_LIST_MAX_LEN 5

@ -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, "&gt;", 0, NULL);
g_regex_unref(regex);
regex = g_regex_new("\\<", 0, 0, NULL);
str2 = g_regex_replace(regex, str, -1, 0, "&lt;", 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, "<b>\\1</b>", 0, NULL);
g_regex_unref(regex);
g_free(str2);
regex = g_regex_new("\\*([^\\*]*)\\*", 0, 0, NULL);
str2 = g_regex_replace(regex, str, -1, 0, "<i>\\1</i>", 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;
}

@ -0,0 +1,7 @@
#ifndef MD_PANGO_H
#define MD_PANGO_H
#include <glib.h>
gchar *md_pango_render(gchar *markup, gint length);
#endif /* MD_PANGO_H */
Loading…
Cancel
Save