aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-06-24 16:36:51 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2025-06-24 16:36:51 +0530
commit665cd823efa91f2c1d614531f199e8e990709575 (patch)
tree909acc8684973d11b23db7323dd748594629ad5e /src
parentdbf8b45bbd40dd28f934ad23b227576ff60335cf (diff)
(engine): split handling into separate functions
Diffstat (limited to 'src')
-rw-r--r--src/engine.c107
-rw-r--r--src/lexer.c3
-rw-r--r--src/template.c1
3 files changed, 65 insertions, 46 deletions
diff --git a/src/engine.c b/src/engine.c
index e5ee5a3..201aab1 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -10,6 +10,64 @@
#include "../config.h"
+static void
+handle_include(char **buffer, key_match_t *match, directive_t *directive)
+{
+ char *operand = directive->operands;
+ char *partial_path;
+ asprintf(&partial_path, "%s/%s/%s", DIRECTORY, PARTIALS, operand);
+
+ FILE *f = fopen(partial_path, "r");
+ if (f == NULL) {
+ printf("Could not open: %s\n", partial_path);
+ return;
+ }
+
+ unsigned int size = fsize(f);
+ char *partial_content = fcontent(f, size);
+
+ char *temp_buffer;
+ asprintf(&temp_buffer, "%s", *buffer);
+
+ free(*buffer);
+ asprintf(buffer,
+ "%.*s%s%s\n",
+ match->offset,
+ temp_buffer,
+ partial_content,
+ temp_buffer + match->offset + match->length);
+
+ free(temp_buffer);
+}
+
+static void
+handle_contentfor(char **buffer,
+ key_match_t *match,
+ directive_t *directive,
+ list_t *content_headers)
+{
+ contentfor_operand_t *operand = directive->operands;
+ list_add(content_headers, operand);
+
+#ifdef DEBUG
+ printf("CONTENTFOR: %s\n", operand->key);
+ printf("CONTENT: %s\n", operand->content);
+#endif
+
+ char *temp_buffer;
+ asprintf(&temp_buffer, "%s", *buffer);
+
+ free(*buffer);
+ asprintf(buffer,
+ "%.*s%s",
+ match->offset,
+ temp_buffer,
+ temp_buffer + operand->length);
+
+ free(temp_buffer);
+ free(operand);
+}
+
void
ingest(char **buffer)
{
@@ -43,59 +101,16 @@ ingest(char **buffer)
switch (directive->type) {
case INCLUDE: {
- char *operand = directive->operands;
- char *partial_path;
- asprintf(&partial_path, "%s/%s/%s", DIRECTORY, PARTIALS, operand);
-
- FILE *f = fopen(partial_path, "r");
- if (f == NULL) {
- printf("Could not open: %s\n", partial_path);
- return;
- }
-
- unsigned int size = fsize(f);
- char *partial_content = fcontent(f, size);
-
- char *temp_buffer;
- asprintf(&temp_buffer, "%s", *buffer);
-
- free(*buffer);
- asprintf(buffer,
- "%.*s%s%s\n",
- match->offset,
- temp_buffer,
- partial_content,
- temp_buffer + match->offset + match->length);
-
- free(temp_buffer);
+ handle_include(buffer, match, directive);
break;
}
case CONTENTFOR: {
- contentfor_operand_t *operand = directive->operands;
- list_add(content_headers, operand);
-
- /* printf("CONTENTFOR: %s\n", operand->key); */
- /* printf("CONTENT: %s\n", operand->content); */
-
- /* printf("CONTENT: %.*s\n", operand->length, *buffer + match->offset);
- */
-
- char *temp_buffer;
- asprintf(&temp_buffer, "%s", *buffer);
-
- free(*buffer);
- asprintf(buffer,
- "%.*s%s",
- match->offset,
- temp_buffer,
- temp_buffer + operand->length);
-
- free(temp_buffer);
- /* free(operand); */
+ handle_contentfor(buffer, match, directive, content_headers);
break;
}
/* NOTE: This will never occur */
+ case BODY:
case ENDCONTENT:
break;
}
diff --git a/src/lexer.c b/src/lexer.c
index ea2a94e..2a80ddb 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -80,6 +80,9 @@ found_start:
} else if (strncmp(buffer + n, "endcontent", strlen("endcontent")) == 0) {
directive->type = ENDCONTENT;
directive->operands = NULL;
+ } else if (strncmp(buffer + n, "body", strlen("body")) == 0) {
+ directive->type = BODY;
+ directive->operands = NULL;
} else if (strncmp(buffer + n, "contentfor", strlen("contentfor")) == 0) {
directive->type = CONTENTFOR;
contentfor_operand_t *operands = malloc(sizeof(contentfor_operand_t));
diff --git a/src/template.c b/src/template.c
index e3ce60a..e8f8fd5 100644
--- a/src/template.c
+++ b/src/template.c
@@ -20,6 +20,7 @@ template_create(void)
fclose(base);
key_match_t *match = find_next_key(contents);
+
asprintf(&template->pre, "%.*s", match->offset, contents);
asprintf(&template->post,
"%.*s",