diff options
author | June McEnroe <june@causal.agency> | 2020-12-01 18:05:08 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-12-01 19:48:50 -0500 |
commit | f2aee721bedc0fbb96b3bf2d5911ab6a6ef8e69c (patch) | |
tree | 6d5e68645f343f488e602b09d5d8ead2e2c67bfa | |
parent | Remove unnecessary margin CSS (diff) | |
download | bubger-f2aee721bedc0fbb96b3bf2d5911ab6a6ef8e69c.tar.gz bubger-f2aee721bedc0fbb96b3bf2d5911ab6a6ef8e69c.zip |
Add conditionals to templates
Diffstat (limited to '')
-rw-r--r-- | template.c | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/template.c b/template.c index a47d9f5..e3adcd1 100644 --- a/template.c +++ b/template.c @@ -102,21 +102,29 @@ int escapeXML(FILE *file, const char *str) { return 0; } +static const char * +variableValue(const struct Variable vars[], const char *name, size_t len) { + for (const struct Variable *var = vars; var && var->name; ++var) { + if (strlen(var->name) != len) continue; + if (strncmp(var->name, name, len)) continue; + return var->value; + } + return NULL; +} + int templateRender( FILE *file, const char *template, const struct Variable vars[], EscapeFn *escape ) { if (!escape) escape = escapeNull; - for (bool subst = false; *template; subst ^= true) { + for (bool subst = false, write = true; *template; subst ^= true) { size_t len = strcspn(template, "[]"); - if (subst) { - const char *value = NULL; - for (const struct Variable *var = vars; var && var->name; ++var) { - if (strlen(var->name) != len) continue; - if (strncmp(var->name, template, len)) continue; - value = var->value; - break; - } + if (subst && template[0] == '+') { + write = variableValue(vars, &template[1], len - 1); + } else if (subst && template[0] == '-') { + write = true; + } else if (subst && write) { + const char *value = variableValue(vars, template, len); if (!value) { errx( EX_SOFTWARE, "no value for template variable %.*s", @@ -125,7 +133,7 @@ int templateRender( } int error = escape(file, value); if (error) return error; - } else if (len) { + } else if (len && write) { size_t n = fwrite(template, len, 1, file); if (!n) return -1; } |