aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-08-28 20:10:24 -0400
committerRaghuram Subramani <raghus2247@gmail.com>2025-08-28 20:10:24 -0400
commit9430a0c1a919cd4264fac9650e20c265e1bbb9e1 (patch)
treeb206a9899dc98587d9110d99eb826b52e18082b7
parent7a989b9fa0c52c6dd0454e211c628c7f719f83b2 (diff)
template: add support for eachdo page.xyz
-rw-r--r--include/engine.h6
-rw-r--r--src/config.c1
-rw-r--r--src/engine/eachdo.c34
-rw-r--r--src/engine/engine.c11
-rw-r--r--src/template.c16
5 files changed, 60 insertions, 8 deletions
diff --git a/include/engine.h b/include/engine.h
index d7e05e0..ab08e7a 100644
--- a/include/engine.h
+++ b/include/engine.h
@@ -50,6 +50,12 @@ void handle_contentfor(char **buffer,
key_match_t *match,
directive_t *directive,
list_t *content_headers);
+
+/* EACHDO */
void handle_eachdo(char **buffer, key_match_t *match, directive_t *directive);
+void handle_page_source(list_t *atoms,
+ eachdo_operands_t *operands,
+ list_t *directives,
+ config_t *config);
#endif
diff --git a/src/config.c b/src/config.c
index 88b320e..50cbb6e 100644
--- a/src/config.c
+++ b/src/config.c
@@ -124,6 +124,7 @@ config_parse(char *content)
config->values = values;
config->array_values = array_values;
config->nested_config_values = nested_config_values;
+
return config;
}
diff --git a/src/engine/eachdo.c b/src/engine/eachdo.c
index d503256..0aba13e 100644
--- a/src/engine/eachdo.c
+++ b/src/engine/eachdo.c
@@ -153,10 +153,33 @@ handle_file_source(list_t *atoms,
list_delete(files);
}
+void
+handle_page_source(list_t *atoms,
+ eachdo_operands_t *operands,
+ list_t *directives,
+ config_t *config)
+{
+ list_t *nested_blocks
+ = unwrap(list_find_corresponding_value_from_ptr_wrapper(
+ config->keys, config->nested_config_values, trim(operands->key)));
+
+ if (nested_blocks == NULL) {
+ printf("Could not find nested block %s\n", trim(operands->key));
+ return;
+ }
+
+ for (size_t i = 0; i < nested_blocks->size; i++) {
+ config_t *config = unwrap(list_get(nested_blocks, i));
+
+ write_eachdo_iteration(
+ atoms, directives, config->keys, config->values, 0);
+ }
+}
+
/*
- * Handles EACHDO calls. Given a pointer to the buffer, it replaces the EACHDO
- * call along with its content block and ENDEACHDO call with the fetched
- * content.
+ * Handles EACHDO calls. Given a pointer to the buffer, it replaces the
+ * EACHDO call along with its content block and ENDEACHDO call with the
+ * fetched content.
*
* buffer: Pointer to the buffer that is modified
* match: Pointer to the key match
@@ -177,11 +200,8 @@ handle_eachdo(char **buffer, key_match_t *match, directive_t *directive)
if (!strcmp(operands->source, "resources"))
handle_file_source(atoms, operands, directives);
- else {
+ else
printf("Unknown source: %s\n", operands->source);
- /* TODO: handle this gracefully */
- return;
- }
/* Sort atoms by priority */
qsort(atoms->elements, atoms->size, sizeof(atom_t), comp);
diff --git a/src/engine/engine.c b/src/engine/engine.c
index 0b41c42..7edac19 100644
--- a/src/engine/engine.c
+++ b/src/engine/engine.c
@@ -101,9 +101,18 @@ engine_ingest(char **buffer)
handle_contentfor(
buffer, match, directive, engine->content_headers);
break;
- case EACHDO:
+ case EACHDO: {
+ eachdo_operands_t *operands = directive->operands;
+ /* TODO: Don't handle page source only if a template is currently
+ * being parsed */
+ if (!strcmp(operands->source, "page")) {
+ skip++;
+ break;
+ }
+
handle_eachdo(buffer, match, directive);
break;
+ }
case PUTPAGE:
/* TODO: handle */
diff --git a/src/template.c b/src/template.c
index a522475..f892eb7 100644
--- a/src/template.c
+++ b/src/template.c
@@ -197,6 +197,22 @@ template_write(engine_t *engine, FILE *f, void *doc, bool is_markdown)
break;
}
+ case EACHDO: {
+ eachdo_operands_t *operands = directive->operands;
+ if (!strcmp(operands->source, "page")) {
+ list_t *atoms = list_create(sizeof(ptr_wrapper_t));
+ list_t *directives = lex(operands->content);
+
+ handle_page_source(
+ atoms, operands, directives, engine->config);
+
+ for (size_t i = 0; i < atoms->size; i++) {
+ atom_t *atom = list_get(atoms, i);
+ fprintf(f, "%s", atom->content);
+ }
+ }
+ }
+
/* TODO: Handle this gracefully */
default:
break;