aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compromyse.xyz/posts/a.html1
-rw-r--r--compromyse.xyz/templates/xyz.html4
-rw-r--r--include/engine.h9
-rw-r--r--include/template.h7
-rw-r--r--src/engine.c29
-rw-r--r--src/msg.c11
-rw-r--r--src/template.c18
7 files changed, 48 insertions, 31 deletions
diff --git a/compromyse.xyz/posts/a.html b/compromyse.xyz/posts/a.html
index 8237d7f..6722f21 100644
--- a/compromyse.xyz/posts/a.html
+++ b/compromyse.xyz/posts/a.html
@@ -1,4 +1,5 @@
title = A.html
+template = xyz.html
---
<p>a.html</p>
diff --git a/compromyse.xyz/templates/xyz.html b/compromyse.xyz/templates/xyz.html
new file mode 100644
index 0000000..7b61a16
--- /dev/null
+++ b/compromyse.xyz/templates/xyz.html
@@ -0,0 +1,4 @@
+<!DOCTYPE html>
+<html lang="en">
+ {{ body }}
+</html>
diff --git a/include/engine.h b/include/engine.h
index 86aa167..137f201 100644
--- a/include/engine.h
+++ b/include/engine.h
@@ -19,10 +19,17 @@
#ifndef __ENGINE_H
#define __ENGINE_H
+#include <config.h>
#include <lexer.h>
#include <list.h>
-list_t *engine_ingest(char **buffer);
+typedef struct {
+ list_t *content_headers;
+ config_t *config;
+} engine_t;
+
+engine_t *engine_ingest(char **buffer);
+void engine_delete(engine_t *engine);
void handle_include(char **buffer, key_match_t *match, directive_t *directive);
void handle_contentfor(char **buffer,
key_match_t *match,
diff --git a/include/template.h b/include/template.h
index 4676feb..122825c 100644
--- a/include/template.h
+++ b/include/template.h
@@ -19,6 +19,7 @@
#ifndef __TEMPLATE_H
#define __TEMPLATE_H
+#include <engine.h>
#include <list.h>
#include <stdbool.h>
#include <stdio.h>
@@ -31,10 +32,6 @@ void template_initialize(void);
void template_clean(void);
template_t *template_create(char *template_name);
void template_delete(template_t *template);
-void template_write(template_t *template,
- list_t *content_headers,
- FILE *f,
- void *doc,
- bool is_markdown);
+void template_write(engine_t *engine, FILE *f, void *doc, bool is_markdown);
#endif
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;