Change Markdown renderer to statically link hoedown

This commit is contained in:
Markus Koch 2018-04-07 19:47:22 +02:00
parent 1287949669
commit 16c3dcac15
6 changed files with 40 additions and 22 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "src/hoedown"]
path = src/hoedown
url = git@github.com:hoedown/hoedown.git

View File

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

View File

@ -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 *<name>.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.

1
src/hoedown Submodule

@ -0,0 +1 @@
Subproject commit 980b9c549b4348d50b683ecee6abee470b98acda

View File

@ -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),

View File

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