From 8a4e6248052ff32e02dcec3dd6850d4582836fb5 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Wed, 24 Jul 2019 18:04:28 +0200 Subject: make: fixed filename generation for pagination_tags --- src/blogc-make/rules.c | 18 +- src/blogc-make/utils.c | 34 +++- src/blogc-make/utils.h | 2 + tests/blogc-make/check_blogc_make.sh.in | 182 +++++++++++++++++ tests/blogc-make/check_utils.c | 349 ++++++++++++++++++++++++++++++++ 5 files changed, 571 insertions(+), 14 deletions(-) diff --git a/src/blogc-make/rules.c b/src/blogc-make/rules.c index 591ad21..06223c0 100644 --- a/src/blogc-make/rules.c +++ b/src/blogc-make/rules.c @@ -367,24 +367,19 @@ pagination_tags_outputlist(bm_ctx_t *ctx) bc_trie_free(local); if (last_page == NULL) - break; + continue; long pages = strtol(last_page, NULL, 10); free(last_page); - char *prefix = bc_strdup_printf("%s/%s/%s", tag_prefix, - ctx->settings->tags[k], pagination_prefix); - for (size_t i = 0; i < pages; i++) { char *j = bc_strdup_printf("%d", i + 1); - char *f = bm_generate_filename(ctx->short_output_dir, prefix, - j, html_ext); + char *f = bm_generate_filename2(ctx->short_output_dir, tag_prefix, + ctx->settings->tags[k], pagination_prefix, j, html_ext); rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL, NULL)); free(j); free(f); } - - free(prefix); } bc_trie_free(variables); @@ -425,16 +420,14 @@ pagination_tags_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args) // tag and page from the file path right now :/ char *tag = NULL; for (size_t i = 0; ctx->settings->tags[i] != NULL; i++) { - char *prefix = bc_strdup_printf("%s/%s/%s", tag_prefix, - ctx->settings->tags[i], pagination_prefix); bool b = false; // it is impossible to have more output files per tag than the whole // amount of output pages for (size_t k = 1; k <= bc_slist_length(outputs); k++) { char *j = bc_strdup_printf("%d", k); - char *f = bm_generate_filename(ctx->short_output_dir, prefix, - j, html_ext); + char *f = bm_generate_filename2(ctx->short_output_dir, tag_prefix, + ctx->settings->tags[i], pagination_prefix, j, html_ext); free(j); if (0 == strcmp(fctx->short_path, f)) { tag = ctx->settings->tags[i]; @@ -445,7 +438,6 @@ pagination_tags_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args) } free(f); } - free(prefix); if (b) break; } diff --git a/src/blogc-make/utils.c b/src/blogc-make/utils.c index 91b7b53..8f69e44 100644 --- a/src/blogc-make/utils.c +++ b/src/blogc-make/utils.c @@ -28,7 +28,7 @@ bm_generate_filename(const char *dir, const char *prefix, const char *fname, bool have_ext = ext != NULL && ext[0] != '\0'; bool have_ext_noslash = have_ext && ext[0] != '/'; bool is_index = have_fname && have_ext && ( - (0 == strcmp(fname, "index")) && ext[0] == '/'); + (0 == strcmp(fname, "index")) && ext[0] == '/') && !have_prefix; bc_string_t *rv = bc_string_new(); @@ -69,6 +69,38 @@ bm_generate_filename(const char *dir, const char *prefix, const char *fname, } +char* +bm_generate_filename2(const char *dir, const char *prefix, const char *fname, + const char *prefix2, const char *fname2, const char *ext) +{ + bool have_prefix = prefix != NULL && prefix[0] != '\0'; + bool have_fname = fname != NULL && fname[0] != '\0'; + bool have_prefix2 = prefix2 != NULL && prefix2[0] != '\0'; + + bc_string_t *p = bc_string_new(); + + if (have_prefix) + bc_string_append(p, prefix); + + if (have_prefix && (have_fname || have_prefix2)) + bc_string_append_c(p, '/'); + + if (have_fname) + bc_string_append(p, fname); + + if (have_fname && have_prefix2) + bc_string_append_c(p, '/'); + + if (have_prefix2) + bc_string_append(p, prefix2); + + char *rv = bm_generate_filename(dir, p->str, fname2, ext); + bc_string_free(p, true); + + return rv; +} + + char* bm_abspath(const char *path, bc_error_t **err) { diff --git a/src/blogc-make/utils.h b/src/blogc-make/utils.h index 5353c01..cbcfc0e 100644 --- a/src/blogc-make/utils.h +++ b/src/blogc-make/utils.h @@ -13,6 +13,8 @@ char* bm_generate_filename(const char *dir, const char *prefix, const char *fname, const char *ext); +char* bm_generate_filename2(const char *dir, const char *prefix, const char *fname, + const char *prefix2, const char *fname2, const char *ext); char* bm_abspath(const char *path, bc_error_t **err); #endif /* _MAKE_UTILS_H */ diff --git a/tests/blogc-make/check_blogc_make.sh.in b/tests/blogc-make/check_blogc_make.sh.in index 36dfceb..423b83e 100755 --- a/tests/blogc-make/check_blogc_make.sh.in +++ b/tests/blogc-make/check_blogc_make.sh.in @@ -38,6 +38,7 @@ ${TESTS_ENVIRONMENT} @abs_top_builddir@/blogc-make -f "${TEMP}/proj/blogcfile" 2 cat > "${TEMP}/proj/content/post/foo.txt" < "${TEMP}/proj/content/post/bar.txt" < "${TEMP}/proj/content/post/post${i}.txt" < "${TEMP}/proj/blogcfile" <&1 | tee "${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" +grep "_build/post/post04/index\\.html" "${TEMP}/output.txt" +grep "_build/post/post05/index\\.html" "${TEMP}/output.txt" +grep "_build/post/post06/index\\.html" "${TEMP}/output.txt" +grep "_build/post/post07/index\\.html" "${TEMP}/output.txt" +grep "_build/post/post08/index\\.html" "${TEMP}/output.txt" +grep "_build/post/post09/index\\.html" "${TEMP}/output.txt" +grep "_build/post/post10/index\\.html" "${TEMP}/output.txt" +grep "_build/post/post11/index\\.html" "${TEMP}/output.txt" +grep "_build/tag/asd/index\\.html" "${TEMP}/output.txt" +grep "_build/tag/asd/page/1/index\\.html" "${TEMP}/output.txt" +grep "_build/tag/asd/page/2/index\\.html" "${TEMP}/output.txt" +grep "_build/tag/asd/page/3/index\\.html" "${TEMP}/output.txt" +grep "_build/tag/asd/page/4/index\\.html" "${TEMP}/output.txt" +grep -v "_build/tag/asd/page/5/index\\.html" "${TEMP}/output.txt" +grep "_build/tag/qwe/index\\.html" "${TEMP}/output.txt" +grep "_build/tag/qwe/page/1/index\\.html" "${TEMP}/output.txt" +grep -v "_build/tag/qwe/page/2/index\\.html" "${TEMP}/output.txt" + +rm "${TEMP}/output.txt" + +cat > "${TEMP}/expected-tag-asd1.html" < "${TEMP}/expected-tag-asd2.html" < "${TEMP}/expected-tag-asd3.html" < "${TEMP}/expected-tag-asd4.html" < "${TEMP}/expected-tag-qwe1.html" < "${TEMP}/proj/blogcfile" < "${TEMP}/expected-tag2.html" <This is hue.

