summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-01-02 01:25:07 -0600
committersanine <sanine.not@pm.me>2022-01-02 01:25:07 -0600
commita0c01b8ab019039094f036a287db6cfdbe9cdca6 (patch)
treedddd4e3968011c8857df7de849d80aed18f32cb4
parente7c41c6d0af6f718f431fbc8ec9b488d37d118d9 (diff)
implement markdown() function
-rw-r--r--CMakeLists.txt1
-rw-r--r--src/argent.c28
-rw-r--r--src/bindings.c66
-rw-r--r--src/bindings.h8
4 files changed, 99 insertions, 4 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4e707b6..68a57e4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -29,6 +29,7 @@ set(HONEYSUCKLE_SOURCES
add_executable(argent
${SRC_ROOT}/argent.c
${SRC_ROOT}/options.c
+ ${SRC_ROOT}/bindings.c
${MD4C_SOURCES}
${HONEYSUCKLE_SOURCES})
set_target_properties(argent PROPERTIES
diff --git a/src/argent.c b/src/argent.c
index c42161a..7426394 100644
--- a/src/argent.c
+++ b/src/argent.c
@@ -3,10 +3,14 @@
#include <lauxlib.h>
#include "honeysuckle.h"
-#include "md4c-html.h"
-#include "tinydir.h"
#include "options.h"
+#include "bindings.h"
+
+
+struct settings {
+ const char *template_dir;
+};
int main(int argc, char **argv)
@@ -25,11 +29,27 @@ int main(int argc, char **argv)
lua_State *L = luaL_newstate();
luaL_openlibs(L);
- error = luaL_dofile(L, opts.conf_filename);
+ hs_create_table
+ (L,
+ hs_str_cfunc("markdown", markdown)
+ );
+ lua_setglobal(L, "argent");
+
+ error = luaL_loadfile(L, opts.conf_filename);
+ if (error != 0) {
+ fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
+ lua_close(L);
+ return error;
+ }
+ error = hs_call(L, 0, 1);
if (error != 0) {
fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
+ lua_close(L);
+ return error;
}
-
+
+ printf("here!\n");
+
lua_close(L);
return 0;
}
diff --git a/src/bindings.c b/src/bindings.c
new file mode 100644
index 0000000..235e2df
--- /dev/null
+++ b/src/bindings.c
@@ -0,0 +1,66 @@
+#include <stdlib.h>
+#include <string.h>
+#include <lua.h>
+
+#include "honeysuckle.h"
+#include "md4c-html.h"
+#include "bindings.h"
+
+struct concat_buffer {
+ char *buf;
+ size_t size;
+ int index;
+ bool ok;
+};
+
+static void md_callback(const MD_CHAR *html, MD_SIZE size, void *data)
+{
+ struct concat_buffer *d = data;
+
+ if (!d->ok)
+ return;
+
+ if (d->index + size >= d->size) {
+ char *new_buf = realloc(d->buf, d->size * 2);
+ if (new_buf == NULL) {
+ // failed to allocate memory, abort!
+ d->ok = false;
+ return;
+ }
+
+ d->buf = new_buf;
+ d->size *= 2;
+ }
+
+ memcpy((d->buf) + d->index, html, size);
+ d->index += size;
+}
+
+int markdown(lua_State *L)
+{
+ char *markdown_buffer;
+ hs_parse_args(L, hs_str(markdown_buffer));
+ size_t len = strlen(markdown_buffer);
+
+ unsigned int md_flags = MD_FLAG_TABLES | MD_FLAG_STRIKETHROUGH | MD_FLAG_UNDERLINE;
+
+ struct concat_buffer data;
+ data.buf = malloc(128 * sizeof(char));
+ data.size = 128 * sizeof(char);
+ data.index = 0;
+ data.ok = true;
+
+ // fill out the buffer
+ int error = md_html(markdown_buffer, len, md_callback, &data, md_flags, 0);
+ if (error)
+ hs_throw_error(L, "markdown parsing failed!");
+
+ if (data.ok == false)
+ hs_throw_error(L, "encountered error (re)allocating html buffer memory!");
+
+ lua_pushstring(L, data.buf);
+
+ free(data.buf);
+
+ return 1;
+}
diff --git a/src/bindings.h b/src/bindings.h
new file mode 100644
index 0000000..3ee3a1a
--- /dev/null
+++ b/src/bindings.h
@@ -0,0 +1,8 @@
+#ifndef ARGENT_BINDINGS_H
+#define ARGENT_BINDINGS_H
+
+#include <lua.h>
+
+int markdown(lua_State *L);
+
+#endif