From e4c08cd44e37f28cd11d62a95f4791ace663c62d Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Tue, 24 Jul 2018 21:54:27 +0200 Subject: make: fixed filename generation for rules and atom feed --- src/blogc-make/atom.c | 39 ++- tests/blogc-make/check_atom.c | 86 +++++- tests/blogc-make/check_blogc_make.sh.in | 526 +++++++++++++++++++++++++++++--- 3 files changed, 582 insertions(+), 69 deletions(-) diff --git a/src/blogc-make/atom.c b/src/blogc-make/atom.c index 0e4e631..ae5e0c1 100644 --- a/src/blogc-make/atom.c +++ b/src/blogc-make/atom.c @@ -13,6 +13,7 @@ #include "../common/error.h" #include "../common/utils.h" #include "settings.h" +#include "utils.h" #include "atom.h" static const char atom_template[] = @@ -20,12 +21,10 @@ static const char atom_template[] = "\n" " {{ SITE_TITLE }}{%% ifdef FILTER_TAG %%} - " "{{ FILTER_TAG }}{%% endif %%}\n" - " {{ BASE_URL }}%s%s{%% ifdef FILTER_TAG %%}/{{ FILTER_TAG }}" - "{%% endif %%}%s\n" + " {{ BASE_URL }}%s\n" " {{ DATE_FIRST_FORMATTED }}\n" " \n" - " \n" + " \n" " \n" " {{ AUTHOR_NAME }}\n" " {{ AUTHOR_EMAIL }}\n" @@ -34,10 +33,10 @@ static const char atom_template[] = " {%% block listing %%}\n" " \n" " {{ TITLE }}\n" - " {{ BASE_URL }}%s%s/{{ FILENAME }}/\n" + " {{ BASE_URL }}%s\n" " {{ DATE_FORMATTED }}\n" " {{ DATE_FORMATTED }}\n" - " \n" + " \n" " \n" " {{ AUTHOR_NAME }}\n" " {{ AUTHOR_EMAIL }}\n" @@ -66,12 +65,30 @@ bm_atom_deploy(bm_settings_t *settings, bc_error_t **err) const char *atom_prefix = bc_trie_lookup(settings->settings, "atom_prefix"); const char *atom_ext = bc_trie_lookup(settings->settings, "atom_ext"); const char *post_prefix = bc_trie_lookup(settings->settings, "post_prefix"); - const char *atom_slash = atom_prefix[0] == '\0' ? "" : "/"; - const char *post_slash = post_prefix[0] == '\0' ? "" : "/"; + const char *post_ext = bc_trie_lookup(settings->settings, "html_ext"); - char *content = bc_strdup_printf(atom_template, atom_slash, atom_prefix, - atom_ext, atom_slash, atom_prefix, atom_ext, post_slash, post_prefix, - post_slash, post_prefix); + bc_string_t *atom_url = bc_string_new(); + + if (atom_prefix[0] != '\0') + bc_string_append_c(atom_url, '/'); + + bc_string_append(atom_url, atom_prefix); + bc_string_append(atom_url, "{% ifdef FILTER_TAG %}/{{ FILTER_TAG }}"); + + if (atom_prefix[0] == '\0' && atom_ext[0] != '/') + bc_string_append(atom_url, "{% else %}/index"); + + bc_string_append(atom_url, "{% endif %}"); + bc_string_append(atom_url, atom_ext); + + char *post_url = bm_generate_filename(NULL, post_prefix, "{{ FILENAME }}", + post_ext); + + char *content = bc_strdup_printf(atom_template, atom_url->str, atom_url->str, + post_url, post_url); + + bc_string_free(atom_url, true); + free(post_url); if (-1 == write(fd, content, strlen(content))) { *err = bc_error_new_printf(BLOGC_MAKE_ERROR_ATOM, diff --git a/tests/blogc-make/check_atom.c b/tests/blogc-make/check_atom.c index 43ed93d..ac68e67 100644 --- a/tests/blogc-make/check_atom.c +++ b/tests/blogc-make/check_atom.c @@ -22,13 +22,14 @@ static void -test_atom_empty(void **state) +test_atom_empty_file(void **state) { bm_settings_t *settings = bc_malloc(sizeof(bm_settings_t)); settings->settings = bc_trie_new(free); bc_trie_insert(settings->settings, "atom_prefix", bc_strdup("")); bc_trie_insert(settings->settings, "atom_ext", bc_strdup(".xml")); bc_trie_insert(settings->settings, "post_prefix", bc_strdup("")); + bc_trie_insert(settings->settings, "html_ext", bc_strdup(".html")); bc_error_t *err = NULL; char *rv = bm_atom_deploy(settings, &err); @@ -48,11 +49,73 @@ test_atom_empty(void **state) " {{ SITE_TITLE }}{% ifdef FILTER_TAG %} - " "{{ FILTER_TAG }}{% endif %}\n" " {{ BASE_URL }}{% ifdef FILTER_TAG %}/{{ FILTER_TAG }}" - "{% endif %}.xml\n" + "{% else %}/index{% endif %}.xml\n" " {{ DATE_FIRST_FORMATTED }}\n" " \n" " \n" + "/{{ FILTER_TAG }}{% else %}/index{% endif %}.xml\" rel=\"self\" />\n" + " \n" + " {{ AUTHOR_NAME }}\n" + " {{ AUTHOR_EMAIL }}\n" + " \n" + " {{ SITE_TAGLINE }}\n" + " {% block listing %}\n" + " \n" + " {{ TITLE }}\n" + " {{ BASE_URL }}/{{ FILENAME }}.html\n" + " {{ DATE_FORMATTED }}\n" + " {{ DATE_FORMATTED }}\n" + " \n" + " \n" + " {{ AUTHOR_NAME }}\n" + " {{ AUTHOR_EMAIL }}\n" + " \n" + " \n" + " \n" + " {% endblock %}\n" + "\n"); + + free(cmp); + bm_atom_destroy(rv); + free(rv); + bc_trie_free(settings->settings); + free(settings); +} + + +static void +test_atom_empty_dir(void **state) +{ + bm_settings_t *settings = bc_malloc(sizeof(bm_settings_t)); + settings->settings = bc_trie_new(free); + bc_trie_insert(settings->settings, "atom_prefix", bc_strdup("")); + bc_trie_insert(settings->settings, "atom_ext", bc_strdup("/index.xml")); + bc_trie_insert(settings->settings, "post_prefix", bc_strdup("")); + bc_trie_insert(settings->settings, "html_ext", bc_strdup("/index.html")); + + bc_error_t *err = NULL; + char *rv = bm_atom_deploy(settings, &err); + + assert_non_null(rv); + assert_null(err); + + size_t cmp_len; + char *cmp = bc_file_get_contents(rv, true, &cmp_len, &err); + + assert_non_null(cmp); + assert_null(err); + + assert_string_equal(cmp, + "\n" + "\n" + " {{ SITE_TITLE }}{% ifdef FILTER_TAG %} - " + "{{ FILTER_TAG }}{% endif %}\n" + " {{ BASE_URL }}{% ifdef FILTER_TAG %}/{{ FILTER_TAG }}" + "{% endif %}/index.xml\n" + " {{ DATE_FIRST_FORMATTED }}\n" + " \n" + " \n" " \n" " {{ AUTHOR_NAME }}\n" " {{ AUTHOR_EMAIL }}\n" @@ -61,10 +124,10 @@ test_atom_empty(void **state) " {% block listing %}\n" " \n" " {{ TITLE }}\n" - " {{ BASE_URL }}/{{ FILENAME }}/\n" + " {{ BASE_URL }}/{{ FILENAME }}/index.html\n" " {{ DATE_FORMATTED }}\n" " {{ DATE_FORMATTED }}\n" - " \n" + " \n" " \n" " {{ AUTHOR_NAME }}\n" " {{ AUTHOR_EMAIL }}\n" @@ -90,6 +153,7 @@ test_atom_file(void **state) bc_trie_insert(settings->settings, "atom_prefix", bc_strdup("atom")); bc_trie_insert(settings->settings, "atom_ext", bc_strdup(".xml")); bc_trie_insert(settings->settings, "post_prefix", bc_strdup("post")); + bc_trie_insert(settings->settings, "html_ext", bc_strdup(".html")); bc_error_t *err = NULL; char *rv = bm_atom_deploy(settings, &err); @@ -122,10 +186,10 @@ test_atom_file(void **state) " {% block listing %}\n" " \n" " {{ TITLE }}\n" - " {{ BASE_URL }}/post/{{ FILENAME }}/\n" + " {{ BASE_URL }}/post/{{ FILENAME }}.html\n" " {{ DATE_FORMATTED }}\n" " {{ DATE_FORMATTED }}\n" - " \n" + " \n" " \n" " {{ AUTHOR_NAME }}\n" " {{ AUTHOR_EMAIL }}\n" @@ -151,6 +215,7 @@ test_atom_dir(void **state) bc_trie_insert(settings->settings, "atom_prefix", bc_strdup("atom")); bc_trie_insert(settings->settings, "atom_ext", bc_strdup("/index.xml")); bc_trie_insert(settings->settings, "post_prefix", bc_strdup("post")); + bc_trie_insert(settings->settings, "html_ext", bc_strdup("/index.html")); bc_error_t *err = NULL; char *rv = bm_atom_deploy(settings, &err); @@ -183,10 +248,10 @@ test_atom_dir(void **state) " {% block listing %}\n" " \n" " {{ TITLE }}\n" - " {{ BASE_URL }}/post/{{ FILENAME }}/\n" + " {{ BASE_URL }}/post/{{ FILENAME }}/index.html\n" " {{ DATE_FORMATTED }}\n" " {{ DATE_FORMATTED }}\n" - " \n" + " \n" " \n" " {{ AUTHOR_NAME }}\n" " {{ AUTHOR_EMAIL }}\n" @@ -208,7 +273,8 @@ int main(void) { const UnitTest tests[] = { - unit_test(test_atom_empty), + unit_test(test_atom_empty_file), + unit_test(test_atom_empty_dir), unit_test(test_atom_file), unit_test(test_atom_dir), }; diff --git a/tests/blogc-make/check_blogc_make.sh.in b/tests/blogc-make/check_blogc_make.sh.in index 064c801..2a82b2d 100755 --- a/tests/blogc-make/check_blogc_make.sh.in +++ b/tests/blogc-make/check_blogc_make.sh.in @@ -111,10 +111,10 @@ cat > "${TEMP}/expected-atom.xml" < Bar - /post/bar/ + /post/bar/index.html 2016-09-01T00:00:00Z 2016-09-01T00:00:00Z - + Lol author@example.com @@ -125,10 +125,10 @@ cat > "${TEMP}/expected-atom.xml" < Foo - /post/foo/ + /post/foo/index.html 2016-10-01T00:00:00Z 2016-10-01T00:00:00Z - + Lol author@example.com @@ -255,10 +255,10 @@ cat > "${TEMP}/expected-atom.xml" < Post 11 - /post/post11/ + /post/post11/index.html 2016-09-11T00:00:00Z 2016-09-11T00:00:00Z - + Lol author@example.com @@ -269,10 +269,10 @@ cat > "${TEMP}/expected-atom.xml" < Post 10 - /post/post10/ + /post/post10/index.html 2016-09-10T00:00:00Z 2016-09-10T00:00:00Z - + Lol author@example.com @@ -283,10 +283,10 @@ cat > "${TEMP}/expected-atom.xml" < Post 09 - /post/post09/ + /post/post09/index.html 2016-09-09T00:00:00Z 2016-09-09T00:00:00Z - + Lol author@example.com @@ -297,10 +297,10 @@ cat > "${TEMP}/expected-atom.xml" < Post 08 - /post/post08/ + /post/post08/index.html 2016-09-08T00:00:00Z 2016-09-08T00:00:00Z - + Lol author@example.com @@ -311,10 +311,10 @@ cat > "${TEMP}/expected-atom.xml" < Post 07 - /post/post07/ + /post/post07/index.html 2016-09-07T00:00:00Z 2016-09-07T00:00:00Z - + Lol author@example.com @@ -325,10 +325,10 @@ cat > "${TEMP}/expected-atom.xml" < Post 06 - /post/post06/ + /post/post06/index.html 2016-09-06T00:00:00Z 2016-09-06T00:00:00Z - + Lol author@example.com @@ -339,10 +339,10 @@ cat > "${TEMP}/expected-atom.xml" < Post 05 - /post/post05/ + /post/post05/index.html 2016-09-05T00:00:00Z 2016-09-05T00:00:00Z - + Lol author@example.com @@ -353,10 +353,10 @@ cat > "${TEMP}/expected-atom.xml" < Post 04 - /post/post04/ + /post/post04/index.html 2016-09-04T00:00:00Z 2016-09-04T00:00:00Z - + Lol author@example.com @@ -367,10 +367,10 @@ cat > "${TEMP}/expected-atom.xml" < Post 03 - /post/post03/ + /post/post03/index.html 2016-09-03T00:00:00Z 2016-09-03T00:00:00Z - + Lol author@example.com @@ -381,10 +381,10 @@ cat > "${TEMP}/expected-atom.xml" < Post 02 - /post/post02/ + /post/post02/index.html 2016-09-02T00:00:00Z 2016-09-02T00:00:00Z - + Lol author@example.com @@ -395,10 +395,10 @@ cat > "${TEMP}/expected-atom.xml" < Post 01 - /post/post01/ + /post/post01/index.html 2016-09-01T00:00:00Z 2016-09-01T00:00:00Z - + Lol author@example.com @@ -559,10 +559,10 @@ cat > "${TEMP}/expected-atom.xml" < Foo - /post/foo/ + /post/foo/index.html 2016-10-01T00:00:00Z 2016-10-01T00:00:00Z - + Lol author@example.com @@ -573,10 +573,10 @@ cat > "${TEMP}/expected-atom.xml" < Bar - /post/bar/ + /post/bar/index.html 2016-09-01T00:00:00Z 2016-09-01T00:00:00Z - + Lol author@example.com @@ -674,10 +674,10 @@ cat > "${TEMP}/expected-atom.xml" < Foo - /post/foo/ + /post/foo/index.html 2016-10-01T00:00:00Z 2016-10-01T00:00:00Z - + Lol author@example.com @@ -688,10 +688,10 @@ cat > "${TEMP}/expected-atom.xml" < Bar - /post/bar/ + /post/bar/index.html 2016-09-01T00:00:00Z 2016-09-01T00:00:00Z - + Lol author@example.com @@ -702,10 +702,10 @@ cat > "${TEMP}/expected-atom.xml" < Baz - /post/baz/ + /post/baz/index.html 2016-08-01T00:00:00Z 2016-08-01T00:00:00Z - + Lol author@example.com @@ -734,10 +734,10 @@ cat > "${TEMP}/expected-atom-tag1.xml" < Baz - /post/baz/ + /post/baz/index.html 2016-08-01T00:00:00Z 2016-08-01T00:00:00Z - + Lol author@example.com @@ -766,10 +766,10 @@ cat > "${TEMP}/expected-atom-tag2.xml" < Baz - /post/baz/ + /post/baz/index.html 2016-08-01T00:00:00Z 2016-08-01T00:00:00Z - + Lol author@example.com @@ -993,10 +993,10 @@ cat > "${TEMP}/expected-atom.xml" < Foo - /poost/foo/ + /poost/foo.html 2016-10-01T00:00:00Z 2016-10-01T00:00:00Z - + Lol author@example.com @@ -1034,7 +1034,7 @@ diff -uN "${TEMP}/proj/_build/poost/bar.html" "${TEMP}/expected-post-bar.html" rm -rf "${TEMP}/proj/_build" -### default settings with some posts and tags +### custom settings with some posts and tags cat > "${TEMP}/proj/contents/poost/baz.blogc" < "${TEMP}/expected-atom-tag1.xml" < Baz - /poost/baz/ + /poost/baz.html 2016-08-01T00:00:00Z 2016-08-01T00:00:00Z - + Lol author@example.com @@ -1129,10 +1129,10 @@ cat > "${TEMP}/expected-atom-tag2.xml" < Baz - /poost/baz/ + /poost/baz.html 2016-08-01T00:00:00Z 2016-08-01T00:00:00Z - + Lol author@example.com @@ -1177,7 +1177,7 @@ diff -uN "${TEMP}/proj/_build/taag/tag2.html" "${TEMP}/expected-tag2.html" rm -rf "${TEMP}/proj/_build" -### default settings with some posts, pages and tags +### custom settings with some posts, pages and tags cat > "${TEMP}/proj/contents/page1.blogc" < "${TEMP}/proj/contents/foo.blogc" < "${TEMP}/proj/contents/bar.blogc" < "${TEMP}/proj/temp/main.html" < "${TEMP}/proj/blogcfile" <&1 | tee "${TEMP}/output.txt" +grep "_build/index\\.html" "${TEMP}/output.txt" +grep "_build/index\\.xml" "${TEMP}/output.txt" +grep "_build/1/index\\.html" "${TEMP}/output.txt" +grep "_build/2/index\\.html" "${TEMP}/output.txt" +grep "_build/foo/index\\.html" "${TEMP}/output.txt" +grep "_build/bar/index\\.html" "${TEMP}/output.txt" + +rm "${TEMP}/output.txt" + +cat > "${TEMP}/expected-index.html" < "${TEMP}/expected-page-2.html" < "${TEMP}/expected-atom.xml" < + + Lol's Website + /index.xml + 2016-10-01T00:00:00Z + + + + Lol + author@example.com + + WAT?! + + + Foo + /foo/index.html + 2016-10-01T00:00:00Z + 2016-10-01T00:00:00Z + + + Lol + author@example.com + + This is foo.