@@ -1292,6 +1424,7 @@ Listing: tag2 - Baz - Aug 01, 2016, 12:00 AM GMT EOF diff -uN "${TEMP}/proj/_build/tag/tag2/index.html" "${TEMP}/expected-tag2.html" +diff -uN "${TEMP}/proj/_build/tag/tag2/page/1/index.html" "${TEMP}/expected-tag2.html" rm -rf "${TEMP}/proj/_build" @@ -1322,11 +1455,16 @@ grep "_build/atom\\.xml" "${TEMP}/output.txt" grep "_build/atom/tag1\\.xml" "${TEMP}/output.txt" grep "_build/atom/tag2\\.xml" "${TEMP}/output.txt" grep "_build/page/1/index\\.html" "${TEMP}/output.txt" +grep -v "_build/page/2/index\\.html" "${TEMP}/output.txt" grep "_build/post/foo/index\\.html" "${TEMP}/output.txt" grep "_build/post/bar/index\\.html" "${TEMP}/output.txt" grep "_build/post/baz/index\\.html" "${TEMP}/output.txt" grep "_build/tag/tag1/index\\.html" "${TEMP}/output.txt" +grep "_build/tag/tag1/page/1/index\\.html" "${TEMP}/output.txt" +grep -v "_build/tag/tag1/page/2/index\\.html" "${TEMP}/output.txt" grep "_build/tag/tag2/index\\.html" "${TEMP}/output.txt" +grep "_build/tag/tag2/page/1/index\\.html" "${TEMP}/output.txt" +grep -v "_build/tag/tag2/page/2/index\\.html" "${TEMP}/output.txt" grep "_build/page1/index\\.html" "${TEMP}/output.txt" grep "_build/page2/index\\.html" "${TEMP}/output.txt" @@ -1345,7 +1483,9 @@ diff -uN "${TEMP}/proj/_build/post/bar/index.html" "${TEMP}/expected-post-bar.ht diff -uN "${TEMP}/proj/_build/post/baz/index.html" "${TEMP}/expected-post-baz.html" diff -uN "${TEMP}/proj/_build/tag/tag1/index.html" "${TEMP}/expected-tag1.html" +diff -uN "${TEMP}/proj/_build/tag/tag1/page/1/index.html" "${TEMP}/expected-tag1.html" diff -uN "${TEMP}/proj/_build/tag/tag2/index.html" "${TEMP}/expected-tag2.html" +diff -uN "${TEMP}/proj/_build/tag/tag2/page/1/index.html" "${TEMP}/expected-tag2.html" cat > "${TEMP}/expected-page1.html" < "${TEMP}/expected-tag2.html" < "${TEMP}/expected-page1.html" <