aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/engine.c29
-rw-r--r--src/msg.c11
-rw-r--r--src/template.c18
3 files changed, 33 insertions, 25 deletions
diff --git a/src/engine.c b/src/engine.c
index b9825bf..d62b44f 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -96,8 +96,8 @@ handle_eachdo(char **buffer, key_match_t *match, directive_t *directive)
{
eachdo_operands_t *operands = directive->operands;
- list_t *content_headers = engine_ingest(&operands->content);
- list_delete(content_headers);
+ engine_t *engine = engine_ingest(&operands->content);
+ engine_delete(engine);
list_t *directives = lex(operands->content);
#ifdef DEBUG
@@ -206,18 +206,21 @@ handle_eachdo(char **buffer, key_match_t *match, directive_t *directive)
free(temp_buffer);
}
-list_t *
+engine_t *
engine_ingest(char **buffer)
{
- /* don't ingest the config if it is present */
+ engine_t *engine = malloc(sizeof(engine_t));
+ engine->config = NULL;
+
char *p = strstr(*buffer, "---");
if (p != NULL) {
+ engine->config = config_parse(p);
strcpy(*buffer, p + strlen("---"));
}
key_match_t *match;
- list_t *content_headers = list_create(sizeof(contentfor_operand_t));
- if (content_headers == NULL) {
+ engine->content_headers = list_create(sizeof(contentfor_operand_t));
+ if (engine->content_headers == NULL) {
printf("Could not create content_headers\n");
return NULL;
}
@@ -249,7 +252,7 @@ engine_ingest(char **buffer)
handle_include(buffer, match, directive);
break;
case CONTENTFOR:
- handle_contentfor(buffer, match, directive, content_headers);
+ handle_contentfor(buffer, match, directive, engine->content_headers);
break;
case EACHDO:
handle_eachdo(buffer, match, directive);
@@ -271,5 +274,15 @@ engine_ingest(char **buffer)
free(match);
}
- return content_headers;
+ return engine;
+}
+
+void
+engine_delete(engine_t *engine)
+{
+ if (engine->config != NULL)
+ config_delete(engine->config);
+
+ list_delete(engine->content_headers);
+ free(engine);
}
diff --git a/src/msg.c b/src/msg.c
index 76cf9cc..65f4293 100644
--- a/src/msg.c
+++ b/src/msg.c
@@ -37,7 +37,6 @@
#include <util.h>
extern msg_t *msg;
-template_t *base_template;
void
handle_file(const char *path)
@@ -95,11 +94,11 @@ handle_file(const char *path)
if (dot && strcmp(dot, ".md") == 0) {
MMIOT *doc = mkd_string(buffer, size, 0);
- template_write(base_template, NULL, out, doc, true);
+ template_write(NULL, out, doc, true);
} else if (strlen(buffer) != 0) {
- list_t *content_headers = engine_ingest(&buffer);
- template_write(base_template, content_headers, out, buffer, false);
- list_delete(content_headers);
+ engine_t *engine = engine_ingest(&buffer);
+ template_write(engine, out, buffer, false);
+ engine_delete(engine);
}
free(buffer);
@@ -129,7 +128,6 @@ run(void)
return EXIT_FAILURE;
template_initialize();
- base_template = template_create(BASE_TEMPLATE);
int err = mkdir(msg->output_directory, 0700);
if (err != 0 && errno != EEXIST) {
@@ -178,7 +176,6 @@ run(void)
handle_file(path);
}
- template_delete(base_template);
template_clean();
config_delete(config);
diff --git a/src/template.c b/src/template.c
index aea077e..1ebda4b 100644
--- a/src/template.c
+++ b/src/template.c
@@ -23,6 +23,7 @@
#include <engine.h>
#include <filehandler.h>
#include <lexer.h>
+#include <list.h>
#include <mkdio.h>
#include <msg.h>
#include <stdio.h>
@@ -114,8 +115,8 @@ template_create(char *template_name)
char *buffer = fcontent(base, size);
fclose(base);
- list_t *content_headers = engine_ingest(&buffer);
- list_delete(content_headers);
+ engine_t *engine = engine_ingest(&buffer);
+ engine_delete(engine);
template->components = lex(buffer);
free(buffer);
@@ -130,14 +131,11 @@ template_delete(template_t *template)
}
void
-template_write(template_t *template,
- list_t *content_headers,
- FILE *f,
- void *doc,
- bool is_markdown)
+template_write(engine_t *engine, FILE *f, void *doc, bool is_markdown)
{
- char *output = malloc(1);
- strcpy(output, "");
+ template_t *template = template
+ = list_find_corresponding_value_from_ptr_wrapper(
+ keys, templates, "base.html");
for (size_t i = 0; i < template->components->size; i++) {
directive_t *match = list_get(template->components, i);
@@ -151,7 +149,7 @@ template_write(template_t *template,
/* TODO: handle this gracefully */
if (!is_markdown) {
char *content
- = find_contentfor_value(content_headers, match->operands);
+ = find_contentfor_value(engine->content_headers, match->operands);
fprintf(f, "%s", content);
}
break;