diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2019-04-02 22:57:33 +0200 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2019-04-02 22:57:33 +0200 |
commit | adc3e6d77117d095e485ec64a93f1b64ffbf2cea (patch) | |
tree | 684980dc6772818afd71cfddd14cc583285ad17e /src | |
parent | 1101d2fa0eb14baab50cc5c6cf63078930c06290 (diff) | |
download | blogc-adc3e6d77117d095e485ec64a93f1b64ffbf2cea.tar.gz blogc-adc3e6d77117d095e485ec64a93f1b64ffbf2cea.tar.bz2 blogc-adc3e6d77117d095e485ec64a93f1b64ffbf2cea.zip |
blogc: added `-e` cli option and `listing_entry` template block
Diffstat (limited to 'src')
-rw-r--r-- | src/blogc/main.c | 30 | ||||
-rw-r--r-- | src/blogc/renderer.c | 16 | ||||
-rw-r--r-- | src/blogc/renderer.h | 4 | ||||
-rw-r--r-- | src/blogc/template-parser.c | 12 |
4 files changed, 52 insertions, 10 deletions
diff --git a/src/blogc/main.c b/src/blogc/main.c index bae3c80..5a4df99 100644 --- a/src/blogc/main.c +++ b/src/blogc/main.c @@ -54,8 +54,8 @@ blogc_print_help(void) #ifdef MAKE_EMBEDDED "[-m] " #endif - "[-h] [-v] [-d] [-i] [-l] [-D KEY=VALUE ...] [-p KEY] [-t TEMPLATE]\n" - " [-o OUTPUT] [SOURCE ...] - A blog compiler.\n" + "[-h] [-v] [-d] [-i] [-l [-e SOURCE]] [-D KEY=VALUE ...] [-p KEY]\n" + " [-t TEMPLATE] [-o OUTPUT] [SOURCE ...] - A blog compiler.\n" "\n" "positional arguments:\n" " SOURCE source file(s)\n" @@ -66,6 +66,7 @@ blogc_print_help(void) " -d enable debug\n" " -i read list of source files from standard input\n" " -l build listing page, from multiple source files\n" + " -e SOURCE source file with content for listing page. requires '-l'\n" " -D KEY=VALUE set global variable\n" " -p KEY show the value of a variable after source parsing and exit\n" " -t TEMPLATE template file\n" @@ -85,8 +86,8 @@ blogc_print_usage(void) #ifdef MAKE_EMBEDDED "[-m] " #endif - "[-h] [-v] [-d] [-i] [-l] [-D KEY=VALUE ...] [-p KEY] [-t TEMPLATE]\n" - " [-o OUTPUT] [SOURCE ...]\n"); + "[-h] [-v] [-d] [-i] [-l [-e SOURCE]] [-D KEY=VALUE ...] [-p KEY]\n" + " [-t TEMPLATE] [-o OUTPUT] [SOURCE ...]\n"); } @@ -160,6 +161,7 @@ main(int argc, char **argv) bool debug = false; bool input_stdin = false; bool listing = false; + char *listing_entry = NULL; char *template = NULL; char *output = NULL; char *print = NULL; @@ -167,6 +169,7 @@ main(int argc, char **argv) char **pieces = NULL; bc_slist_t *sources = NULL; + bc_trie_t *listing_entry_source = NULL; bc_trie_t *config = bc_trie_new(free); bc_trie_insert(config, "BLOGC_VERSION", bc_strdup(PACKAGE_VERSION)); @@ -189,6 +192,12 @@ main(int argc, char **argv) case 'l': listing = true; break; + case 'e': + if (argv[i][2] != '\0') + listing_entry = bc_strdup(argv[i] + 2); + else if (i + 1 < argc) + listing_entry = bc_strdup(argv[++i]); + break; case 't': if (argv[i][2] != '\0') template = bc_strdup(argv[i] + 2); @@ -296,6 +305,15 @@ main(int argc, char **argv) goto cleanup2; } + if (listing && listing_entry != NULL) { + listing_entry_source = blogc_source_parse_from_file(listing_entry, &err); + if (err != NULL) { + bc_error_print(err, "blogc"); + rv = 1; + goto cleanup2; + } + } + if (print != NULL) { const char *val = NULL; if (!listing && s != NULL) { @@ -332,7 +350,7 @@ main(int argc, char **argv) if (debug) blogc_debug_template(l); - char *out = blogc_render(l, s, config, listing); + char *out = blogc_render(l, s, listing_entry_source, config, listing); bool write_to_stdout = (output == NULL || (0 == strcmp(output, "-"))); @@ -366,6 +384,8 @@ cleanup: free(template); free(output); free(print); + free(listing_entry); + bc_trie_free(listing_entry_source); bc_slist_free_full(sources, free); return rv; } diff --git a/src/blogc/renderer.c b/src/blogc/renderer.c index 035f71b..87b3f5a 100644 --- a/src/blogc/renderer.c +++ b/src/blogc/renderer.c @@ -166,7 +166,7 @@ blogc_split_list_variable(const char *name, bc_trie_t *global, bc_trie_t *local) char* -blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, bool listing) +blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *listing_entry, bc_trie_t *config, bool listing) { if (tmpl == NULL) return NULL; @@ -221,6 +221,20 @@ blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, bool list current_source = sources; tmp_source = current_source != NULL ? current_source->data : NULL; } + if (0 == strcmp("listing_entry", node->data[0])) { + if (listing_entry == NULL || !listing) { + + // we can just skip anything and walk until the next + // 'endblock' + while (node->type != BLOGC_TEMPLATE_NODE_ENDBLOCK) { + tmp = tmp->next; + node = tmp->data; + } + break; + } + current_source = NULL; + tmp_source = listing_entry; + } else if ((0 == strcmp("listing", node->data[0])) || (0 == strcmp("listing_once", node->data[0]))) { if (!listing) { diff --git a/src/blogc/renderer.h b/src/blogc/renderer.h index 0b2b058..8f2516a 100644 --- a/src/blogc/renderer.h +++ b/src/blogc/renderer.h @@ -18,7 +18,7 @@ char* blogc_format_variable(const char *name, bc_trie_t *global, bc_trie_t *loca bc_slist_t *foreach_var); bc_slist_t* blogc_split_list_variable(const char *name, bc_trie_t *global, bc_trie_t *local); -char* blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, - bool listing); +char* blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *listing_entr, + bc_trie_t *config, bool listing); #endif /* _RENDERER_H */ diff --git a/src/blogc/template-parser.c b/src/blogc/template-parser.c index a53f1aa..0b4eb55 100644 --- a/src/blogc/template-parser.c +++ b/src/blogc/template-parser.c @@ -379,11 +379,19 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err) state = TEMPLATE_BLOCK_END_WHITESPACE_CLEANER; break; } + else if ((current - start == 13) && + (0 == strncmp("listing_entry", src + start, 13))) + { + block_open = true; + end = current; + state = TEMPLATE_BLOCK_END_WHITESPACE_CLEANER; + break; + } } *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, - "Invalid block type. Allowed types are: 'entry', 'listing' " - "and 'listing_once'."); + "Invalid block type. Allowed types are: 'entry', 'listing', " + "'listing_once' and 'listing_entry'."); break; case TEMPLATE_BLOCK_IF_START: |