diff options
author | Palmer Dabbelt <palmer@dabbelt.com> | 2015-10-26 22:27:19 -0700 |
---|---|---|
committer | Palmer Dabbelt <palmer@dabbelt.com> | 2015-10-26 22:27:19 -0700 |
commit | a5128b3c739a79d2bf57de25023603387dcf9ce2 (patch) | |
tree | aec8bb454af64e6f17d6ca7b8ce0e68702d940ea /src/renderer.c | |
parent | 9293ef61f278ca339f62da5173fc68f9b6d5b482 (diff) | |
download | blogc-a5128b3c739a79d2bf57de25023603387dcf9ce2.tar.gz blogc-a5128b3c739a79d2bf57de25023603387dcf9ce2.tar.bz2 blogc-a5128b3c739a79d2bf57de25023603387dcf9ce2.zip |
Allow {% if VALUE1 == VALUE2 %}, with two defines
I was surprised to see that blogc doesn't support testing for equality
between two defined values, it just supports comparison between a
variable and a constant string. I want to be able to compare two
variables so I can build the same source with different "-D" argument on
the command line to produce different outputs.
This patch adds support for this pattern. I changed the parser to
include the '"' at the beginning and end of a string, which allows the
renderer to determine if the user passed in a string or a variable name.
This is a bit hacky and causes some of the tests to fail -- these tests
look at the string values coming out of the parser. I updated the tests
to match the new behavior.
Diffstat (limited to 'src/renderer.c')
-rw-r--r-- | src/renderer.c | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/renderer.c b/src/renderer.c index c5b3e9f..167145d 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -215,8 +215,24 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing } evaluate = false; if (stmt->op != 0) { - if (defined != NULL && stmt->value2 != NULL) { - cmp = strcmp(defined, stmt->value2); + // 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 (defined != NULL && defined2 != NULL) { + cmp = strcmp(defined, defined2); if (cmp != 0 && stmt->op & BLOGC_TEMPLATE_OP_NEQ) evaluate = true; else if (cmp == 0 && stmt->op & BLOGC_TEMPLATE_OP_EQ) @@ -226,6 +242,8 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing else if (cmp > 0 && stmt->op & BLOGC_TEMPLATE_OP_GT) evaluate = true; } + + free(defined2); } else { if (if_not && defined == NULL) |