From 724627420b8aca92fbf4c16a30d4593c25a37f13 Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Wed, 17 Nov 2021 21:34:41 -0500 Subject: Render markdown as HTML --- src/clog.c | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) (limited to 'src/clog.c') diff --git a/src/clog.c b/src/clog.c index 87e58e7..92e1dd2 100644 --- a/src/clog.c +++ b/src/clog.c @@ -4,6 +4,13 @@ #include "assets.h" +#include "../lib/md4c/src/entity.h" +#include "../lib/md4c/src/entity.c" +#include "../lib/md4c/src/md4c.h" +#include "../lib/md4c/src/md4c.c" +#include "../lib/md4c/src/md4c-html.h" +#include "../lib/md4c/src/md4c-html.c" + int init(int); int page(struct http_request *); @@ -19,6 +26,29 @@ int init(int state) return (err); } +static void +process_md_output(const MD_CHAR* html, MD_SIZE size, void *buf) +{ + kore_buf_append((struct kore_buf *) buf, (void *) html, (size_t) size); +} + +static int +render_md(char *in, struct kore_buf *out) +{ + static unsigned parser_flags = 0; + static unsigned renderer_flags = MD_HTML_FLAG_DEBUG; + + ret = md_html(in, (MD_SIZE) strlen(in), process_md_output, (void*) out, + parser_flags, renderer_flags); + + if(ret != 0) { + fprintf(stderr, "Parsing failed.\n"); + return -1; + } + + return 1; +} + int page(struct http_request *req) { @@ -62,9 +92,7 @@ int page(struct http_request *req) goto out; } - /* - * Iterate over the result and dump it to somewhere. - */ + // Iterate over the result and dump it to somewhere. rows = kore_pgsql_ntuples(&sql); for (row = 0; row < rows; row++) { id = kore_pgsql_getvalue(&sql, row, 0); @@ -74,8 +102,17 @@ int page(struct http_request *req) kore_log(LOG_NOTICE, "id: '%s'", id); kore_log(LOG_NOTICE, "title: '%s'", title); kore_log(LOG_NOTICE, "body: '%s'", body); - - kore_buf_appendf(buf, "\t%s%s%s\n", id, title, body); + + struct kore_buf *html = kore_buf_alloc(4096); + if (!render_md(body, html)) { + kore_log(LOG_NOTICE, "Error rendering markdown."); + kore_buf_free(html); + continue; + } + kore_log(LOG_NOTICE, "html: '%s'", kore_buf_stringify(html, NULL)); + + kore_buf_appendf(buf, "\t%s%s%s\n", id, title, kore_buf_stringify(html, NULL)); + kore_buf_free(html); } out: ; @@ -87,7 +124,7 @@ out: ; // http_response_stream ??? kore_buf_free(buf); - /* Don't forget to cleanup the kore_pgsql data structure. */ + // Cleanup the kore_pgsql data structure. kore_pgsql_cleanup(&sql); return (KORE_RESULT_OK); -- cgit v1.2.3