aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2016-01-08 02:36:17 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2016-01-08 02:36:17 +0100
commit56a5ebf7f0b0d86b7d6e3fd468d3415da312e69b (patch)
tree1405b183551c93baf575d1efda5e65f461d7c36b
parentfc0e0f4c2415b816daaca8d292aada532be19faa (diff)
downloadblogc-56a5ebf7f0b0d86b7d6e3fd468d3415da312e69b.tar.gz
blogc-56a5ebf7f0b0d86b7d6e3fd468d3415da312e69b.tar.bz2
blogc-56a5ebf7f0b0d86b7d6e3fd468d3415da312e69b.zip
renderer: handle FOREACH_ITEM as a normal variable
-rw-r--r--src/renderer.c22
-rw-r--r--src/renderer.h3
-rw-r--r--tests/check_renderer.c56
3 files changed, 67 insertions, 14 deletions
diff --git a/src/renderer.c b/src/renderer.c
index defbe10..2fb60b5 100644
--- a/src/renderer.c
+++ b/src/renderer.c
@@ -58,8 +58,16 @@ blogc_format_date(const char *date, b_trie_t *global, b_trie_t *local)
char*
-blogc_format_variable(const char *name, b_trie_t *global, b_trie_t *local)
+blogc_format_variable(const char *name, b_trie_t *global, b_trie_t *local,
+ b_slist_t *foreach_var)
{
+ if (0 == strcmp(name, "FOREACH_ITEM")) {
+ if (foreach_var != NULL && foreach_var->data != NULL) {
+ return b_strdup(foreach_var->data);
+ }
+ return NULL;
+ }
+
char *var = NULL;
bool must_format = false;
if (b_str_ends_with(name, "_FORMATTED")) {
@@ -200,13 +208,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, "FOREACH_ITEM")) { // foreach
- if (foreach_var != NULL && foreach_var->data != NULL)
- b_string_append(str, foreach_var->data);
- break;
- }
config_value = blogc_format_variable(stmt->value,
- config, inside_block ? tmp_source : NULL);
+ config, inside_block ? tmp_source : NULL, foreach_var);
if (config_value != NULL) {
b_string_append(str, config_value);
free(config_value);
@@ -237,7 +240,7 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing
defined = NULL;
if (stmt->value != NULL)
defined = blogc_format_variable(stmt->value, config,
- inside_block ? tmp_source : NULL);
+ inside_block ? tmp_source : NULL, foreach_var);
evaluate = false;
if (stmt->op != 0) {
// Strings that start with a '"' are actually strings, the
@@ -254,7 +257,8 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing
}
else {
defined2 = blogc_format_variable(stmt->value2,
- config, inside_block ? tmp_source : NULL);
+ config, inside_block ? tmp_source : NULL,
+ foreach_var);
}
}
diff --git a/src/renderer.h b/src/renderer.h
index de26e98..d99fea0 100644
--- a/src/renderer.h
+++ b/src/renderer.h
@@ -14,7 +14,8 @@
const char* blogc_get_variable(const char *name, b_trie_t *global, b_trie_t *local);
char* blogc_format_date(const char *date, b_trie_t *global, b_trie_t *local);
-char* blogc_format_variable(const char *name, b_trie_t *global, b_trie_t *local);
+char* blogc_format_variable(const char *name, b_trie_t *global, b_trie_t *local,
+ b_slist_t *foreach_var);
b_slist_t* blogc_split_list_variable(const char *name, b_trie_t *global,
b_trie_t *local);
char* blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config,
diff --git a/tests/check_renderer.c b/tests/check_renderer.c
index d641506..014cd84 100644
--- a/tests/check_renderer.c
+++ b/tests/check_renderer.c
@@ -557,6 +557,31 @@ test_render_foreach(void **state)
static void
+test_render_foreach_if(void **state)
+{
+ const char *str =
+ "{% block entry %}\n"
+ "{% foreach TAGS %} {% if FOREACH_ITEM == \"bar\" %}{{ FOREACH_ITEM }}"
+ "{% endif %} {% endforeach %}\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);
+ char *out = blogc_render(l, s, NULL, false);
+ assert_string_equal(out,
+ "\n"
+ " bar \n"
+ "\n");
+ blogc_template_free_stmts(l);
+ b_slist_free_full(s, (b_free_func_t) b_trie_free);
+ free(out);
+}
+
+
+static void
test_render_null(void **state)
{
assert_null(blogc_render(NULL, NULL, NULL, false));
@@ -770,13 +795,13 @@ test_format_variable(void **state)
b_trie_t *l = b_trie_new(free);
b_trie_insert(l, "NAME", b_strdup("chunda"));
b_trie_insert(l, "TITLE", b_strdup("chunda2"));
- char *tmp = blogc_format_variable("NAME", g, l);
+ char *tmp = blogc_format_variable("NAME", g, l, NULL);
assert_string_equal(tmp, "chunda");
free(tmp);
- tmp = blogc_format_variable("TITLE", g, l);
+ tmp = blogc_format_variable("TITLE", g, l, NULL);
assert_string_equal(tmp, "chunda2");
free(tmp);
- assert_null(blogc_format_variable("BOLA", g, l));
+ assert_null(blogc_format_variable("BOLA", g, l, NULL));
b_trie_free(g);
b_trie_free(l);
}
@@ -790,7 +815,7 @@ test_format_variable_with_date(void **state)
b_trie_insert(g, "DATE_FORMAT", b_strdup("%R"));
b_trie_t *l = b_trie_new(free);
b_trie_insert(l, "DATE", b_strdup("2011-12-13 14:15:16"));
- char *tmp = blogc_format_variable("DATE_FORMATTED", g, l);
+ char *tmp = blogc_format_variable("DATE_FORMATTED", g, l, NULL);
assert_string_equal(tmp, "14:15");
free(tmp);
b_trie_free(g);
@@ -799,6 +824,26 @@ test_format_variable_with_date(void **state)
static void
+test_format_variable_foreach(void **state)
+{
+ b_slist_t *l = NULL;
+ l = b_slist_append(l, b_strdup("asd"));
+ l = b_slist_append(l, b_strdup("qwe"));
+ char *tmp = blogc_format_variable("FOREACH_ITEM", NULL, NULL, l->next);
+ assert_string_equal(tmp, "qwe");
+ free(tmp);
+ b_slist_free_full(l, free);
+}
+
+
+static void
+test_format_variable_foreach_empty(void **state)
+{
+ assert_null(blogc_format_variable("FOREACH_ITEM", NULL, NULL, NULL));
+}
+
+
+static void
test_split_list_variable(void **state)
{
b_trie_t *g = b_trie_new(free);
@@ -847,6 +892,7 @@ main(void)
unit_test(test_render_if_lt_eq),
unit_test(test_render_if_gt_eq),
unit_test(test_render_foreach),
+ unit_test(test_render_foreach_if),
unit_test(test_render_null),
unit_test(test_render_outside_block),
unit_test(test_render_prefer_local_variable),
@@ -860,6 +906,8 @@ main(void)
unit_test(test_format_date_without_date),
unit_test(test_format_variable),
unit_test(test_format_variable_with_date),
+ unit_test(test_format_variable_foreach),
+ unit_test(test_format_variable_foreach_empty),
unit_test(test_split_list_variable),
unit_test(test_split_list_variable_not_found),
};