aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2015-05-09 01:23:33 -0300
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2015-05-09 01:23:33 -0300
commitc266bd93852b156b1436e203bb0f691139274f3d (patch)
tree3f549cce5aeb4e6273981b131c3cc9ae0e4883cd /src
parent0a63da7081bd1227c5788000349cf7ac03f8e107 (diff)
downloadblogc-c266bd93852b156b1436e203bb0f691139274f3d.tar.gz
blogc-c266bd93852b156b1436e203bb0f691139274f3d.tar.bz2
blogc-c266bd93852b156b1436e203bb0f691139274f3d.zip
added global variables with date/filename on first and last post
Diffstat (limited to 'src')
-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
5 files changed, 51 insertions, 12 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);