aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael McVady <femtonaut@gmail.com>2021-11-17 21:34:41 -0500
committerMichael McVady <femtonaut@gmail.com>2021-11-17 21:34:41 -0500
commit724627420b8aca92fbf4c16a30d4593c25a37f13 (patch)
treeec7e8fcc1461a1531a78c076f13d245b295cd2ba
parente564fb5054ffe1c17d8c19d0f5b749aa405044e4 (diff)
Render markdown as HTML
-rw-r--r--db/db.sql3
-rw-r--r--src/clog.c49
2 files changed, 44 insertions, 8 deletions
diff --git a/db/db.sql b/db/db.sql
index 3f27856..4df3345 100644
--- a/db/db.sql
+++ b/db/db.sql
@@ -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**'
);
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<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);