+]]>
+
+ +
+EOF +diff -uN "${TEMP}/proj/_build/index.xml" "${TEMP}/expected-atom.xml" + +cat > "${TEMP}/expected-post-foo.html" <This is foo.

+ + +EOF +diff -uN "${TEMP}/proj/_build/foo/index.html" "${TEMP}/expected-post-foo.html" + +cat > "${TEMP}/expected-post-bar.html" <This is bar.

+ + +EOF +diff -uN "${TEMP}/proj/_build/bar/index.html" "${TEMP}/expected-post-bar.html" + +rm -rf "${TEMP}/proj/_build" + + +### empty prefixes with some posts and tags, and different exts + +cat > "${TEMP}/proj/contents/baz.blogc" < "${TEMP}/proj/blogcfile" <&1 | tee "${TEMP}/output.txt" +grep "_build/index\\.html" "${TEMP}/output.txt" +grep "_build/index\\.xml" "${TEMP}/output.txt" +grep "_build/tag1/index\\.xml" "${TEMP}/output.txt" +grep "_build/tag2/index\\.xml" "${TEMP}/output.txt" +grep "_build/1\\.html" "${TEMP}/output.txt" +grep "_build/2\\.html" "${TEMP}/output.txt" +grep "_build/3\\.html" "${TEMP}/output.txt" +grep "_build/foo\\.html" "${TEMP}/output.txt" +grep "_build/bar\\.html" "${TEMP}/output.txt" +grep "_build/baz\\.html" "${TEMP}/output.txt" +grep "_build/tag1\\.html" "${TEMP}/output.txt" +grep "_build/tag2\\.html" "${TEMP}/output.txt" + +rm "${TEMP}/output.txt" + +diff -uN "${TEMP}/proj/_build/index.html" "${TEMP}/expected-index.html" +diff -uN "${TEMP}/proj/_build/1.html" "${TEMP}/expected-index.html" +diff -uN "${TEMP}/proj/_build/2.html" "${TEMP}/expected-page-2.html" + +cat > "${TEMP}/expected-page-3.html" < "${TEMP}/expected-atom.xml" < + + Lol's Website + /index.xml + 2016-10-01T00:00:00Z + + + + Lol + author@example.com + + WAT?! + + + Foo + /foo.html + 2016-10-01T00:00:00Z + 2016-10-01T00:00:00Z + + + Lol + author@example.com + + This is foo.

