From 7a01d4616cec1831ec025e62bc64b4ec5d6ed600 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sun, 10 Jun 2018 11:15:47 +0200 Subject: make: disable post listings if {atom_,}posts_per_page is 0 --- man/blogcfile.5.ronn | 6 ++--- src/blogc-make/rules.c | 29 +++++++++++++++++++- tests/blogc-make/check_blogc_make.sh.in | 47 +-------------------------------- 3 files changed, 32 insertions(+), 50 deletions(-) diff --git a/man/blogcfile.5.ronn b/man/blogcfile.5.ronn index 2836bad..61812a4 100644 --- a/man/blogcfile.5.ronn +++ b/man/blogcfile.5.ronn @@ -50,7 +50,7 @@ however these rules can be customized with the following settings, from the * `atom_posts_per_page` (default: `10`): Number of posts per page in the Atom feeds. If negative, all the posts are - included. If `0`, no posts are include. + included. If `0`, no Atom feeds are generated. * `atom_prefix` (default: `atom`): The prefix of the generated Atom feeds. It is relative to the output @@ -100,8 +100,8 @@ however these rules can be customized with the following settings, from the * `posts_per_page` (default: `10`): Number of posts per page in the pagination pages. If negative, all the posts - are included. If `0`, no posts are included. Also, if negative or `0`, the - `pagination` build rule is disabled. + are included. If `0`, no post listing pages are generated. Also, if negative or + `0`, the `pagination` build rule is disabled. * `source_ext` (default: `.txt`): The extension of the source files. diff --git a/src/blogc-make/rules.c b/src/blogc-make/rules.c index aea9c0f..8851313 100644 --- a/src/blogc-make/rules.c +++ b/src/blogc-make/rules.c @@ -53,6 +53,19 @@ posts_pagination(bm_ctx_t *ctx, bc_trie_t *variables, const char *variable) } +static bool +posts_pagination_enabled(bm_ctx_t *ctx, const char *variable) +{ + if (ctx == NULL || ctx->settings == NULL || ctx->settings->settings == NULL) + return false; + + long posts_per_page = strtol( + bc_trie_lookup(ctx->settings->settings, variable), + NULL, 10); // FIXME: improve + return posts_per_page != 0; +} + + // INDEX RULE static bc_slist_t* @@ -61,6 +74,9 @@ index_outputlist(bm_ctx_t *ctx) if (ctx == NULL || ctx->settings->posts == NULL) return NULL; + if (!posts_pagination_enabled(ctx, "posts_per_page")) + return NULL; + bc_slist_t *rv = NULL; const char *html_ext = bc_trie_lookup(ctx->settings->settings, "html_ext"); @@ -119,6 +135,9 @@ atom_outputlist(bm_ctx_t *ctx) if (ctx == NULL || ctx->settings->posts == NULL) return NULL; + if (!posts_pagination_enabled(ctx, "atom_posts_per_page")) + return NULL; + bc_slist_t *rv = NULL; const char *atom_prefix = bc_trie_lookup(ctx->settings->settings, "atom_prefix"); @@ -173,6 +192,9 @@ atom_tags_outputlist(bm_ctx_t *ctx) if (ctx == NULL || ctx->settings->posts == NULL || ctx->settings->tags == NULL) return NULL; + if (!posts_pagination_enabled(ctx, "atom_posts_per_page")) + return NULL; + bc_slist_t *rv = NULL; const char *atom_prefix = bc_trie_lookup(ctx->settings->settings, "atom_prefix"); @@ -234,13 +256,15 @@ pagination_outputlist(bm_ctx_t *ctx) if (ctx == NULL || ctx->settings->posts == NULL) return NULL; - long num_posts = bc_slist_length(ctx->posts_fctx); + // not using posts_pagination_enabled() here because we need to calculate + // posts per page here anyway, and the condition is different. long posts_per_page = strtol( bc_trie_lookup(ctx->settings->settings, "posts_per_page"), NULL, 10); // FIXME: improve if (posts_per_page <= 0) return NULL; + long num_posts = bc_slist_length(ctx->posts_fctx); size_t pages = ceilf(((float) num_posts) / posts_per_page); const char *pagination_prefix = bc_trie_lookup(ctx->settings->settings, @@ -374,6 +398,9 @@ tags_outputlist(bm_ctx_t *ctx) if (ctx == NULL || ctx->settings->posts == NULL || ctx->settings->tags == NULL) return NULL; + if (!posts_pagination_enabled(ctx, "posts_per_page")) + return NULL; + bc_slist_t *rv = NULL; const char *tag_prefix = bc_trie_lookup(ctx->settings->settings, "tag_prefix"); diff --git a/tests/blogc-make/check_blogc_make.sh.in b/tests/blogc-make/check_blogc_make.sh.in index fcf47a9..064c801 100755 --- a/tests/blogc-make/check_blogc_make.sh.in +++ b/tests/blogc-make/check_blogc_make.sh.in @@ -448,6 +448,7 @@ BASE_DOMAIN = http://example.org [settings] atom_posts_per_page = 0 +posts_per_page = 0 [posts] post01 @@ -464,8 +465,6 @@ post11 EOF ${TESTS_ENVIRONMENT} @abs_top_builddir@/blogc-make -f "${TEMP}/proj/blogcfile" 2>&1 | tee "${TEMP}/output.txt" -grep "_build/index\\.html" "${TEMP}/output.txt" -grep "_build/atom\\.xml" "${TEMP}/output.txt" grep "_build/post/post01/index\\.html" "${TEMP}/output.txt" grep "_build/post/post02/index\\.html" "${TEMP}/output.txt" grep "_build/post/post03/index\\.html" "${TEMP}/output.txt" @@ -480,50 +479,6 @@ grep "_build/post/post11/index\\.html" "${TEMP}/output.txt" rm "${TEMP}/output.txt" -cat > "${TEMP}/expected-index.html" < "${TEMP}/expected-atom.xml" < - - Lol's Website - /atom.xml - - - - - Lol - author@example.com - - WAT?! - - -EOF -diff -uN "${TEMP}/proj/_build/atom.xml" "${TEMP}/expected-atom.xml" - cat > "${TEMP}/expected-post-post01.html" <