diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2019-04-03 00:39:56 +0200 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2019-04-03 00:40:03 +0200 |
commit | 3afa72febc3d6ed7003d10f3a5c4211bf761d746 (patch) | |
tree | b102b9fa81ce90c99a251156635aee74dfd012e9 | |
parent | adc3e6d77117d095e485ec64a93f1b64ffbf2cea (diff) | |
download | blogc-3afa72febc3d6ed7003d10f3a5c4211bf761d746.tar.gz blogc-3afa72febc3d6ed7003d10f3a5c4211bf761d746.tar.bz2 blogc-3afa72febc3d6ed7003d10f3a5c4211bf761d746.zip |
make: added support for `blogc -e`
must still add tests
-rw-r--r-- | man/blogcfile.5.ronn | 6 | ||||
-rw-r--r-- | src/blogc-make/ctx.c | 11 | ||||
-rw-r--r-- | src/blogc-make/ctx.h | 1 | ||||
-rw-r--r-- | src/blogc-make/exec.c | 16 | ||||
-rw-r--r-- | src/blogc-make/exec.h | 7 | ||||
-rw-r--r-- | src/blogc-make/rules.c | 39 | ||||
-rw-r--r-- | src/blogc-make/rules.h | 3 | ||||
-rw-r--r-- | src/blogc-make/settings.c | 1 | ||||
-rw-r--r-- | tests/blogc-make/check_exec.c | 54 |
9 files changed, 98 insertions, 40 deletions
diff --git a/man/blogcfile.5.ronn b/man/blogcfile.5.ronn index faeb85f..efaa5bd 100644 --- a/man/blogcfile.5.ronn +++ b/man/blogcfile.5.ronn @@ -95,6 +95,12 @@ however these rules can be customized with the following settings, from the option is useful if the user wants to host a page in the root of the website, and move the posts listing index to a subdirectory. + * `listing_entry` (default: unset): + The page that will be included in the listing pages of the website, without + the page prefix and without the extension, only the "slug" should be used. + See blogc(1) and the description of `listing_entry` block in blogc-template(7) + for details. + * `locale` (default: unset): The locale to be used when calling blogc(1). E.g. `en_US.UTF-8`. diff --git a/src/blogc-make/ctx.c b/src/blogc-make/ctx.c index 334a11d..b8b7bb6 100644 --- a/src/blogc-make/ctx.c +++ b/src/blogc-make/ctx.c @@ -256,6 +256,14 @@ bm_ctx_new(bm_ctx_t *base, const char *settings_file, const char *argv0, const char *content_dir = bm_ctx_settings_lookup(rv, "content_dir"); const char *post_prefix = bm_ctx_settings_lookup(rv, "post_prefix"); const char *source_ext = bm_ctx_settings_lookup(rv, "source_ext"); + const char *listing_entry = bm_ctx_settings_lookup(rv, "listing_entry"); + + rv->listing_entry_fctx = NULL; + if (listing_entry != NULL) { + char *f = bm_generate_filename(content_dir, NULL, listing_entry, source_ext); + rv->listing_entry_fctx = bm_filectx_new(rv, f, listing_entry, NULL); + free(f); + } rv->posts_fctx = NULL; if (settings->posts != NULL) { @@ -317,6 +325,7 @@ bm_ctx_reload(bm_ctx_t **ctx) bm_filectx_reload((*ctx)->main_template_fctx); bm_filectx_reload((*ctx)->atom_template_fctx); + bm_filectx_reload((*ctx)->listing_entry_fctx); for (bc_slist_t *tmp = (*ctx)->posts_fctx; tmp != NULL; tmp = tmp->next) bm_filectx_reload((bm_filectx_t*) tmp->data); @@ -357,6 +366,8 @@ bm_ctx_free_internal(bm_ctx_t *ctx) ctx->atom_template_fctx = NULL; bm_filectx_free(ctx->settings_fctx); ctx->settings_fctx = NULL; + bm_filectx_free(ctx->listing_entry_fctx); + ctx->listing_entry_fctx = NULL; bc_slist_free_full(ctx->posts_fctx, (bc_free_func_t) bm_filectx_free); ctx->posts_fctx = NULL; diff --git a/src/blogc-make/ctx.h b/src/blogc-make/ctx.h index 80aa6ea..a66d51c 100644 --- a/src/blogc-make/ctx.h +++ b/src/blogc-make/ctx.h @@ -60,6 +60,7 @@ typedef struct { bm_filectx_t *main_template_fctx; bm_filectx_t *atom_template_fctx; bm_filectx_t *settings_fctx; + bm_filectx_t *listing_entry_fctx; bc_slist_t *posts_fctx; bc_slist_t *pages_fctx; diff --git a/src/blogc-make/exec.c b/src/blogc-make/exec.c index cd71810..a78d506 100644 --- a/src/blogc-make/exec.c +++ b/src/blogc-make/exec.c @@ -219,7 +219,8 @@ list_variables(const char *key, const char *value, bc_string_t *str) char* bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings, bc_trie_t *global_variables, bc_trie_t *local_variables, bool listing, - const char *template, const char *output, bool dev, bool sources_stdin) + const char *listing_entry, const char *template, const char *output, + bool dev, bool sources_stdin) { bc_string_t *rv = bc_string_new(); @@ -255,6 +256,11 @@ bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings, if (listing) { bc_string_append(rv, " -l"); + if (listing_entry != NULL) { + char *tmp = bc_shell_quote(listing_entry); + bc_string_append_printf(rv, " -e %s", tmp); + free(tmp); + } } if (template != NULL) { @@ -279,8 +285,8 @@ bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings, int bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *global_variables, bc_trie_t *local_variables, - bool listing, bm_filectx_t *template, bm_filectx_t *output, bc_slist_t *sources, - bool only_first_source) + bool listing, bm_filectx_t *listing_entry, bm_filectx_t *template, + bm_filectx_t *output, bc_slist_t *sources, bool only_first_source) { if (ctx == NULL) return 1; @@ -293,8 +299,8 @@ bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *global_variables, bc_trie_t *local_varia } char *cmd = bm_exec_build_blogc_cmd(ctx->blogc, ctx->settings, global_variables, - local_variables, listing, template->path, output->path, ctx->dev, - input->len > 0); + local_variables, listing, listing_entry == NULL ? NULL : listing_entry->path, + template->path, output->path, ctx->dev, input->len > 0); if (ctx->verbose) printf("%s\n", cmd); diff --git a/src/blogc-make/exec.h b/src/blogc-make/exec.h index 1d58c0d..e09e8ee 100644 --- a/src/blogc-make/exec.h +++ b/src/blogc-make/exec.h @@ -20,10 +20,11 @@ int bm_exec_command(const char *cmd, const char *input, char **output, char **error, bc_error_t **err); char* bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings, bc_trie_t *global_variables, bc_trie_t *local_variables, bool listing, - const char *template, const char *output, bool dev, bool sources_stdin); + const char *listing_entry, const char *template, const char *output, bool dev, + bool sources_stdin); int bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *global_variables, bc_trie_t *local_variables, - bool listing, bm_filectx_t *template, bm_filectx_t *output, bc_slist_t *sources, - bool only_first_source); + bool listing, bm_filectx_t *listing_entry, bm_filectx_t *template, + bm_filectx_t *output, bc_slist_t *sources, bool only_first_source); int bm_exec_blogc_runserver(bm_ctx_t *ctx, const char *host, const char *port, const char *threads); diff --git a/src/blogc-make/rules.c b/src/blogc-make/rules.c index 5361502..59d11f2 100644 --- a/src/blogc-make/rules.c +++ b/src/blogc-make/rules.c @@ -109,10 +109,10 @@ index_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args) if (fctx == NULL) continue; if (bm_rule_need_rebuild(ctx->posts_fctx, ctx->settings_fctx, - ctx->main_template_fctx, fctx, false)) + ctx->listing_entry_fctx, ctx->main_template_fctx, fctx, false)) { - rv = bm_exec_blogc(ctx, variables, NULL, true, ctx->main_template_fctx, - fctx, ctx->posts_fctx, false); + rv = bm_exec_blogc(ctx, variables, NULL, true, ctx->listing_entry_fctx, + ctx->main_template_fctx, fctx, ctx->posts_fctx, false); if (rv != 0) break; } @@ -167,10 +167,10 @@ atom_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args) bm_filectx_t *fctx = l->data; if (fctx == NULL) continue; - if (bm_rule_need_rebuild(ctx->posts_fctx, ctx->settings_fctx, NULL, + if (bm_rule_need_rebuild(ctx->posts_fctx, ctx->settings_fctx, NULL, NULL, fctx, false)) { - rv = bm_exec_blogc(ctx, variables, NULL, true, ctx->atom_template_fctx, + rv = bm_exec_blogc(ctx, variables, NULL, true, NULL, ctx->atom_template_fctx, fctx, ctx->posts_fctx, false); if (rv != 0) break; @@ -233,10 +233,10 @@ atom_tags_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args) bc_trie_insert(variables, "FILTER_TAG", bc_strdup(ctx->settings->tags[i])); - if (bm_rule_need_rebuild(ctx->posts_fctx, ctx->settings_fctx, NULL, + if (bm_rule_need_rebuild(ctx->posts_fctx, ctx->settings_fctx, NULL, NULL, fctx, false)) { - rv = bm_exec_blogc(ctx, variables, NULL, true, ctx->atom_template_fctx, + rv = bm_exec_blogc(ctx, variables, NULL, true, NULL, ctx->atom_template_fctx, fctx, ctx->posts_fctx, false); if (rv != 0) break; @@ -310,10 +310,10 @@ pagination_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args) continue; bc_trie_insert(variables, "FILTER_PAGE", bc_strdup_printf("%zu", page)); if (bm_rule_need_rebuild(ctx->posts_fctx, ctx->settings_fctx, - ctx->main_template_fctx, fctx, false)) + ctx->listing_entry_fctx, ctx->main_template_fctx, fctx, false)) { - rv = bm_exec_blogc(ctx, variables, NULL, true, ctx->main_template_fctx, - fctx, ctx->posts_fctx, false); + rv = bm_exec_blogc(ctx, variables, NULL, true, ctx->listing_entry_fctx, + ctx->main_template_fctx, fctx, ctx->posts_fctx, false); if (rv != 0) break; } @@ -373,12 +373,12 @@ posts_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args) bm_filectx_t *o_fctx = o->data; if (o_fctx == NULL) continue; - if (bm_rule_need_rebuild(s, ctx->settings_fctx, + if (bm_rule_need_rebuild(s, ctx->settings_fctx, NULL, ctx->main_template_fctx, o_fctx, true)) { bc_trie_t *local = bc_trie_new(NULL); bc_trie_insert(local, "MAKE_SLUG", s_fctx->slug); // no need to copy - rv = bm_exec_blogc(ctx, variables, local, false, ctx->main_template_fctx, + rv = bm_exec_blogc(ctx, variables, local, false, NULL, ctx->main_template_fctx, o_fctx, s, true); bc_trie_free(local); if (rv != 0) @@ -444,10 +444,10 @@ tags_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args) bc_strdup(ctx->settings->tags[i])); if (bm_rule_need_rebuild(ctx->posts_fctx, ctx->settings_fctx, - ctx->main_template_fctx, fctx, false)) + ctx->listing_entry_fctx, ctx->main_template_fctx, fctx, false)) { - rv = bm_exec_blogc(ctx, variables, NULL, true, ctx->main_template_fctx, - fctx, ctx->posts_fctx, false); + rv = bm_exec_blogc(ctx, variables, NULL, true, ctx->listing_entry_fctx, + ctx->main_template_fctx, fctx, ctx->posts_fctx, false); if (rv != 0) break; } @@ -504,12 +504,12 @@ pages_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args) bm_filectx_t *o_fctx = o->data; if (o_fctx == NULL) continue; - if (bm_rule_need_rebuild(s, ctx->settings_fctx, + if (bm_rule_need_rebuild(s, ctx->settings_fctx, NULL, ctx->main_template_fctx, o_fctx, true)) { bc_trie_t *local = bc_trie_new(NULL); bc_trie_insert(local, "MAKE_SLUG", s_fctx->slug); // no need to copy - rv = bm_exec_blogc(ctx, variables, local, false, ctx->main_template_fctx, + rv = bm_exec_blogc(ctx, variables, local, false, NULL, ctx->main_template_fctx, o_fctx, s, true); bc_trie_free(local); if (rv != 0) @@ -562,7 +562,7 @@ copy_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args) if (o_fctx == NULL) continue; - if (bm_rule_need_rebuild(s, ctx->settings_fctx, NULL, o_fctx, true)) { + if (bm_rule_need_rebuild(s, ctx->settings_fctx, NULL, NULL, o_fctx, true)) { rv = bm_exec_native_cp(s->data, o_fctx, ctx->verbose); if (rv != 0) break; @@ -860,7 +860,8 @@ bm_rule_execute(bm_ctx_t *ctx, const bm_rule_t *rule, bc_trie_t *args) bool bm_rule_need_rebuild(bc_slist_t *sources, bm_filectx_t *settings, - bm_filectx_t *template, bm_filectx_t *output, bool only_first_source) + bm_filectx_t *listing_entry, bm_filectx_t *template, bm_filectx_t *output, + bool only_first_source) { if (output == NULL || !output->readable) return true; diff --git a/src/blogc-make/rules.h b/src/blogc-make/rules.h index 5fcc1eb..29ba27e 100644 --- a/src/blogc-make/rules.h +++ b/src/blogc-make/rules.h @@ -29,7 +29,8 @@ bc_trie_t* bm_rule_parse_args(const char *sep); int bm_rule_executor(bm_ctx_t *ctx, bc_slist_t *rule_list); int bm_rule_execute(bm_ctx_t *ctx, const bm_rule_t *rule, bc_trie_t *args); bool bm_rule_need_rebuild(bc_slist_t *sources, bm_filectx_t *settings, - bm_filectx_t *template, bm_filectx_t *output, bool only_first_source); + bm_filectx_t *listing_entry, bm_filectx_t *template, bm_filectx_t *output, + bool only_first_source); bc_slist_t* bm_rule_list_built_files(bm_ctx_t *ctx); void bm_rule_print_help(void); diff --git a/src/blogc-make/settings.c b/src/blogc-make/settings.c index deed018..05e610a 100644 --- a/src/blogc-make/settings.c +++ b/src/blogc-make/settings.c @@ -27,6 +27,7 @@ static const struct default_settings_map { {"atom_template", NULL}, // default: atom.c {"main_template", "main.tmpl"}, {"source_ext", ".txt"}, + {"listing_entry", NULL}, // pagination {"pagination_prefix", "page"}, diff --git a/tests/blogc-make/check_exec.c b/tests/blogc-make/check_exec.c index 32c7fae..8193ad7 100644 --- a/tests/blogc-make/check_exec.c +++ b/tests/blogc-make/check_exec.c @@ -88,20 +88,27 @@ test_build_blogc_cmd_with_settings(void **state) settings->tags = NULL; char *rv = bm_exec_build_blogc_cmd("blogc", settings, variables, local, true, - "main.tmpl", "foo.html", false, true); + NULL, "main.tmpl", "foo.html", false, true); assert_string_equal(rv, "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ' -D LOL='HEHE' " "-D ASD='QWE' -l -t 'main.tmpl' -o 'foo.html' -i"); free(rv); + rv = bm_exec_build_blogc_cmd("blogc", settings, variables, local, true, + "foo.txt", "main.tmpl", "foo.html", false, true); + assert_string_equal(rv, + "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ' -D LOL='HEHE' " + "-D ASD='QWE' -l -e 'foo.txt' -t 'main.tmpl' -o 'foo.html' -i"); + free(rv); + rv = bm_exec_build_blogc_cmd("blogc", settings, variables, NULL, false, NULL, - NULL, false, false); + NULL, NULL, false, false); assert_string_equal(rv, "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ' -D LOL='HEHE'"); free(rv); rv = bm_exec_build_blogc_cmd("blogc", settings, NULL, NULL, false, NULL, NULL, - false, false); + NULL, false, false); assert_string_equal(rv, "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ'"); free(rv); @@ -130,22 +137,30 @@ test_build_blogc_cmd_with_settings_and_dev(void **state) settings->tags = NULL; char *rv = bm_exec_build_blogc_cmd("blogc", settings, variables, local, true, - "main.tmpl", "foo.html", true, true); + NULL, "main.tmpl", "foo.html", true, true); assert_string_equal(rv, "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ' -D LOL='HEHE' " "-D ASD='QWE' -D MAKE_ENV_DEV=1 -D MAKE_ENV='dev' -l -t 'main.tmpl' " "-o 'foo.html' -i"); free(rv); + rv = bm_exec_build_blogc_cmd("blogc", settings, variables, local, true, + "foo.txt", "main.tmpl", "foo.html", true, true); + assert_string_equal(rv, + "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ' -D LOL='HEHE' " + "-D ASD='QWE' -D MAKE_ENV_DEV=1 -D MAKE_ENV='dev' -l -e 'foo.txt' " + "-t 'main.tmpl' -o 'foo.html' -i"); + free(rv); + rv = bm_exec_build_blogc_cmd("blogc", settings, variables, NULL, false, NULL, - NULL, true, false); + NULL, NULL, true, false); assert_string_equal(rv, "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ' -D LOL='HEHE' " "-D MAKE_ENV_DEV=1 -D MAKE_ENV='dev'"); free(rv); rv = bm_exec_build_blogc_cmd("blogc", settings, NULL, NULL, false, NULL, NULL, - true, false); + NULL, true, false); assert_string_equal(rv, "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ' " "-D MAKE_ENV_DEV=1 -D MAKE_ENV='dev'"); @@ -175,22 +190,30 @@ test_build_blogc_cmd_with_settings_and_tags(void **state) settings->tags = bc_str_split("asd foo bar", ' ', 0); char *rv = bm_exec_build_blogc_cmd("blogc", settings, variables, local, true, - "main.tmpl", "foo.html", true, true); + NULL, "main.tmpl", "foo.html", true, true); assert_string_equal(rv, "LC_ALL='en_US.utf8' blogc -D MAKE_TAGS='asd foo bar' -D FOO='BAR' " "-D BAR='BAZ' -D LOL='HEHE' -D ASD='QWE' -D MAKE_ENV_DEV=1 " "-D MAKE_ENV='dev' -l -t 'main.tmpl' -o 'foo.html' -i"); free(rv); + rv = bm_exec_build_blogc_cmd("blogc", settings, variables, local, true, + "foo.txt", "main.tmpl", "foo.html", true, true); + assert_string_equal(rv, + "LC_ALL='en_US.utf8' blogc -D MAKE_TAGS='asd foo bar' -D FOO='BAR' " + "-D BAR='BAZ' -D LOL='HEHE' -D ASD='QWE' -D MAKE_ENV_DEV=1 " + "-D MAKE_ENV='dev' -l -e 'foo.txt' -t 'main.tmpl' -o 'foo.html' -i"); + free(rv); + rv = bm_exec_build_blogc_cmd("blogc", settings, variables, NULL, false, NULL, - NULL, true, false); + NULL, NULL, true, false); assert_string_equal(rv, "LC_ALL='en_US.utf8' blogc -D MAKE_TAGS='asd foo bar' -D FOO='BAR' " "-D BAR='BAZ' -D LOL='HEHE' -D MAKE_ENV_DEV=1 -D MAKE_ENV='dev'"); free(rv); rv = bm_exec_build_blogc_cmd("blogc", settings, NULL, NULL, false, NULL, NULL, - true, false); + NULL, true, false); assert_string_equal(rv, "LC_ALL='en_US.utf8' blogc -D MAKE_TAGS='asd foo bar' -D FOO='BAR' " "-D BAR='BAZ' -D MAKE_ENV_DEV=1 -D MAKE_ENV='dev'"); @@ -214,19 +237,26 @@ test_build_blogc_cmd_without_settings(void **state) bc_trie_insert(local, "ASD", bc_strdup("QWE")); char *rv = bm_exec_build_blogc_cmd("blogc", NULL, variables, local, true, - "main.tmpl", "foo.html", false, true); + NULL, "main.tmpl", "foo.html", false, true); assert_string_equal(rv, "blogc -D LOL='HEHE' -D ASD='QWE' -l -t 'main.tmpl' -o 'foo.html' -i"); free(rv); + rv = bm_exec_build_blogc_cmd("blogc", NULL, variables, local, true, + "foo.txt", "main.tmpl", "foo.html", false, true); + assert_string_equal(rv, + "blogc -D LOL='HEHE' -D ASD='QWE' -l -e 'foo.txt' -t 'main.tmpl' " + "-o 'foo.html' -i"); + free(rv); + rv = bm_exec_build_blogc_cmd("blogc", NULL, variables, NULL, false, NULL, - NULL, false, false); + NULL, NULL, false, false); assert_string_equal(rv, "blogc -D LOL='HEHE'"); free(rv); rv = bm_exec_build_blogc_cmd("blogc", NULL, NULL, NULL, false, NULL, NULL, - false, false); + NULL, false, false); assert_string_equal(rv, "blogc"); free(rv); |