+]]>
+
+ +
+EOF +diff -uN "${TEMP}/proj/_build/index.xml" "${TEMP}/expected-atom.xml" + +cat > "${TEMP}/expected-atom-tag1.xml" < + + Lol's Website - tag1 + /tag1/index.xml + 2016-08-01T00:00:00Z + + + + Lol + author@example.com + + WAT?! + + + Baz + /baz.html + 2016-08-01T00:00:00Z + 2016-08-01T00:00:00Z + + + Lol + author@example.com + + This is baz.

+]]>
+
+ +
+EOF +diff -uN "${TEMP}/proj/_build/tag1/index.xml" "${TEMP}/expected-atom-tag1.xml" + +cat > "${TEMP}/expected-atom-tag2.xml" < + + Lol's Website - tag2 + /tag2/index.xml + 2016-08-01T00:00:00Z + + + + Lol + author@example.com + + WAT?! + + + Baz + /baz.html + 2016-08-01T00:00:00Z + 2016-08-01T00:00:00Z + + + Lol + author@example.com + + This is baz.

+]]>
+
+ +
+EOF +diff -uN "${TEMP}/proj/_build/tag2/index.xml" "${TEMP}/expected-atom-tag2.xml" + +cat > "${TEMP}/expected-post-baz.html" <This is baz.

