diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-11-04 01:31:20 -0200 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-11-04 01:31:20 -0200 |
commit | 5be9458de0fe21aaebd43cee6fd0c084b4464a68 (patch) | |
tree | 6f30cc155b26ed11b808b3d02c8d9c973d4e4593 /src | |
parent | 1ed6d31c8f97c71fdaa2eef8b575ba14d1437a33 (diff) | |
download | blogc-5be9458de0fe21aaebd43cee6fd0c084b4464a68.tar.gz blogc-5be9458de0fe21aaebd43cee6fd0c084b4464a68.tar.bz2 blogc-5be9458de0fe21aaebd43cee6fd0c084b4464a68.zip |
content-parser: implemented youtube directive
Diffstat (limited to 'src')
-rw-r--r-- | src/directives.c | 41 | ||||
-rw-r--r-- | src/directives.h | 13 | ||||
-rw-r--r-- | src/utils/trie.c | 2 |
3 files changed, 53 insertions, 3 deletions
diff --git a/src/directives.c b/src/directives.c index 5ff3b4d..829a736 100644 --- a/src/directives.c +++ b/src/directives.c @@ -10,13 +10,50 @@ #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) { - // TODO: implement me! - return b_strdup("TODO\n"); + 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 index d03102a..cbfd213 100644 --- a/src/directives.h +++ b/src/directives.h @@ -12,7 +12,20 @@ #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/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; |