diff options
-rw-r--r-- | src/renderer.c | 114 | ||||
-rw-r--r-- | src/renderer.h | 1 | ||||
-rw-r--r-- | tests/check_renderer.c | 54 |
3 files changed, 110 insertions, 59 deletions
diff --git a/src/renderer.c b/src/renderer.c index 167145d..3061c43 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -10,6 +10,7 @@ #include <config.h> #endif /* HAVE_CONFIG_H */ +#include <stdbool.h> #include <stdio.h> #include <string.h> #include "utils/utils.h" @@ -57,6 +58,37 @@ 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 *var = NULL; + bool must_format = false; + if (b_str_ends_with(name, "_FORMATTED")) { + var = b_strndup(name, strlen(name) - 10); + must_format = true; + } + if (var == NULL) + var = b_strdup(name); + + const char *value = blogc_get_variable(var, global, local); + free(var); + + if (value == NULL) + return NULL; + + char *rv = NULL; + if (must_format) { + if (b_str_starts_with(name, "DATE_")) { + rv = blogc_format_date(value, global, local); + } + } + + if (rv == NULL) + return b_strdup(value); + return rv; +} + + +char* blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing) { if (tmpl == NULL) @@ -68,9 +100,7 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing b_string_t *str = b_string_new(); b_trie_t *tmp_source = NULL; - const char *config_value = NULL; - const char *config_var = NULL; - char *config_value2 = NULL; + char *config_value = NULL; char *defined = NULL; unsigned int if_count = 0; @@ -144,30 +174,13 @@ 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) { - config_var = NULL; - if (0 == strcmp(stmt->value, "DATE_FORMATTED")) - config_var = "DATE"; - else if (0 == strcmp(stmt->value, "DATE_FIRST_FORMATTED")) - config_var = "DATE_FIRST"; - else if (0 == strcmp(stmt->value, "DATE_LAST_FORMATTED")) - config_var = "DATE_LAST"; - if (config_var != NULL) { - config_value2 = blogc_format_date( - blogc_get_variable(config_var, config, - inside_block ? tmp_source : NULL), - config, inside_block ? tmp_source : NULL); - if (config_value2 != NULL) { - b_string_append(str, config_value2); - free(config_value2); - config_value2 = NULL; - break; - } - } - else { - config_value = blogc_get_variable(stmt->value, config, - inside_block ? tmp_source : NULL); - if (config_value != NULL) - b_string_append(str, config_value); + config_value = blogc_format_variable(stmt->value, + config, inside_block ? tmp_source : NULL); + if (config_value != NULL) { + b_string_append(str, config_value); + free(config_value); + config_value = NULL; + break; } } break; @@ -191,44 +204,27 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing case BLOGC_TEMPLATE_IF_STMT: case BLOGC_TEMPLATE_IFDEF_STMT: defined = NULL; - if (stmt->value != NULL) { - config_var = NULL; - if (0 == strcmp(stmt->value, "DATE_FORMATTED")) - config_var = "DATE"; - else if (0 == strcmp(stmt->value, "DATE_FIRST_FORMATTED")) - config_var = "DATE_FIRST"; - else if (0 == strcmp(stmt->value, "DATE_LAST_FORMATTED")) - config_var = "DATE_LAST"; - if (config_var != NULL) { - config_value2 = blogc_format_date( - blogc_get_variable(config_var, config, - inside_block ? tmp_source : NULL), - config, inside_block ? tmp_source : NULL); - if (config_value2 != NULL) { - defined = config_value2; - config_value2 = NULL; - } - } - else - defined = b_strdup(blogc_get_variable(stmt->value, - config, inside_block ? tmp_source : NULL)); - } + if (stmt->value != NULL) + defined = blogc_format_variable(stmt->value, config, + inside_block ? tmp_source : NULL); evaluate = false; if (stmt->op != 0) { // Strings that start with a '"' are actually strings, the // others are meant to be looked up as a second variable // check. char *defined2 = NULL; - if (stmt->value2[0] != '"') { - defined2 = - b_strdup(blogc_get_variable(stmt->value2, - config, - inside_block ? tmp_source - : NULL) - ); - } else { - defined2 = b_strndup(stmt->value2 + 1, - strlen(stmt->value2) - 2); + if (stmt->value2 != NULL) { + if ((strlen(stmt->value2) >= 2) && + (stmt->value2[0] == '"') && + (stmt->value2[strlen(stmt->value2) - 1] == '"')) + { + defined2 = b_strndup(stmt->value2 + 1, + strlen(stmt->value2) - 2); + } + else { + defined2 = blogc_format_variable(stmt->value2, + config, inside_block ? tmp_source : NULL); + } } if (defined != NULL && defined2 != NULL) { diff --git a/src/renderer.h b/src/renderer.h index ebe0aee..e5cff6e 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -14,6 +14,7 @@ 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_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing); diff --git a/tests/check_renderer.c b/tests/check_renderer.c index fbe38de..cb8f8f1 100644 --- a/tests/check_renderer.c +++ b/tests/check_renderer.c @@ -28,6 +28,7 @@ create_sources(unsigned int count) const char *s[] = { "BOLA: asd\n" "GUDA: zxc\n" + "GUDA2: zxc\n" "DATE: 2015-01-02 03:04:05\n" "DATE_FORMAT: %R\n" "-----\n" @@ -68,6 +69,7 @@ test_render_entry(void **state) "{% ifdef CHUNDA %}{{ CHUNDA }}{% endif %}\n" "{% endblock %}\n" "{% block listing %}lol{% endblock %}\n" + "{% if GUDA == GUDA2 %}gudabola{% endif %}\n" "{% if GUDA == \"zxc\" %}LOL{% endif %}\n" "{% if GUDA != \"bola\" %}HEHE{% endif %}\n" "{% if GUDA < \"zxd\" %}LOL2{% endif %}\n" @@ -90,6 +92,7 @@ test_render_entry(void **state) "\n" "\n" "\n" + "gudabola\n" "LOL\n" "HEHE\n" "LOL2\n" @@ -302,6 +305,7 @@ test_render_if_eq(void **state) { const char *str = "{% block entry %}\n" + "{% if GUDA == GUDA2 %}gudabola{% endif %}\n" "{% if GUDA == \"zxc\" %}guda\n" "{% ifdef BOLA %}bola\n" "{% if GUDA > \"zxc\" %}asd\n" @@ -318,6 +322,7 @@ test_render_if_eq(void **state) char *out = blogc_render(l, s, NULL, false); assert_string_equal(out, "\n" + "gudabola\n" "guda\n" "bola\n" "\n" @@ -335,6 +340,7 @@ test_render_if_neq(void **state) { const char *str = "{% block entry %}\n" + "{% if GUDA != BOLA %}gudabola{% endif %}\n" "{% if GUDA != \"zxa\" %}guda\n" "{% ifdef BOLA %}bola\n" "{% if GUDA > \"zxc\" %}asd\n" @@ -351,6 +357,7 @@ test_render_if_neq(void **state) char *out = blogc_render(l, s, NULL, false); assert_string_equal(out, "\n" + "gudabola\n" "guda\n" "bola\n" "\n" @@ -368,6 +375,7 @@ test_render_if_lt(void **state) { const char *str = "{% block entry %}\n" + "{% if BOLA < GUDA %}gudabola{% endif %}\n" "{% if GUDA < \"zxe\" %}guda\n" "{% ifdef BOLA %}bola\n" "{% if GUDA > \"zxc\" %}asd\n" @@ -384,6 +392,7 @@ test_render_if_lt(void **state) char *out = blogc_render(l, s, NULL, false); assert_string_equal(out, "\n" + "gudabola\n" "guda\n" "bola\n" "\n" @@ -401,6 +410,7 @@ test_render_if_gt(void **state) { const char *str = "{% block entry %}\n" + "{% if GUDA > BOLA %}gudabola{% endif %}\n" "{% if GUDA > \"zxa\" %}guda\n" "{% ifdef BOLA %}bola\n" "{% if GUDA > \"zxc\" %}asd\n" @@ -417,6 +427,7 @@ test_render_if_gt(void **state) char *out = blogc_render(l, s, NULL, false); assert_string_equal(out, "\n" + "gudabola\n" "guda\n" "bola\n" "\n" @@ -434,6 +445,7 @@ test_render_if_lt_eq(void **state) { const char *str = "{% block entry %}\n" + "{% if BOLA <= GUDA %}gudabola{% endif %}\n" "{% if GUDA <= \"zxc\" %}guda\n" "{% if GUDA <= \"zxe\" %}guda2\n" "{% ifdef BOLA %}bola\n" @@ -452,6 +464,7 @@ test_render_if_lt_eq(void **state) char *out = blogc_render(l, s, NULL, false); assert_string_equal(out, "\n" + "gudabola\n" "guda\n" "guda2\n" "bola\n" @@ -471,6 +484,7 @@ test_render_if_gt_eq(void **state) { const char *str = "{% block entry %}\n" + "{% if GUDA >= BOLA %}gudabola{% endif %}\n" "{% if GUDA >= \"zxc\" %}guda\n" "{% if GUDA >= \"zxa\" %}guda2\n" "{% ifdef BOLA %}bola\n" @@ -489,6 +503,7 @@ test_render_if_gt_eq(void **state) char *out = blogc_render(l, s, NULL, false); assert_string_equal(out, "\n" + "gudabola\n" "guda\n" "guda2\n" "bola\n" @@ -708,6 +723,43 @@ test_format_date_without_date(void **state) } +static void +test_format_variable(void **state) +{ + b_trie_t *g = b_trie_new(free); + b_trie_insert(g, "NAME", b_strdup("bola")); + b_trie_insert(g, "TITLE", b_strdup("bola2")); + 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); + assert_string_equal(tmp, "chunda"); + free(tmp); + tmp = blogc_format_variable("TITLE", g, l); + assert_string_equal(tmp, "chunda2"); + free(tmp); + assert_null(blogc_format_variable("BOLA", g, l)); + b_trie_free(g); + b_trie_free(l); +} + + +static void +test_format_variable_with_date(void **state) +{ + b_trie_t *g = b_trie_new(free); + b_trie_insert(g, "DATE", b_strdup("2010-11-12 13:14:15")); + 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); + assert_string_equal(tmp, "14:15"); + free(tmp); + b_trie_free(g); + b_trie_free(l); +} + + int main(void) { @@ -736,6 +788,8 @@ main(void) unit_test(test_format_date_with_global_format), unit_test(test_format_date_without_format), unit_test(test_format_date_without_date), + unit_test(test_format_variable), + unit_test(test_format_variable_with_date), }; return run_tests(tests); } |