From 803eb202e9d2d96fbb0de6f0514db2f0ab959d11 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Tue, 25 Jul 2017 19:46:14 +0200 Subject: loader: added FILTER_REVERSE --- man/blogc-pagination.7.ronn | 5 +++++ src/blogc/loader.c | 15 ++++++++++++++- tests/blogc/check_loader.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/man/blogc-pagination.7.ronn b/man/blogc-pagination.7.ronn index db40334..80bdb16 100644 --- a/man/blogc-pagination.7.ronn +++ b/man/blogc-pagination.7.ronn @@ -27,6 +27,11 @@ files passed as arguments to it: variables. The pagination filters will only act on the files with the provided tag, instead of filtering the whole file set. + * `FILTER_REVERSE`: + Any string, if defined, blogc(1) will list files in reverse order. This + is always the first filter applied to the files. All the other filters will + get the files already in the reverse order, and won't care about this. + ## TEMPLATE VARIABLES blogc(1) will export some global blogc-template(7) variables, that can be used diff --git a/src/blogc/loader.c b/src/blogc/loader.c index a9a660e..881661b 100644 --- a/src/blogc/loader.c +++ b/src/blogc/loader.c @@ -103,6 +103,17 @@ blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, bc_error_t **err) if (err == NULL || *err != NULL) return NULL; + bool reverse = bc_trie_lookup(conf, "FILTER_REVERSE"); + bc_slist_t* sources = NULL; + for (bc_slist_t *tmp = l; tmp != NULL; tmp = tmp->next) { + if (reverse) { + sources = bc_slist_prepend(sources, tmp->data); + } + else { + sources = bc_slist_append(sources, tmp->data); + } + } + bc_error_t *tmp_err = NULL; bc_slist_t *rv = NULL; unsigned int with_date = 0; @@ -135,7 +146,7 @@ blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, bc_error_t **err) unsigned int end = start + per_page; unsigned int counter = 0; - for (bc_slist_t *tmp = l; tmp != NULL; tmp = tmp->next) { + for (bc_slist_t *tmp = sources; tmp != NULL; tmp = tmp->next) { char *f = tmp->data; bc_trie_t *s = blogc_source_parse_from_file(f, &tmp_err); if (s == NULL) { @@ -182,6 +193,8 @@ blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, bc_error_t **err) rv = bc_slist_append(rv, s); } + bc_slist_free(sources); + if (with_date > 0 && with_date < bc_slist_length(rv)) { *err = bc_error_new_printf(BLOGC_ERROR_LOADER, "'DATE' variable provided for at least one source file, but not " diff --git a/tests/blogc/check_loader.c b/tests/blogc/check_loader.c index 461fac8..ec82af1 100644 --- a/tests/blogc/check_loader.c +++ b/tests/blogc/check_loader.c @@ -168,6 +168,52 @@ test_source_parse_from_files(void **state) } +static void +test_source_parse_from_files_filter_reverse(void **state) +{ + will_return(__wrap_bc_file_get_contents, "bola3.txt"); + will_return(__wrap_bc_file_get_contents, bc_strdup( + "ASD: 789\n" + "DATE: 2003-02-03 04:05:06\n" + "--------\n" + "bola")); + will_return(__wrap_bc_file_get_contents, "bola2.txt"); + will_return(__wrap_bc_file_get_contents, bc_strdup( + "ASD: 456\n" + "DATE: 2002-02-03 04:05:06\n" + "TAGS: bola, chunda\n" + "--------\n" + "bola")); + will_return(__wrap_bc_file_get_contents, "bola1.txt"); + will_return(__wrap_bc_file_get_contents, bc_strdup( + "ASD: 123\n" + "DATE: 2001-02-03 04:05:06\n" + "TAGS: chunda\n" + "--------\n" + "bola")); + bc_error_t *err = NULL; + bc_slist_t *s = NULL; + s = bc_slist_append(s, bc_strdup("bola1.txt")); + s = bc_slist_append(s, bc_strdup("bola2.txt")); + s = bc_slist_append(s, bc_strdup("bola3.txt")); + bc_trie_t *c = bc_trie_new(free); + bc_trie_insert(c, "FILTER_REVERSE", bc_strdup("")); + bc_slist_t *t = blogc_source_parse_from_files(c, s, &err); + assert_null(err); + assert_non_null(t); + assert_int_equal(bc_slist_length(t), 3); // it is enough, no need to look at the items + assert_int_equal(bc_trie_size(c), 5); + assert_string_equal(bc_trie_lookup(c, "FILENAME_FIRST"), "bola3"); + assert_string_equal(bc_trie_lookup(c, "FILENAME_LAST"), "bola1"); + assert_string_equal(bc_trie_lookup(c, "DATE_FIRST"), "2003-02-03 04:05:06"); + assert_string_equal(bc_trie_lookup(c, "DATE_LAST"), "2001-02-03 04:05:06"); + assert_string_equal(bc_trie_lookup(c, "FILTER_REVERSE"), ""); + bc_trie_free(c); + bc_slist_free_full(s, free); + bc_slist_free_full(t, (bc_free_func_t) bc_trie_free); +} + + static void test_source_parse_from_files_filter_by_tag(void **state) { @@ -744,6 +790,7 @@ main(void) unit_test(test_source_parse_from_file), unit_test(test_source_parse_from_file_null), unit_test(test_source_parse_from_files), + unit_test(test_source_parse_from_files_filter_reverse), unit_test(test_source_parse_from_files_filter_by_tag), unit_test(test_source_parse_from_files_filter_by_page), unit_test(test_source_parse_from_files_filter_by_page2), -- cgit v1.2.3-18-g5258