diff options
-rw-r--r-- | include/lexer.h | 2 | ||||
-rw-r--r-- | src/engine.c | 107 | ||||
-rw-r--r-- | src/lexer.c | 3 | ||||
-rw-r--r-- | src/template.c | 1 |
4 files changed, 66 insertions, 47 deletions
diff --git a/include/lexer.h b/include/lexer.h index 2a03cec..dc6c744 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -1,7 +1,7 @@ #ifndef __LEXER_H #define __LEXER_H -typedef enum { INCLUDE, CONTENTFOR, ENDCONTENT } directive_e; +typedef enum { INCLUDE, CONTENTFOR, ENDCONTENT, BODY } directive_e; typedef struct { unsigned int offset; 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", |