aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/content-parser.c10
-rw-r--r--src/directives.c32
-rw-r--r--src/directives.h15
-rw-r--r--tests/check_content_parser.c18
4 files changed, 46 insertions, 29 deletions
diff --git a/src/content-parser.c b/src/content-parser.c
index 8bdd861..8e98405 100644
--- a/src/content-parser.c
+++ b/src/content-parser.c
@@ -1118,8 +1118,14 @@ 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_directive_ctx_t *ctx = b_malloc(
+ sizeof(blogc_directive_ctx_t));
+ ctx->name = directive_name;
+ ctx->argument = directive_argument;
+ ctx->params = directive_params;
+ ctx->eol = line_ending;
+ char *rv_d = blogc_directive_loader(ctx, &err);
+ free(ctx);
blogc_error_print(err);
if (rv_d != NULL)
b_string_append(rv, rv_d);
diff --git a/src/directives.c b/src/directives.c
index 829a736..ed8b568 100644
--- a/src/directives.c
+++ b/src/directives.c
@@ -24,36 +24,40 @@ blogc_directive_t registry[] = {
char*
-blogc_directive_loader(const char *name, const char *argument, b_trie_t *params,
- blogc_error_t **err)
+blogc_directive_loader(blogc_directive_ctx_t *ctx, 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);
+ if (ctx == NULL)
+ return NULL;
+ for (unsigned int i = 0; registry[i].name != NULL; i++) {
+ if (0 == strcmp(ctx->name, registry[i].name)) {
+ if (err != NULL && *err != NULL) {
+ return NULL;
+ }
+ return registry[i].callback(ctx, err);
+ }
+ }
return NULL;
}
char*
-blogc_directive_youtube(const char *argument, b_trie_t *params, blogc_error_t **err)
+blogc_directive_youtube(blogc_directive_ctx_t *ctx, blogc_error_t **err)
{
- if (err != NULL && *err != NULL)
- return NULL;
- if (argument == NULL) {
+ if (ctx->argument == NULL) {
*err = blogc_error_new_printf(BLOGC_WARNING_CONTENT_PARSER,
- "youtube: Invalid video ID: %s", argument);
+ "youtube: Invalid video ID: %s", ctx->argument);
return NULL;
}
- char *width = b_trie_lookup(params, "width");
- char *height = b_trie_lookup(params, "height");
+ char *width = b_trie_lookup(ctx->params, "width");
+ char *height = b_trie_lookup(ctx->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",
+ "allowfullscreen></iframe>%s",
width == NULL ? "560" : width,
height == NULL ? "315" : height,
- argument);
+ ctx->argument, ctx->eol);
}
diff --git a/src/directives.h b/src/directives.h
index cbfd213..dd27f03 100644
--- a/src/directives.h
+++ b/src/directives.h
@@ -12,7 +12,14 @@
#include "utils/utils.h"
#include "error.h"
-typedef char* (*blogc_directive_func_t)(const char *argument, b_trie_t *params,
+typedef struct {
+ const char *name;
+ const char *argument;
+ b_trie_t *params;
+ const char *eol;
+} blogc_directive_ctx_t;
+
+typedef char* (*blogc_directive_func_t)(blogc_directive_ctx_t *ctx,
blogc_error_t **err);
typedef struct {
@@ -20,12 +27,10 @@ typedef struct {
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);
+char* blogc_directive_loader(blogc_directive_ctx_t *ctx, 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);
+char* blogc_directive_youtube(blogc_directive_ctx_t *ctx, blogc_error_t **err);
#endif /* _DIRECTIVES_H */
diff --git a/tests/check_content_parser.c b/tests/check_content_parser.c
index 86ff1d0..817edba 100644
--- a/tests/check_content_parser.c
+++ b/tests/check_content_parser.c
@@ -16,6 +16,7 @@
#include <cmocka.h>
#include <string.h>
#include "../src/content-parser.h"
+#include "../src/directives.h"
#include "../src/utils/utils.h"
@@ -1015,21 +1016,22 @@ test_content_parse_ordered_list_crlf(void **state)
char*
-__wrap_blogc_directive_loader(const char *name, const char *argument,
- b_trie_t *params)
+__wrap_blogc_directive_loader(blogc_directive_ctx_t *ctx, blogc_error_t **err)
{
- assert_string_equal(name, mock_type(const char*));
+ assert_non_null(err);
+ assert_null(*err);
+ assert_string_equal(ctx->name, mock_type(const char*));
const char *arg = mock_type(const char*);
if (arg == NULL)
- assert_null(argument);
+ assert_null(ctx->argument);
else
- assert_string_equal(argument, arg);
- assert_int_equal(b_trie_size(params), mock_type(unsigned int));
+ assert_string_equal(ctx->argument, arg);
+ assert_int_equal(b_trie_size(ctx->params), mock_type(unsigned int));
- for (unsigned int i = 0; i < b_trie_size(params); i++) {
+ for (unsigned int i = 0; i < b_trie_size(ctx->params); i++) {
const char *key = mock_type(const char*);
const char *value = mock_type(const char*);
- assert_string_equal(b_trie_lookup(params, key), value);
+ assert_string_equal(b_trie_lookup(ctx->params, key), value);
}
return b_strdup("CHUNDA\n");