aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/source-parser.c57
-rw-r--r--tests/check_loader.c2
-rw-r--r--tests/check_source_parser.c89
3 files changed, 140 insertions, 8 deletions
diff --git a/src/source-parser.c b/src/source-parser.c
index f0fca29..9e1f01d 100644
--- a/src/source-parser.c
+++ b/src/source-parser.c
@@ -27,6 +27,9 @@ typedef enum {
SOURCE_SEPARATOR,
SOURCE_CONTENT_START,
SOURCE_CONTENT,
+ SOURCE_EXCERPT_SEPARATOR,
+ SOURCE_CONTENT2_START,
+ SOURCE_CONTENT2,
} blogc_source_parser_state_t;
@@ -41,6 +44,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err)
char *key = NULL;
char *tmp = NULL;
+ char *tmp2 = NULL;
b_trie_t *rv = b_trie_new(free);
blogc_source_parser_state_t state = SOURCE_START;
@@ -145,10 +149,53 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err)
break;
case SOURCE_CONTENT:
+ if (c == '.') {
+ state = SOURCE_EXCERPT_SEPARATOR;
+ tmp = b_strndup(src + start, current - start);
+ b_trie_insert(rv, "EXCERPT", blogc_content_parse(tmp));
+ free(tmp);
+ tmp = NULL;
+ break;
+ }
if (current == (src_len - 1)) {
tmp = b_strndup(src + start, src_len - start);
- b_trie_insert(rv, "RAW_CONTENT", tmp);
- b_trie_insert(rv, "CONTENT", blogc_content_parse(tmp));
+ b_trie_insert(rv, "EXCERPT", blogc_content_parse(tmp));
+ free(tmp);
+ tmp = NULL;
+ }
+ break;
+
+ case SOURCE_EXCERPT_SEPARATOR:
+ if (c == '.')
+ break;
+ if (c == '\n' || c == '\r') {
+ state = SOURCE_CONTENT2_START;
+ break;
+ }
+ *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len,
+ current,
+ "Invalid excerpt separator. Must be more than one '.' characters.");
+ break;
+
+ case SOURCE_CONTENT2_START:
+ start = current;
+ state = SOURCE_CONTENT2;
+ break;
+
+ case SOURCE_CONTENT2:
+ if (current == (src_len - 1)) {
+ tmp = b_strndup(src + start, src_len - start);
+ tmp2 = b_strdup_printf("%s\n%s",
+ b_trie_lookup(rv, "EXCERPT"), tmp);
+ free(tmp);
+ tmp = NULL;
+ b_trie_insert(rv, "CONTENT", blogc_content_parse(tmp2));
+ free(tmp2);
+ tmp2 = NULL;
+ }
+ if (c == '.') {
+ state = SOURCE_EXCERPT_SEPARATOR;
+ break;
}
break;
}
@@ -159,6 +206,9 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err)
current++;
}
+ if (b_trie_lookup(rv, "CONTENT") == NULL)
+ b_trie_insert(rv, "CONTENT", b_strdup(b_trie_lookup(rv, "EXCERPT")));
+
if (*err == NULL && b_trie_size(rv) == 0) {
// ok, nothing found in the config trie, but no error set either.
@@ -187,6 +237,9 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err)
case SOURCE_SEPARATOR:
case SOURCE_CONTENT_START:
case SOURCE_CONTENT:
+ case SOURCE_EXCERPT_SEPARATOR:
+ case SOURCE_CONTENT2_START:
+ case SOURCE_CONTENT2:
break; // won't happen, and if even happen, shouldn't be fatal
}
}
diff --git a/tests/check_loader.c b/tests/check_loader.c
index 865d598..2d16dba 100644
--- a/tests/check_loader.c
+++ b/tests/check_loader.c
@@ -117,7 +117,7 @@ test_source_parse_from_file(void **state)
assert_string_equal(b_trie_lookup(t, "ASD"), "123");
assert_string_equal(b_trie_lookup(t, "FILENAME"), "bola");
assert_string_equal(b_trie_lookup(t, "CONTENT"), "<p>bola</p>\n");
- assert_string_equal(b_trie_lookup(t, "RAW_CONTENT"), "bola");
+ assert_string_equal(b_trie_lookup(t, "EXCERPT"), "<p>bola</p>\n");
b_trie_free(t);
}
diff --git a/tests/check_source_parser.c b/tests/check_source_parser.c
index 0cec364..c33b4c1 100644
--- a/tests/check_source_parser.c
+++ b/tests/check_source_parser.c
@@ -40,10 +40,42 @@ test_source_parse(void **state)
assert_string_equal(b_trie_lookup(source, "CONTENT"),
"<h1>This is a test</h1>\n"
"<p>bola</p>\n");
- assert_string_equal(b_trie_lookup(source, "RAW_CONTENT"),
+ assert_string_equal(b_trie_lookup(source, "EXCERPT"),
+ "<h1>This is a test</h1>\n"
+ "<p>bola</p>\n");
+ b_trie_free(source);
+}
+
+
+static void
+test_source_parse_with_excerpt(void **state)
+{
+ const char *a =
+ "VAR1: asd asd\n"
+ "VAR2: 123chunda\n"
+ "----------\n"
"# This is a test\n"
"\n"
- "bola\n");
+ "bola\n"
+ "\n"
+ "...\n"
+ "guda\n"
+ "bola\n";
+ blogc_error_t *err = NULL;
+ b_trie_t *source = blogc_source_parse(a, strlen(a), &err);
+ assert_null(err);
+ assert_non_null(source);
+ assert_int_equal(b_trie_size(source), 4);
+ assert_string_equal(b_trie_lookup(source, "VAR1"), "asd asd");
+ assert_string_equal(b_trie_lookup(source, "VAR2"), "123chunda");
+ assert_string_equal(b_trie_lookup(source, "CONTENT"),
+ "<h1>This is a test</h1>\n"
+ "<p>bola</p>\n"
+ "<p>guda\n"
+ "bola</p>\n");
+ assert_string_equal(b_trie_lookup(source, "EXCERPT"),
+ "<h1>This is a test</h1>\n"
+ "<p>bola</p>\n");
b_trie_free(source);
}
@@ -70,10 +102,37 @@ test_source_parse_with_spaces(void **state)
assert_string_equal(b_trie_lookup(source, "CONTENT"),
"<h1>This is a test</h1>\n"
"<p>bola</p>\n");
- assert_string_equal(b_trie_lookup(source, "RAW_CONTENT"),
+ assert_string_equal(b_trie_lookup(source, "EXCERPT"),
+ "<h1>This is a test</h1>\n"
+ "<p>bola</p>\n");
+ b_trie_free(source);
+}
+
+
+static void
+test_source_parse_with_spaces_and_excerpt(void **state)
+{
+ const char *a =
+ "\n \n"
+ "VAR1: chunda \t \n"
+ "\n\n"
+ "BOLA: guda\n"
+ "----------\n"
"# This is a test\n"
- "\n"
- "bola\n");
+ "..\n"
+ "bola\n";
+ blogc_error_t *err = NULL;
+ b_trie_t *source = blogc_source_parse(a, strlen(a), &err);
+ assert_null(err);
+ assert_non_null(source);
+ assert_int_equal(b_trie_size(source), 4);
+ assert_string_equal(b_trie_lookup(source, "VAR1"), "chunda");
+ assert_string_equal(b_trie_lookup(source, "BOLA"), "guda");
+ assert_string_equal(b_trie_lookup(source, "CONTENT"),
+ "<h1>This is a test</h1>\n"
+ "<p>bola</p>\n");
+ assert_string_equal(b_trie_lookup(source, "EXCERPT"),
+ "<h1>This is a test</h1>\n");
b_trie_free(source);
}
@@ -375,12 +434,31 @@ test_source_parse_invalid_separator(void **state)
}
+static void
+test_source_parse_invalid_excerpt_separator(void **state)
+{
+ const char *a = "BOLA: asd\n---\nbola\n...#";
+ blogc_error_t *err = NULL;
+ b_trie_t *source = blogc_source_parse(a, strlen(a), &err);
+ assert_null(source);
+ assert_non_null(err);
+ assert_int_equal(err->type, BLOGC_ERROR_SOURCE_PARSER);
+ assert_string_equal(err->msg,
+ "Invalid excerpt separator. Must be more than one '.' characters.\n"
+ "Error occurred near to '#'");
+ blogc_error_free(err);
+ b_trie_free(source);
+}
+
+
int
main(void)
{
const UnitTest tests[] = {
unit_test(test_source_parse),
+ unit_test(test_source_parse_with_excerpt),
unit_test(test_source_parse_with_spaces),
+ unit_test(test_source_parse_with_spaces_and_excerpt),
unit_test(test_source_parse_config_empty),
unit_test(test_source_parse_config_invalid_key),
unit_test(test_source_parse_config_no_key),
@@ -399,6 +477,7 @@ main(void)
unit_test(test_source_parse_config_reserved_name10),
unit_test(test_source_parse_config_value_no_line_ending),
unit_test(test_source_parse_invalid_separator),
+ unit_test(test_source_parse_invalid_excerpt_separator),
};
return run_tests(tests);
}