diff options
author | Michael McVady <femtonaut@gmail.com> | 2021-11-17 21:34:41 -0500 |
---|---|---|
committer | Michael McVady <femtonaut@gmail.com> | 2021-11-17 21:34:41 -0500 |
commit | 724627420b8aca92fbf4c16a30d4593c25a37f13 (patch) | |
tree | ec7e8fcc1461a1531a78c076f13d245b295cd2ba /src/clog.c | |
parent | e564fb5054ffe1c17d8c19d0f5b749aa405044e4 (diff) |
Render markdown as HTML
Diffstat (limited to 'src/clog.c')
-rw-r--r-- | src/clog.c | 49 |
1 files changed, 43 insertions, 6 deletions
@@ -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<tr><td>%s</td><td>%s</td><td>%s</td></tr>\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<tr><td>%s</td><td>%s</td><td>%s</td></tr>\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); |