diff options
| author | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-20 19:39:38 +0530 |
|---|---|---|
| committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-20 19:39:38 +0530 |
| commit | 9f844fa3a032fc92e69cb5028a9b6e8a5ef0a235 (patch) | |
| tree | 60a8a6542bf2f0534d839a710ede816d7695a5d6 | |
| parent | e4c5d83e418f1a045758339779fb49b635db2bdb (diff) | |
(lexer): add support for contentfor key
| -rw-r--r-- | CMakeLists.txt | 3 | ||||
| -rw-r--r-- | compromyse.xyz/base.html | 1 | ||||
| -rw-r--r-- | compromyse.xyz/index.html | 2 | ||||
| -rw-r--r-- | include/lexer.h | 7 | ||||
| -rw-r--r-- | src/engine.c | 18 | ||||
| -rw-r--r-- | src/lexer.c | 16 |
6 files changed, 40 insertions, 7 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index b2b3fe5..602ef5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,8 @@ set(C_COMPILE_OPTIONS -Wextra # -Werror - -g + -g3 + -glldb -std=c99 diff --git a/compromyse.xyz/base.html b/compromyse.xyz/base.html index a315c2f..5f5937f 100644 --- a/compromyse.xyz/base.html +++ b/compromyse.xyz/base.html @@ -9,7 +9,6 @@ <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script> <link href="https://fonts.googleapis.com/css2?family=Pixelify+Sans:wght@400..700&display=swap" rel="stylesheet"> - </head> <body class="bg-zinc-900 text-pink-300 font-[Pixelify_Sans] selection:text-pink-900 selection:bg-pink-100"> {{ content }} diff --git a/compromyse.xyz/index.html b/compromyse.xyz/index.html index 3d3fe3a..5cab5fd 100644 --- a/compromyse.xyz/index.html +++ b/compromyse.xyz/index.html @@ -1,3 +1,5 @@ +{{ contentfor "head" }} + {{ include "navbar.html" }} <div class="p-16"> diff --git a/include/lexer.h b/include/lexer.h index 8f7aab1..48deb0c 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -1,7 +1,7 @@ #ifndef __LEXER_H #define __LEXER_H -typedef enum { INCLUDE } directive_e; +typedef enum { INCLUDE, CONTENTFOR } directive_e; typedef struct { unsigned int offset; @@ -13,6 +13,11 @@ typedef struct { void *operands; } directive_t; +typedef struct { + char *key; + char *content; +} contentfor_operands_t; + directive_t *find_directive(char *content, key_match_t *match); key_match_t *find_next_key(char *buffer); diff --git a/src/engine.c b/src/engine.c index 4e4d037..e4d362e 100644 --- a/src/engine.c +++ b/src/engine.c @@ -23,12 +23,18 @@ ingest(char **buffer) if (directive == NULL) break; - if (directive->type == INCLUDE) { + switch (directive->type) { + case INCLUDE: { char *operand = (char *) 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); @@ -44,6 +50,16 @@ ingest(char **buffer) temp_buffer + match->offset + match->length); free(temp_buffer); + break; + } + case CONTENTFOR: { + contentfor_operands_t *operand + = (contentfor_operands_t *) directive->operands; + printf("CONTENTFOR: %s\n", operand->key); + + return; + break; + } } free(directive); diff --git a/src/lexer.c b/src/lexer.c index 34db154..dc972e9 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -64,9 +64,7 @@ found_start: directive->type = INCLUDE; char *operand = NULL; - for (size_t i = n + strlen("include"); - i < match->length - strlen("include"); - i++) + for (size_t i = n + strlen("include"); i < match->length; i++) if (isalnum(buffer[i])) { sscanf(buffer + i, "%ms\"", &operand); operand[strlen(operand) - 1] = '\0'; @@ -75,6 +73,18 @@ found_start: asprintf((char **) &directive->operands, "%s", operand); free(operand); + } else if (strncmp(buffer + n, "contentfor", strlen("contentfor")) == 0) { + directive->type = CONTENTFOR; + contentfor_operands_t *operands = malloc(sizeof(contentfor_operands_t)); + + for (size_t i = n + strlen("contentfor"); i < match->length; i++) + if (isalnum(buffer[i])) { + sscanf(buffer + i, "%ms\"", &operands->key); + operands->key[strlen(operands->key) - 1] = '\0'; + break; + } + + directive->operands = operands; } return directive; |
