From 59e1df233e351a91240c02ac55e8d97bda2f0e43 Mon Sep 17 00:00:00 2001
From: "Rafael G. Martins"
Date: Thu, 3 Aug 2017 02:44:36 +0200
Subject: make: added 'html_order' and 'atom_order' settings
these settings are used to change the order of the posts on the listings.
by default, users are supposed to list their posts from older to newer in
the blogcfile, and blogc-make will list them on descending order, both for
html listings and atom listings.
---
src/blogc-make/rules.c | 20 ++++++
src/blogc-make/settings.c | 2 +
tests/blogc-make/check_blogc_make.sh.in | 116 +++++++++++++++++++++++++++++++-
tests/blogc-make/check_settings.c | 12 +++-
4 files changed, 145 insertions(+), 5 deletions(-)
diff --git a/src/blogc-make/rules.c b/src/blogc-make/rules.c
index dd59d51..f27ef91 100644
--- a/src/blogc-make/rules.c
+++ b/src/blogc-make/rules.c
@@ -21,6 +21,20 @@
#include "rules.h"
+static void
+posts_ordering(bm_ctx_t *ctx, bc_trie_t *variables, const char *variable)
+{
+ if (ctx == NULL || ctx->settings == NULL || ctx->settings->settings == NULL)
+ return; // something is wrong, let's not add any variable
+
+ const char *value = bc_trie_lookup(ctx->settings->settings, variable);
+ if (value != NULL && ((0 == strcmp(value, "ASC")) || (0 == strcmp(value, "asc"))))
+ return; // user explicitly asked for ASC
+
+ bc_trie_insert(variables, "FILTER_REVERSE", bc_strdup("1"));
+}
+
+
// INDEX RULE
static bc_slist_t*
@@ -55,6 +69,7 @@ index_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args)
bc_trie_insert(variables, "FILTER_PER_PAGE",
bc_strdup(bc_trie_lookup(ctx->settings->settings, "posts_per_page")));
bc_trie_insert(variables, "FILTER_PAGE", bc_strdup("1"));
+ posts_ordering(ctx, variables, "html_order");
bc_trie_insert(variables, "DATE_FORMAT",
bc_strdup(bc_trie_lookup(ctx->settings->settings, "date_format")));
bc_trie_insert(variables, "MAKE_RULE", bc_strdup("index"));
@@ -112,6 +127,7 @@ atom_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args)
bc_strdup(bc_trie_lookup(ctx->settings->settings,
"atom_posts_per_page")));
bc_trie_insert(variables, "FILTER_PAGE", bc_strdup("1"));
+ posts_ordering(ctx, variables, "atom_order");
bc_trie_insert(variables, "DATE_FORMAT", bc_strdup("%Y-%m-%dT%H:%M:%SZ"));
bc_trie_insert(variables, "MAKE_RULE", bc_strdup("atom"));
bc_trie_insert(variables, "MAKE_TYPE", bc_strdup("atom"));
@@ -171,6 +187,7 @@ atom_tags_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args)
bc_strdup(bc_trie_lookup(ctx->settings->settings,
"atom_posts_per_page")));
bc_trie_insert(variables, "FILTER_PAGE", bc_strdup("1"));
+ posts_ordering(ctx, variables, "atom_order");
bc_trie_insert(variables, "DATE_FORMAT", bc_strdup("%Y-%m-%dT%H:%M:%SZ"));
bc_trie_insert(variables, "MAKE_RULE", bc_strdup("atom_tags"));
bc_trie_insert(variables, "MAKE_TYPE", bc_strdup("atom"));
@@ -240,6 +257,7 @@ pagination_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args)
bc_trie_t *variables = bc_trie_new(free);
bc_trie_insert(variables, "FILTER_PER_PAGE",
bc_strdup(bc_trie_lookup(ctx->settings->settings, "posts_per_page")));
+ posts_ordering(ctx, variables, "html_order");
bc_trie_insert(variables, "DATE_FORMAT",
bc_strdup(bc_trie_lookup(ctx->settings->settings, "date_format")));
bc_trie_insert(variables, "MAKE_RULE", bc_strdup("pagination"));
@@ -301,6 +319,7 @@ posts_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args)
bc_trie_insert(variables, "IS_POST", bc_strdup("1"));
bc_trie_insert(variables, "DATE_FORMAT",
bc_strdup(bc_trie_lookup(ctx->settings->settings, "date_format")));
+ posts_ordering(ctx, variables, "html_order");
bc_trie_insert(variables, "MAKE_RULE", bc_strdup("posts"));
bc_trie_insert(variables, "MAKE_TYPE", bc_strdup("post"));
@@ -363,6 +382,7 @@ tags_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args)
bc_strdup(bc_trie_lookup(ctx->settings->settings,
"atom_posts_per_page")));
bc_trie_insert(variables, "FILTER_PAGE", bc_strdup("1"));
+ posts_ordering(ctx, variables, "html_order");
bc_trie_insert(variables, "DATE_FORMAT",
bc_strdup(bc_trie_lookup(ctx->settings->settings, "date_format")));
bc_trie_insert(variables, "MAKE_RULE", bc_strdup("tags"));
diff --git a/src/blogc-make/settings.c b/src/blogc-make/settings.c
index c80feaa..9e24d41 100644
--- a/src/blogc-make/settings.c
+++ b/src/blogc-make/settings.c
@@ -37,10 +37,12 @@ static const struct default_settings_map {
{"index_prefix", NULL},
{"post_prefix", "post"},
{"tag_prefix", "tag"},
+ {"html_order", "DESC"},
// atom
{"atom_prefix", "atom"},
{"atom_ext", ".xml"},
+ {"atom_order", "DESC"},
// generic
{"date_format", "%b %d, %Y, %I:%M %p GMT"},
diff --git a/tests/blogc-make/check_blogc_make.sh.in b/tests/blogc-make/check_blogc_make.sh.in
index beb1f6c..d27c3c0 100755
--- a/tests/blogc-make/check_blogc_make.sh.in
+++ b/tests/blogc-make/check_blogc_make.sh.in
@@ -77,6 +77,116 @@ rm "${TEMP}/output.txt"
cat > "${TEMP}/expected-index.html" < "${TEMP}/expected-atom.xml" <
+
+ Lol's Website
+ /atom.xml
+ 2016-09-01T00:00:00Z
+
+
+
+ Lol
+ author@example.com
+
+ WAT?!
+
+
+ Bar
+ /post/bar/
+ 2016-09-01T00:00:00Z
+ 2016-09-01T00:00:00Z
+
+
+ Lol
+ author@example.com
+
+ This is bar.
+]]>
+
+
+
+ Foo
+ /post/foo/
+ 2016-10-01T00:00:00Z
+ 2016-10-01T00:00:00Z
+
+
+ Lol
+ author@example.com
+
+ This is foo.
+]]>
+
+
+
+EOF
+diff -uN "${TEMP}/proj/_build/atom.xml" "${TEMP}/expected-atom.xml"
+
+cat > "${TEMP}/expected-post-foo.html" <This is foo.
+
+
+EOF
+diff -uN "${TEMP}/proj/_build/post/foo/index.html" "${TEMP}/expected-post-foo.html"
+
+cat > "${TEMP}/expected-post-bar.html" <This is bar.
+
+
+EOF
+diff -uN "${TEMP}/proj/_build/post/bar/index.html" "${TEMP}/expected-post-bar.html"
+
+rm -rf "${TEMP}/proj/_build"
+
+
+### default settings with some posts, order asc
+
+cat > "${TEMP}/proj/blogcfile" <&1 | tee "${TEMP}/output.txt"
+grep "_build/index\\.html" "${TEMP}/output.txt"
+grep "_build/atom\\.xml" "${TEMP}/output.txt"
+grep "_build/page/1/index\\.html" "${TEMP}/output.txt"
+grep "_build/post/foo/index\\.html" "${TEMP}/output.txt"
+grep "_build/post/bar/index\\.html" "${TEMP}/output.txt"
+
+rm "${TEMP}/output.txt"
+
+cat > "${TEMP}/expected-index.html" < "${TEMP}/proj/content/post/baz.txt" < "${TEMP}/proj/content/page1.txt" <global, "SITE_TITLE"), "Fuuuuuuuuu");
assert_string_equal(bc_trie_lookup(s->global, "SITE_TAGLINE"), "My cool tagline");
assert_string_equal(bc_trie_lookup(s->global, "BASE_DOMAIN"), "http://example.com");
- assert_int_equal(bc_trie_size(s->settings), 13);
+ assert_int_equal(bc_trie_size(s->settings), 15);
assert_string_equal(bc_trie_lookup(s->settings, "source_ext"), ".txt");
assert_string_equal(bc_trie_lookup(s->settings, "html_ext"), "/index.html");
assert_string_equal(bc_trie_lookup(s->settings, "content_dir"), "guda");
@@ -141,6 +141,8 @@ test_settings2(void **state)
assert_string_equal(bc_trie_lookup(s->settings, "pagination_prefix"), "page");
assert_string_equal(bc_trie_lookup(s->settings, "post_prefix"), "post");
assert_string_equal(bc_trie_lookup(s->settings, "tag_prefix"), "tag");
+ assert_string_equal(bc_trie_lookup(s->settings, "html_order"), "DESC");
+ assert_string_equal(bc_trie_lookup(s->settings, "atom_order"), "DESC");
assert_non_null(s->posts);
assert_string_equal(s->posts[0], "aaaa");
assert_string_equal(s->posts[1], "bbbb");
@@ -213,7 +215,7 @@ test_settings_env2(void **state)
assert_string_equal(bc_trie_lookup(s->global, "SITE_TITLE"), "Fuuuuuuuuu");
assert_string_equal(bc_trie_lookup(s->global, "SITE_TAGLINE"), "My cool tagline");
assert_string_equal(bc_trie_lookup(s->global, "BASE_DOMAIN"), "http://example.com");
- assert_int_equal(bc_trie_size(s->settings), 13);
+ assert_int_equal(bc_trie_size(s->settings), 15);
assert_string_equal(bc_trie_lookup(s->settings, "source_ext"), ".txt");
assert_string_equal(bc_trie_lookup(s->settings, "html_ext"), "/index.html");
assert_string_equal(bc_trie_lookup(s->settings, "content_dir"), "guda");
@@ -228,6 +230,8 @@ test_settings_env2(void **state)
assert_string_equal(bc_trie_lookup(s->settings, "pagination_prefix"), "page");
assert_string_equal(bc_trie_lookup(s->settings, "post_prefix"), "post");
assert_string_equal(bc_trie_lookup(s->settings, "tag_prefix"), "tag");
+ assert_string_equal(bc_trie_lookup(s->settings, "html_order"), "DESC");
+ assert_string_equal(bc_trie_lookup(s->settings, "atom_order"), "DESC");
assert_non_null(s->posts);
assert_string_equal(s->posts[0], "aaaa");
assert_string_equal(s->posts[1], "bbbb");
@@ -300,7 +304,7 @@ test_settings_copy_files(void **state)
assert_string_equal(bc_trie_lookup(s->global, "SITE_TITLE"), "Fuuuuuuuuu");
assert_string_equal(bc_trie_lookup(s->global, "SITE_TAGLINE"), "My cool tagline");
assert_string_equal(bc_trie_lookup(s->global, "BASE_DOMAIN"), "http://example.com");
- assert_int_equal(bc_trie_size(s->settings), 13);
+ assert_int_equal(bc_trie_size(s->settings), 15);
assert_string_equal(bc_trie_lookup(s->settings, "source_ext"), ".txt");
assert_string_equal(bc_trie_lookup(s->settings, "html_ext"), "/index.html");
assert_string_equal(bc_trie_lookup(s->settings, "content_dir"), "guda");
@@ -315,6 +319,8 @@ test_settings_copy_files(void **state)
assert_string_equal(bc_trie_lookup(s->settings, "pagination_prefix"), "page");
assert_string_equal(bc_trie_lookup(s->settings, "post_prefix"), "post");
assert_string_equal(bc_trie_lookup(s->settings, "tag_prefix"), "tag");
+ assert_string_equal(bc_trie_lookup(s->settings, "html_order"), "DESC");
+ assert_string_equal(bc_trie_lookup(s->settings, "atom_order"), "DESC");
assert_non_null(s->posts);
assert_string_equal(s->posts[0], "aaaa");
assert_string_equal(s->posts[1], "bbbb");
--
cgit v1.2.3-18-g5258