+ + +EOF +diff -uN "${TEMP}/proj/_build/foo.html" "${TEMP}/expected-post-foo.html" +diff -uN "${TEMP}/proj/_build/bar.html" "${TEMP}/expected-post-bar.html" +diff -uN "${TEMP}/proj/_build/baz.html" "${TEMP}/expected-post-baz.html" + +cat > "${TEMP}/expected-tag1.html" < "${TEMP}/expected-tag2.html" < "${TEMP}/proj/contents/page1.blogc" < "${TEMP}/proj/contents/page2.blogc" <> "${TEMP}/proj/blogcfile" <&1 | tee "${TEMP}/output.txt" +grep "_build/index\\.html" "${TEMP}/output.txt" +grep "_build/index\\.xml" "${TEMP}/output.txt" +grep "_build/tag1/index\\.xml" "${TEMP}/output.txt" +grep "_build/tag2/index\\.xml" "${TEMP}/output.txt" +grep "_build/1\\.html" "${TEMP}/output.txt" +grep "_build/2\\.html" "${TEMP}/output.txt" +grep "_build/3\\.html" "${TEMP}/output.txt" +grep "_build/foo\\.html" "${TEMP}/output.txt" +grep "_build/bar\\.html" "${TEMP}/output.txt" +grep "_build/baz\\.html" "${TEMP}/output.txt" +grep "_build/tag1\\.html" "${TEMP}/output.txt" +grep "_build/tag2\\.html" "${TEMP}/output.txt" +grep "_build/page1\\.html" "${TEMP}/output.txt" +grep "_build/page2\\.html" "${TEMP}/output.txt" + +rm "${TEMP}/output.txt" + +diff -uN "${TEMP}/proj/_build/index.html" "${TEMP}/expected-index.html" +diff -uN "${TEMP}/proj/_build/1.html" "${TEMP}/expected-index.html" +diff -uN "${TEMP}/proj/_build/2.html" "${TEMP}/expected-page-2.html" +diff -uN "${TEMP}/proj/_build/3.html" "${TEMP}/expected-page-3.html" + +diff -uN "${TEMP}/proj/_build/index.xml" "${TEMP}/expected-atom.xml" +diff -uN "${TEMP}/proj/_build/tag1/index.xml" "${TEMP}/expected-atom-tag1.xml" +diff -uN "${TEMP}/proj/_build/tag2/index.xml" "${TEMP}/expected-atom-tag2.xml" + +diff -uN "${TEMP}/proj/_build/foo.html" "${TEMP}/expected-post-foo.html" +diff -uN "${TEMP}/proj/_build/bar.html" "${TEMP}/expected-post-bar.html" +diff -uN "${TEMP}/proj/_build/baz.html" "${TEMP}/expected-post-baz.html" + +diff -uN "${TEMP}/proj/_build/tag1.html" "${TEMP}/expected-tag1.html" +diff -uN "${TEMP}/proj/_build/tag2.html" "${TEMP}/expected-tag2.html" + +cat > "${TEMP}/expected-page1.html" <This is page 1.

+ + +EOF +diff -uN "${TEMP}/proj/_build/page1.html" "${TEMP}/expected-page1.html" + +cat > "${TEMP}/expected-page2.html" <This is page 2.

+ + +EOF +diff -uN "${TEMP}/proj/_build/page2.html" "${TEMP}/expected-page2.html" + +rm -rf "${TEMP}/proj/_build" -- cgit v1.2.3-18-g5258