diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/content-parser.c | 240 | ||||
-rw-r--r-- | src/directives.c | 59 | ||||
-rw-r--r-- | src/directives.h | 31 | ||||
-rw-r--r-- | src/error.c | 3 | ||||
-rw-r--r-- | src/error.h | 1 | ||||
-rw-r--r-- | src/utils/trie.c | 2 |
6 files changed, 316 insertions, 20 deletions
diff --git a/src/content-parser.c b/src/content-parser.c index f5450d6..a9ab9af 100644 --- a/src/content-parser.c +++ b/src/content-parser.c @@ -15,6 +15,7 @@ #include "utils/utils.h" #include "content-parser.h" +#include "directives.h" // this is a half ass implementation of a markdown-like syntax. bugs are // expected. feel free to improve the parser and add new features. @@ -43,7 +44,7 @@ blogc_slugify(const char *str) typedef enum { CONTENT_START_LINE = 1, - CONTENT_EXCERPT, + CONTENT_EXCERPT_OR_DIRECTIVE, CONTENT_EXCERPT_END, CONTENT_HEADER, CONTENT_HEADER_TITLE_START, @@ -64,6 +65,18 @@ typedef enum { CONTENT_ORDERED_LIST_SPACE, CONTENT_ORDERED_LIST_START, CONTENT_ORDERED_LIST_END, + CONTENT_DIRECTIVE_NAME_START, + CONTENT_DIRECTIVE_NAME, + CONTENT_DIRECTIVE_COLON, + CONTENT_DIRECTIVE_ARGUMENT_START, + CONTENT_DIRECTIVE_ARGUMENT, + CONTENT_DIRECTIVE_PARAM_PREFIX_START, + CONTENT_DIRECTIVE_PARAM_PREFIX, + CONTENT_DIRECTIVE_PARAM_KEY_START, + CONTENT_DIRECTIVE_PARAM_KEY, + CONTENT_DIRECTIVE_PARAM_VALUE_START, + CONTENT_DIRECTIVE_PARAM_VALUE, + CONTENT_DIRECTIVE_PARAM_END, CONTENT_PARAGRAPH, CONTENT_PARAGRAPH_END, } blogc_content_parser_state_t; @@ -415,6 +428,11 @@ blogc_content_parse(const char *src, size_t *end_excerpt) char *parsed = NULL; char *slug = NULL; + char *directive_name = NULL; + char *directive_argument = NULL; + char *directive_key = NULL; + b_trie_t *directive_params = NULL; + // this isn't empty because we need some reasonable default value in the // unlikely case that we need to print some line ending before evaluating // the "real" value. @@ -465,11 +483,9 @@ blogc_content_parse(const char *src, size_t *end_excerpt) break; start = current; if (c == '.') { - if (end_excerpt != NULL) { - eend = rv->len; // fuck it - state = CONTENT_EXCERPT; - break; - } + eend = rv->len; // fuck it + state = CONTENT_EXCERPT_OR_DIRECTIVE; + break; } if (c == '#') { header_level = 1; @@ -504,26 +520,29 @@ blogc_content_parse(const char *src, size_t *end_excerpt) state = CONTENT_PARAGRAPH; break; - case CONTENT_EXCERPT: - if (end_excerpt != NULL) { - if (c == '.') - break; - if (c == '\n' || c == '\r') { - state = CONTENT_EXCERPT_END; - break; - } + case CONTENT_EXCERPT_OR_DIRECTIVE: + if (c == '.') + break; + if ((c == ' ' || c == '\t') && current - start == 2) { + state = CONTENT_DIRECTIVE_NAME_START; + if (is_last) + goto para; + break; + } + if (c == '\n' || c == '\r') { + state = CONTENT_EXCERPT_END; + break; } eend = 0; state = CONTENT_PARAGRAPH; break; case CONTENT_EXCERPT_END: - if (end_excerpt != NULL) { - if (c == '\n' || c == '\r') { + if (c == '\n' || c == '\r') { + if (end_excerpt != NULL) *end_excerpt = eend; - state = CONTENT_START_LINE; - break; - } + state = CONTENT_START_LINE; + break; } eend = 0; state = CONTENT_PARAGRAPH_END; @@ -948,6 +967,183 @@ hr: } break; + case CONTENT_DIRECTIVE_NAME_START: + if (is_last) + goto para; + if (c >= 'a' && c <= 'z') { + start2 = current; + state = CONTENT_DIRECTIVE_NAME; + break; + } + state = CONTENT_PARAGRAPH; + break; + + case CONTENT_DIRECTIVE_NAME: + if (is_last) + goto para; + if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_') + break; + if (c == ':') { + end = current; + state = CONTENT_DIRECTIVE_COLON; + break; + } + state = CONTENT_PARAGRAPH; + break; + + case CONTENT_DIRECTIVE_COLON: + if (c == ':') { + free(directive_name); + directive_name = b_strndup(src + start2, end - start2); + state = CONTENT_DIRECTIVE_ARGUMENT_START; + if (is_last) + goto param_end; + break; + } + if (is_last) + goto para; + state = CONTENT_PARAGRAPH; + break; + + case CONTENT_DIRECTIVE_ARGUMENT_START: + if (c == ' ' || c == '\t') { + if (is_last) + goto param_end; + break; + } + if (c == '\n' || c == '\r' || is_last) { + state = CONTENT_DIRECTIVE_PARAM_PREFIX_START; + directive_argument = NULL; + if (is_last) + goto param_end; + else + start2 = current + 1; + break; + } + start2 = current; + state = CONTENT_DIRECTIVE_ARGUMENT; + break; + + case CONTENT_DIRECTIVE_ARGUMENT: + if (c == '\n' || c == '\r' || is_last) { + state = CONTENT_DIRECTIVE_PARAM_PREFIX_START; + end = is_last && c != '\n' && c != '\r' ? src_len : + (real_end != 0 ? real_end : current); + free(directive_argument); + directive_argument = b_strndup(src + start2, end - start2); + if (is_last) + goto param_end; + else + start2 = current + 1; + } + break; + + case CONTENT_DIRECTIVE_PARAM_PREFIX_START: + if (is_last) + goto para; + if (c == ' ' || c == '\t') + break; + prefix = b_strndup(src + start2, current - start2); + state = CONTENT_DIRECTIVE_PARAM_PREFIX; + current--; + break; + + case CONTENT_DIRECTIVE_PARAM_PREFIX: + if (c == ' ' || c == '\t') + break; + if (c == ':' && b_str_starts_with(src + start2, prefix)) { + state = CONTENT_DIRECTIVE_PARAM_KEY_START; + break; + } + state = CONTENT_PARAGRAPH; + if (is_last) + goto para; + break; + + case CONTENT_DIRECTIVE_PARAM_KEY_START: + if (is_last) + goto para; + if (c >= 'a' && c <= 'z') { + start2 = current; + state = CONTENT_DIRECTIVE_PARAM_KEY; + break; + } + state = CONTENT_PARAGRAPH; + break; + + case CONTENT_DIRECTIVE_PARAM_KEY: + if (is_last) + goto para; + if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_') + break; + if (c == ':') { + free(directive_key); + directive_key = b_strndup(src + start2, current - start2); + state = CONTENT_DIRECTIVE_PARAM_VALUE_START; + break; + } + state = CONTENT_PARAGRAPH; + break; + + case CONTENT_DIRECTIVE_PARAM_VALUE_START: + if (is_last) + goto para; + if (c == ' ' || c == '\t') + break; + start2 = current; + state = CONTENT_DIRECTIVE_PARAM_VALUE; + break; + + case CONTENT_DIRECTIVE_PARAM_VALUE: + if (c == '\n' || c == '\r' || is_last) { + state = CONTENT_DIRECTIVE_PARAM_END; + end = is_last && c != '\n' && c != '\r' ? src_len : + (real_end != 0 ? real_end : current); + if (directive_params == NULL) + directive_params = b_trie_new(free); + b_trie_insert(directive_params, directive_key, + b_strndup(src + start2, end - start2)); + free(directive_key); + directive_key = NULL; + if (!is_last) + start2 = current + 1; + } + if (!is_last) + break; + + case CONTENT_DIRECTIVE_PARAM_END: +param_end: + if (c == '\n' || c == '\r' || is_last) { + // FIXME: handle errors in the rest of the parser. + blogc_error_t *err = NULL; + char *rv_d = blogc_directive_loader(directive_name, + directive_argument, directive_params, &err); + blogc_error_print(err); + if (rv_d != NULL) + b_string_append(rv, rv_d); + free(rv_d); + state = CONTENT_START_LINE; + start = current; + free(directive_name); + directive_name = NULL; + free(directive_argument); + directive_argument = NULL; + b_trie_free(directive_params); + directive_params = NULL; + free(prefix); + prefix = NULL; + break; + } + if (c == ' ' || c == '\t') { + start2 = current; + state = CONTENT_DIRECTIVE_PARAM_PREFIX; + break; + } + state = CONTENT_PARAGRAPH; + if (is_last) + goto para; + break; + case CONTENT_PARAGRAPH: if (c == '\n' || c == '\r' || is_last) { state = CONTENT_PARAGRAPH_END; @@ -987,5 +1183,11 @@ para: current++; } + free(directive_name); + free(directive_argument); + free(directive_key); + b_trie_free(directive_params); + free(prefix); + return b_string_free(rv, false); } diff --git a/src/directives.c b/src/directives.c new file mode 100644 index 0000000..829a736 --- /dev/null +++ b/src/directives.c @@ -0,0 +1,59 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif /* HAVE_CONFIG_H */ + +#include <string.h> + +#include "utils/utils.h" +#include "directives.h" +#include "error.h" + + +blogc_directive_t registry[] = { + {"youtube", blogc_directive_youtube}, + {NULL, NULL}, +}; + + +char* +blogc_directive_loader(const char *name, const char *argument, b_trie_t *params, + blogc_error_t **err) +{ + for (unsigned int i = 0; registry[i].name != NULL; i++) + if (0 == strcmp(name, registry[i].name)) + return registry[i].callback(argument, params, err); + return NULL; +} + + +char* +blogc_directive_youtube(const char *argument, b_trie_t *params, blogc_error_t **err) +{ + if (err != NULL && *err != NULL) + return NULL; + if (argument == NULL) { + *err = blogc_error_new_printf(BLOGC_WARNING_CONTENT_PARSER, + "youtube: Invalid video ID: %s", argument); + return NULL; + } + + char *width = b_trie_lookup(params, "width"); + char *height = b_trie_lookup(params, "height"); + + // using default 16:9 sizes provided by youtube as of 2015-11-04 + return b_strdup_printf( + "<iframe width=\"%s\" height=\"%s\" " + "src=\"https://www.youtube.com/embed/%s\" frameborder=\"0\" " + "allowfullscreen></iframe>\n", + width == NULL ? "560" : width, + height == NULL ? "315" : height, + argument); +} diff --git a/src/directives.h b/src/directives.h new file mode 100644 index 0000000..cbfd213 --- /dev/null +++ b/src/directives.h @@ -0,0 +1,31 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifndef _DIRECTIVES_H +#define _DIRECTIVES_H + +#include "utils/utils.h" +#include "error.h" + +typedef char* (*blogc_directive_func_t)(const char *argument, b_trie_t *params, + blogc_error_t **err); + +typedef struct { + const char *name; + blogc_directive_func_t callback; +} blogc_directive_t; + +char* blogc_directive_loader(const char *name, const char *argument, + b_trie_t *params, blogc_error_t **err); + + +// built-in directives (that are everything we support right now +char* blogc_directive_youtube(const char *argument, b_trie_t *params, + blogc_error_t **err); + +#endif /* _DIRECTIVES_H */ diff --git a/src/error.c b/src/error.c index 28396f8..e472d1f 100644 --- a/src/error.c +++ b/src/error.c @@ -125,6 +125,9 @@ blogc_error_print(blogc_error_t *err) case BLOGC_WARNING_DATETIME_PARSER: fprintf(stderr, "blogc: warning: datetime: %s\n", err->msg); break; + case BLOGC_WARNING_CONTENT_PARSER: + fprintf(stderr, "blogc: warning: content: %s\n", err->msg); + break; default: fprintf(stderr, "blogc: error: %s\n", err->msg); } diff --git a/src/error.h b/src/error.h index 845a316..02ccc96 100644 --- a/src/error.h +++ b/src/error.h @@ -17,6 +17,7 @@ typedef enum { BLOGC_ERROR_TEMPLATE_PARSER, BLOGC_ERROR_LOADER, BLOGC_WARNING_DATETIME_PARSER, + BLOGC_WARNING_CONTENT_PARSER, } blogc_error_type_t; typedef struct { diff --git a/src/utils/trie.c b/src/utils/trie.c index 72a62f6..db64e1d 100644 --- a/src/utils/trie.c +++ b/src/utils/trie.c @@ -110,7 +110,7 @@ clean: void* b_trie_lookup(b_trie_t *trie, const char *key) { - if (trie->root == NULL || key == NULL) + if (trie == NULL || trie->root == NULL || key == NULL) return NULL; b_trie_node_t *parent = trie->root; |