diff options
-rw-r--r-- | db/db.sql | 3 | ||||
-rw-r--r-- | src/clog.c | 49 |
2 files changed, 44 insertions, 8 deletions
@@ -10,7 +10,6 @@ CREATE TABLE entries ( -- updated_at TIMESTAMP NOT NULL ); - INSERT INTO entries ( id, title, @@ -30,6 +29,6 @@ VALUES ( 3, 'xxx', - 'yyy' + '**bold**' ); @@ -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); |