diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..a7a29e3 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "src/hoedown"] + path = src/hoedown + url = git@github.com:hoedown/hoedown.git diff --git a/CMakeLists.txt b/CMakeLists.txt index cef6f9e..f7ca620 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,9 +21,21 @@ add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/resources.c" COMMENT "Generating resources" ) +add_custom_command(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/hoedown/libhoedown.a" + COMMAND make libhoedown.a + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src/hoedown" + COMMENT "Compiling libhoedown" +) + +ADD_LIBRARY(HOEDOWN STATIC IMPORTED) +SET_TARGET_PROPERTIES(HOEDOWN PROPERTIES + IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/src/hoedown/libhoedown.a" +) + aux_source_directory(./src SRC_LIST) add_executable(${PROJECT_NAME} ${SRC_LIST}) target_sources(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/resources.c") -target_link_libraries(${PROJECT_NAME} ${GTK3_LIBRARIES} ${WEBKIT_LIBRARIES}) +target_sources(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src/hoedown/libhoedown.a") +target_link_libraries(${PROJECT_NAME} ${GTK3_LIBRARIES} ${WEBKIT_LIBRARIES} HOEDOWN) install (TARGETS ${PROJECT_NAME} DESTINATION bin) diff --git a/demo-workspace/2018-01-26.md b/demo-workspace/2018-01-26.md index 8670347..09644ab 100644 --- a/demo-workspace/2018-01-26.md +++ b/demo-workspace/2018-01-26.md @@ -13,7 +13,7 @@ Renders everything as is. You will see the source code except for the header abo The following constructs are supported: *italics*, **bold**, Headings 1 to 4. ## Full HTML with Images -As the external md command is invoked and a web browser used to render the content, all markdown syntax supported by the md command is supported. +Markdown is rendered using [Hoedown](https://github.com/hoedown/hoedown/) and displayed using an integrated web browser. This all Markdown syntax (including images) are supported. # Encrypted Entries Using GPG MDiary supports gpg encrypted entries. To use this feature, place a gpg encrypted entry as *.md.gpg* in the workspace. MDiary will automatically show a decrypt button in the header bar which will invoke gpg to decrypt all encrypted entries in the workspace for this session. diff --git a/src/hoedown b/src/hoedown new file mode 160000 index 0000000..980b9c5 --- /dev/null +++ b/src/hoedown @@ -0,0 +1 @@ +Subproject commit 980b9c549b4348d50b683ecee6abee470b98acda diff --git a/src/mainwindow.c b/src/mainwindow.c index c5feead..0b91ac1 100644 --- a/src/mainwindow.c +++ b/src/mainwindow.c @@ -771,16 +771,18 @@ void mainWindow_calendarSelected(GtkWidget *widget, gpointer user_data) static void mainWindow_set_text(struct mainWindow *mainWindow, gchar *text, gchar *file_url) { - gchar *tempfile_path = "/tmp/mdiary.tmp.md"; gchar *cmd = NULL; gchar *cmd_stdout = NULL; gchar *cmd_stderr = NULL; - gint exit_status = 0; - gchar *base_url = NULL; GFileOutputStream *stream = NULL; GFile *file = NULL; GtkTextIter iter; GtkTextIter iter_end; + hoedown_buffer *ob; + hoedown_renderer *renderer = NULL; + void (*renderer_free)(hoedown_renderer *) = NULL; + hoedown_document *document; + gchar *base_url = NULL; if (mainWindow->renderer == RENDERER_TEXT) { gtk_text_buffer_set_text(mainWindow->textBuffer, text, strlen(text)); @@ -802,24 +804,21 @@ static void mainWindow_set_text(struct mainWindow *mainWindow, gchar *text, gcha } else { if (text[0] != '\0') { base_url = g_strdup_printf("file://%s", file_url); - file = g_file_new_for_path(tempfile_path); - stream = g_file_replace( - file, NULL, FALSE, - G_FILE_CREATE_NONE, - NULL, NULL); - g_output_stream_write(G_OUTPUT_STREAM(stream), text, strlen(text), NULL, NULL); - g_output_stream_flush(G_OUTPUT_STREAM(stream), NULL, NULL); + g_info("Rendering markdown as full HTML..."); - 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) { - webkit_web_view_load_html(WEBKIT_WEB_VIEW(mainWindow->webkitView), - cmd_stdout, base_url); - } else { - g_warning("Could not render markdown for file. Showing plain text instead.\n"); - webkit_web_view_load_html(WEBKIT_WEB_VIEW(mainWindow->webkitView), - text, base_url); - } + renderer = hoedown_html_renderer_new(0, 0); + renderer_free = hoedown_html_renderer_free; + + ob = hoedown_buffer_new(64); + document = hoedown_document_new(renderer, 0, 16); + hoedown_document_render(document, ob, (const uint8_t *) text, strlen(text)); + + webkit_web_view_load_html(WEBKIT_WEB_VIEW(mainWindow->webkitView), + hoedown_buffer_cstr(ob), base_url); + + hoedown_buffer_free(ob); + hoedown_document_free(document); + renderer_free(renderer); g_free(base_url); } else { webkit_web_view_load_html(WEBKIT_WEB_VIEW(mainWindow->webkitView), diff --git a/src/mainwindow.h b/src/mainwindow.h index 3348c85..1b1dfe2 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -8,6 +8,9 @@ #include "mdiary.h" #include "md_pango.h" +#include "hoedown/src/document.h" +#include "hoedown/src/html.h" + #define FILE_LIST_MAX_LEN 5 enum {