aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/loader.c20
-rw-r--r--src/loader.h3
-rw-r--r--src/main.c2
-rw-r--r--src/renderer.c36
-rw-r--r--src/renderer.h2
-rw-r--r--tests/check_renderer.c14
6 files changed, 57 insertions, 20 deletions
diff --git a/src/loader.c b/src/loader.c
index 485fb0d..87abd8e 100644
--- a/src/loader.c
+++ b/src/loader.c
@@ -124,10 +124,11 @@ blogc_source_parse_from_file(const char *f, blogc_error_t **err)
b_slist_t*
-blogc_source_parse_from_files(b_slist_t *l, blogc_error_t **err)
+blogc_source_parse_from_files(b_trie_t *conf, b_slist_t *l, blogc_error_t **err)
{
blogc_error_t *tmp_err = NULL;
b_slist_t *rv = NULL;
+ bool first = true;
for (b_slist_t *tmp = l; tmp != NULL; tmp = tmp->next) {
char *f = tmp->data;
@@ -142,6 +143,23 @@ blogc_source_parse_from_files(b_slist_t *l, blogc_error_t **err)
rv = NULL;
break;
}
+ if (first) {
+ const char *val = b_trie_lookup(s, "DATE");
+ if (val != NULL)
+ b_trie_insert(conf, "DATE_FIRST", b_strdup(val));
+ val = b_trie_lookup(s, "FILENAME");
+ if (val != NULL)
+ b_trie_insert(conf, "FILENAME_FIRST", b_strdup(val));
+ first = false;
+ }
+ if (tmp->next == NULL) { // last
+ const char *val = b_trie_lookup(s, "DATE");
+ if (val != NULL)
+ b_trie_insert(conf, "DATE_LAST", b_strdup(val));
+ val = b_trie_lookup(s, "FILENAME");
+ if (val != NULL)
+ b_trie_insert(conf, "FILENAME_LAST", b_strdup(val));
+ }
rv = b_slist_append(rv, s);
}
return rv;
diff --git a/src/loader.h b/src/loader.h
index 6407d0d..2b6b306 100644
--- a/src/loader.h
+++ b/src/loader.h
@@ -18,6 +18,7 @@ char* blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err
char* blogc_get_filename(const char *f);
b_slist_t* blogc_template_parse_from_file(const char *f, blogc_error_t **err);
b_trie_t* blogc_source_parse_from_file(const char *f, blogc_error_t **err);
-b_slist_t* blogc_source_parse_from_files(b_slist_t *l, blogc_error_t **err);
+b_slist_t* blogc_source_parse_from_files(b_trie_t *conf, b_slist_t *l,
+ blogc_error_t **err);
#endif /* _LOADER_H */
diff --git a/src/main.c b/src/main.c
index be18050..f3af371 100644
--- a/src/main.c
+++ b/src/main.c
@@ -208,7 +208,7 @@ main(int argc, char **argv)
goto cleanup2;
}
- b_slist_t *s = blogc_source_parse_from_files(sources, &err);
+ b_slist_t *s = blogc_source_parse_from_files(config, sources, &err);
if (err != NULL) {
blogc_error_print(err);
goto cleanup3;
diff --git a/src/renderer.c b/src/renderer.c
index ccd6c04..c73244b 100644
--- a/src/renderer.c
+++ b/src/renderer.c
@@ -39,9 +39,8 @@ blogc_get_variable(const char *name, b_trie_t *global, b_trie_t *local)
char*
-blogc_format_date(b_trie_t *global, b_trie_t *local)
+blogc_format_date(const char *date, b_trie_t *global, b_trie_t *local)
{
- const char *date = blogc_get_variable("DATE", global, local);
const char *date_format = blogc_get_variable("DATE_FORMAT", global, local);
if (date == NULL)
return NULL;
@@ -82,6 +81,7 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing
b_trie_t *tmp_source = NULL;
const char *config_value = NULL;
+ const char *config_var = NULL;
char *config_value2 = NULL;
unsigned int if_count = 0;
@@ -143,9 +143,18 @@ 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,
- inside_block ? tmp_source : 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);
@@ -181,8 +190,18 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing
case BLOGC_TEMPLATE_IF_STMT:
defined = false;
if (stmt->value != NULL) {
- if (0 == strcmp(stmt->value, "DATE_FORMATTED")) {
- config_value2 = blogc_format_date(config, tmp_source);
+ 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 = true;
free(config_value2);
@@ -190,7 +209,8 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing
}
}
else
- defined = blogc_get_variable(stmt->value, config, tmp_source) != NULL;
+ defined = blogc_get_variable(stmt->value, config,
+ inside_block ? tmp_source : NULL) != NULL;
}
if ((!if_not && !defined) || (if_not && defined)) {
if_skip = if_count;
diff --git a/src/renderer.h b/src/renderer.h
index e4ad4a1..b6bbfed 100644
--- a/src/renderer.h
+++ b/src/renderer.h
@@ -13,7 +13,7 @@
#include "utils/utils.h"
const char* blogc_get_variable(const char *name, b_trie_t *global, b_trie_t *local);
-char* blogc_format_date(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_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 3d8a205..4c77057 100644
--- a/tests/check_renderer.c
+++ b/tests/check_renderer.c
@@ -39,6 +39,7 @@ create_sources(unsigned int count)
"ahahahahahahahaha2",
"BOLA: asd3\n"
"GUDA: zxc3\n"
+ "DATE: 2013-01-02 03:04:05\n"
"-----\n"
"ahahahahahahahaha3",
};
@@ -122,7 +123,7 @@ test_render_listing(void **state)
"2014-02-03 04:05:06\n"
"bola: asd2\n"
"\n"
- "\n"
+ "2013-01-02 03:04:05\n"
"bola: asd3\n"
"\n");
blogc_template_free_stmts(l);
@@ -413,9 +414,8 @@ test_format_date(void **state)
b_trie_t *g = b_trie_new(free);
b_trie_insert(g, "DATE_FORMAT", b_strdup("%H -- %M"));
b_trie_t *l = b_trie_new(free);
- b_trie_insert(l, "DATE", b_strdup("2015-01-02 03:04:05"));
b_trie_insert(l, "DATE_FORMAT", b_strdup("%R"));
- char *date = blogc_format_date(g, l);
+ char *date = blogc_format_date("2015-01-02 03:04:05", g, l);
assert_string_equal(date, "03:04");
free(date);
b_trie_free(g);
@@ -429,8 +429,7 @@ test_format_date_with_global_format(void **state)
b_trie_t *g = b_trie_new(free);
b_trie_insert(g, "DATE_FORMAT", b_strdup("%H -- %M"));
b_trie_t *l = b_trie_new(free);
- b_trie_insert(l, "DATE", b_strdup("2015-01-02 03:04:05"));
- char *date = blogc_format_date(g, l);
+ char *date = blogc_format_date("2015-01-02 03:04:05", g, l);
assert_string_equal(date, "03 -- 04");
free(date);
b_trie_free(g);
@@ -443,8 +442,7 @@ test_format_date_without_format(void **state)
{
b_trie_t *g = b_trie_new(free);
b_trie_t *l = b_trie_new(free);
- b_trie_insert(l, "DATE", b_strdup("2015-01-02 03:04:05"));
- char *date = blogc_format_date(g, l);
+ char *date = blogc_format_date("2015-01-02 03:04:05", g, l);
assert_string_equal(date, "2015-01-02 03:04:05");
free(date);
b_trie_free(g);
@@ -457,7 +455,7 @@ test_format_date_without_date(void **state)
{
b_trie_t *g = b_trie_new(free);
b_trie_t *l = b_trie_new(free);
- char *date = blogc_format_date(g, l);
+ char *date = blogc_format_date(NULL, g, l);
assert_null(date);
free(date);
b_trie_free(g);