From 0a63da7081bd1227c5788000349cf7ac03f8e107 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Fri, 8 May 2015 21:41:53 -0300 Subject: renderer: respect local scope --- src/renderer.c | 9 +++++++-- tests/check_renderer.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/renderer.c b/src/renderer.c index ac269b8..ccd6c04 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -89,6 +89,7 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing bool if_not = false; bool defined = false; + bool inside_block = false; b_slist_t *tmp = tmpl; while (tmp != NULL) { @@ -102,6 +103,7 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing break; case BLOGC_TEMPLATE_BLOCK_STMT: + inside_block = true; if_count = 0; if (0 == strcmp("entry", stmt->value)) { if (listing) { @@ -142,7 +144,8 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing case BLOGC_TEMPLATE_VARIABLE_STMT: if (stmt->value != NULL) { if (0 == strcmp(stmt->value, "DATE_FORMATTED")) { - config_value2 = blogc_format_date(config, tmp_source); + config_value2 = blogc_format_date(config, + inside_block ? tmp_source : NULL); if (config_value2 != NULL) { b_string_append(str, config_value2); free(config_value2); @@ -151,7 +154,8 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing } } else { - config_value = blogc_get_variable(stmt->value, config, tmp_source); + config_value = blogc_get_variable(stmt->value, config, + inside_block ? tmp_source : NULL); if (config_value != NULL) b_string_append(str, config_value); } @@ -159,6 +163,7 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing break; case BLOGC_TEMPLATE_ENDBLOCK_STMT: + inside_block = false; if (listing_start != NULL && current_source != NULL) { current_source = current_source->next; if (current_source != NULL) { diff --git a/tests/check_renderer.c b/tests/check_renderer.c index 1d3c205..3d8a205 100644 --- a/tests/check_renderer.c +++ b/tests/check_renderer.c @@ -330,6 +330,38 @@ test_render_prefer_local_variable(void **state) } +static void +test_render_respect_variable_scope(void **state) +{ + const char *str = + "{{ LOL }}\n" + "{{ BOLA }}\n" + "{% block entry %}\n" + "{% if LOL %}{{ LOL }}{% endif %}\n" + "{% if BOLA %}{{ BOLA }}{% endif %}\n" + "{% endblock %}\n"; + blogc_error_t *err = NULL; + b_slist_t *l = blogc_template_parse(str, strlen(str), &err); + assert_non_null(l); + assert_null(err); + b_slist_t *s = create_sources(1); + assert_non_null(s); + b_trie_t *c = b_trie_new(free); + char *out = blogc_render(l, s, c, false); + assert_string_equal(out, + "\n" + "\n" + "\n" + "\n" + "asd\n" + "\n"); + b_trie_free(c); + blogc_template_free_stmts(l); + b_slist_free_full(s, (b_free_func_t) b_trie_free); + free(out); +} + + static void test_get_variable(void **state) { @@ -446,6 +478,7 @@ main(void) unit_test(test_render_outside_block), unit_test(test_render_null), unit_test(test_render_prefer_local_variable), + unit_test(test_render_respect_variable_scope), unit_test(test_get_variable), unit_test(test_get_variable_only_local), unit_test(test_get_variable_only_global), -- cgit v1.2.3-18-g5258