aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-18 15:14:36 +0100
committerDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-18 15:14:36 +0100
commit0f083050e18858c8142efcab1f40661825ade64a (patch)
tree929458dd6a7b372283222bfa1f9e247ff42e4fa7
parenta13b511a582e0414476b810d2a0096dae088ee78 (diff)
downloadunja-0f083050e18858c8142efcab1f40661825ade64a.tar.gz
unja-0f083050e18858c8142efcab1f40661825ade64a.zip
add whitespace control for statements
-rw-r--r--src/template.c49
-rw-r--r--tests/data/01/base.tmpl8
-rw-r--r--tests/test_template.c2
3 files changed, 29 insertions, 30 deletions
diff --git a/src/template.c b/src/template.c
index cb2ad7b..4a893b8 100644
--- a/src/template.c
+++ b/src/template.c
@@ -376,35 +376,44 @@ int eval(struct buffer *buf, mpc_ast_t* t, struct context *ctx) {
static int trim_whitespace = 0;
// maybe eat whitespace going backward
- if (t->children_num > 0 && strstr(t->children[0]->contents, "{{-")) {
+ if (t->children_num > 0 && strstr(t->children[0]->contents, "-")) {
buf->string = trim_trailing_whitespace(buf->string);
}
+ if (strstr(t->tag, "content|text")) {
+ char *str = t->contents;
+ if (trim_whitespace) {
+ str = trim_leading_whitespace(str);
+ trim_whitespace = 0;
+ }
+
+ buffer_reserve(buf, strlen(str));
+ strcat(buf->string, str);
+ return 0;
+ }
+
if (strstr(t->tag, "content|statement|block")) {
+ trim_whitespace = strstr(t->children[3]->contents, "-") ? 1 : 0;
char *block_name = t->children[2]->contents;
// find block in "lowest" template
struct template *templ = ctx->current_template;
mpc_ast_t *block = hashmap_get(templ->blocks, block_name);
if (block) {
- return eval(buf, block->children[4], ctx);
+ eval(buf, block->children[4], ctx);
+ } else {
+ /* TODO: Keep looking for block in parent templates */
+ // just render this block if it wasn't found in any of the lower templates
+ eval(buf, t->children[4], ctx);
}
- /* TODO: Keep looking for block in parent templates */
-
- // just render this block if it wasn't found in any of the lower templates
- return eval(buf, t->children[4], ctx);
+ trim_whitespace = strstr(t->children[7]->contents, "-") ? 1 : 0;
+ return 0;
}
// eval print statement
if (strstr(t->tag, "content|print")) {
-
-
- /* set flag for next eval() to trim leading whitespace from text */
- if (strstr(t->children[2]->contents, "-}}")) {
- trim_whitespace = 1;
- }
-
+ trim_whitespace = strstr(t->children[2]->contents, "-") ? 1 : 0;
mpc_ast_t *expr = t->children[1];
struct unja_object *obj = eval_expression(expr, ctx);
eval_object(buf, obj);
@@ -413,6 +422,7 @@ int eval(struct buffer *buf, mpc_ast_t* t, struct context *ctx) {
}
if (strstr(t->tag, "content|statement|for")) {
+ trim_whitespace = strstr(t->children[5]->contents, "-") ? 1 : 0;
char *tmp_key = t->children[2]->contents;
char *iterator_key = t->children[4]->contents;
struct vector *list = hashmap_resolve(ctx->vars, iterator_key);
@@ -424,6 +434,7 @@ int eval(struct buffer *buf, mpc_ast_t* t, struct context *ctx) {
}
if (strstr(t->tag, "content|statement|if")) {
+ trim_whitespace = strstr(t->children[3]->contents, "-") ? 1 : 0;
mpc_ast_t *expr = t->children[2];
struct unja_object *result = eval_expression(expr, ctx);
@@ -439,18 +450,6 @@ int eval(struct buffer *buf, mpc_ast_t* t, struct context *ctx) {
return 0;
}
- if (strstr(t->tag, "content|text")) {
- char *str = t->contents;
- if (trim_whitespace) {
- str = trim_leading_whitespace(str);
- trim_whitespace = 0;
- }
-
- buffer_reserve(buf, strlen(str));
- strcat(buf->string, str);
- return 0;
- }
-
for (int i=0; i < t->children_num; i++) {
eval(buf, t->children[i], ctx);
}
diff --git a/tests/data/01/base.tmpl b/tests/data/01/base.tmpl
index 6ca89cb..241c78c 100644
--- a/tests/data/01/base.tmpl
+++ b/tests/data/01/base.tmpl
@@ -1,9 +1,9 @@
Header
-{% block content %}
+{%- block content %}
Content
-{% endblock %}
+{%- endblock %}
-{% block footer %}
+{%- block footer %}
Footer
-{% endblock %} \ No newline at end of file
+{%- endblock -%} \ No newline at end of file
diff --git a/tests/test_template.c b/tests/test_template.c
index 92f179c..244e788 100644
--- a/tests/test_template.c
+++ b/tests/test_template.c
@@ -218,7 +218,7 @@ TEST(buffer_alloc) {
TEST(inheritance) {
struct env *env = env_new("./tests/data/01/");
char *output = template(env, "child.tmpl", NULL);
- assert_str(output, "Header\n\n\nChild content\n\n\n\nFooter\n");
+ assert_str(output, "Header\nChild content\nFooter\n");
free(output);
env_free(env);
}