Browse Source

Change Markdown renderer to statically link hoedown

mdrework
Markus Koch 4 years ago
parent
commit
16c3dcac15
  1. 3
      .gitmodules
  2. 14
      CMakeLists.txt
  3. 2
      demo-workspace/2018-01-26.md
  4. 1
      src/hoedown
  5. 41
      src/mainwindow.c
  6. 3
      src/mainwindow.h

3
.gitmodules vendored

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

14
CMakeLists.txt

@ -21,9 +21,21 @@ add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/resources.c" @@ -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)

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

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

41
src/mainwindow.c

@ -771,16 +771,18 @@ void mainWindow_calendarSelected(GtkWidget *widget, gpointer user_data) @@ -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 @@ -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);
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);
}
g_info("Rendering markdown as full HTML...");
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),

3
src/mainwindow.h

@ -8,6 +8,9 @@ @@ -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 {

Loading…
Cancel
Save