diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-10-12 02:31:02 -0300 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-10-12 02:31:02 -0300 |
commit | 701ffe18c2f256245b4d016daafca208839a8f63 (patch) | |
tree | 6da1c4a4e5161bfa574e0c0149bb0e78346ba02d | |
parent | 9aec1f36e80af5c06cb690bda125c628e26e5601 (diff) | |
download | blogc-701ffe18c2f256245b4d016daafca208839a8f63.tar.gz blogc-701ffe18c2f256245b4d016daafca208839a8f63.tar.bz2 blogc-701ffe18c2f256245b4d016daafca208839a8f63.zip |
content-parser: added id attributes to headers
the id is a slugified version of the (unparsed) header content.
-rw-r--r-- | src/content-parser.c | 32 | ||||
-rw-r--r-- | src/content-parser.h | 1 | ||||
-rw-r--r-- | tests/check_content_parser.c | 62 | ||||
-rw-r--r-- | tests/check_source_parser.c | 12 |
4 files changed, 82 insertions, 25 deletions
diff --git a/src/content-parser.c b/src/content-parser.c index 511d09e..3b661a8 100644 --- a/src/content-parser.c +++ b/src/content-parser.c @@ -20,6 +20,27 @@ // expected. feel free to improve the parser and add new features. +char* +blogc_slugify(const char *str) +{ + if (str == NULL) + return NULL; + char *new_str = b_strdup(str); + int diff = 'a' - 'A'; // just to avoid magic numbers + for (unsigned int i = 0; new_str[i] != '\0'; i++) { + if (new_str[i] >= 'a' && new_str[i] <= 'z') + continue; + if (new_str[i] >= '0' && new_str[i] <= '9') + continue; + if (new_str[i] >= 'A' && new_str[i] <= 'Z') + new_str[i] += diff; + else + new_str[i] = '-'; + } + return new_str; +} + + typedef enum { CONTENT_START_LINE = 1, CONTENT_EXCERPT, @@ -389,6 +410,7 @@ blogc_content_parse(const char *src, size_t *end_excerpt) char *tmp = NULL; char *tmp2 = NULL; char *parsed = NULL; + char *slug = NULL; char d = '\0'; @@ -501,8 +523,14 @@ blogc_content_parse(const char *src, size_t *end_excerpt) end = is_last && c != '\n' && c != '\r' ? src_len : current; tmp = b_strndup(src + start, end - start); parsed = blogc_content_parse_inline(tmp); - b_string_append_printf(rv, "<h%d>%s</h%d>\n", header_level, - parsed, header_level); + slug = blogc_slugify(tmp); + if (slug == NULL) + b_string_append_printf(rv, "<h%d>%s</h%d>\n", + header_level, parsed, header_level); + else + b_string_append_printf(rv, "<h%d id=\"%s\">%s</h%d>\n", + header_level, slug, parsed, header_level); + free(slug); free(parsed); parsed = NULL; free(tmp); diff --git a/src/content-parser.h b/src/content-parser.h index 4579719..2f6b8b9 100644 --- a/src/content-parser.h +++ b/src/content-parser.h @@ -12,6 +12,7 @@ #include <stdlib.h> #include <stdbool.h> +char* blogc_slugify(const char *str); char* blogc_content_parse_inline(const char *src); bool blogc_is_ordered_list_item(const char *str, size_t prefix_len); char* blogc_content_parse(const char *src, size_t *end_excerpt); diff --git a/tests/check_content_parser.c b/tests/check_content_parser.c index b9f83b0..f1430ac 100644 --- a/tests/check_content_parser.c +++ b/tests/check_content_parser.c @@ -20,6 +20,33 @@ static void +test_slugify(void **state) +{ + char *s = blogc_slugify("bola"); + assert_string_equal(s, "bola"); + free(s); + s = blogc_slugify("bola o"); + assert_string_equal(s, "bola-o"); + free(s); + s = blogc_slugify("Bola O"); + assert_string_equal(s, "bola-o"); + free(s); + s = blogc_slugify("bola 0"); + assert_string_equal(s, "bola-0"); + free(s); + s = blogc_slugify("bola () o"); + assert_string_equal(s, "bola-----o"); + free(s); + s = blogc_slugify("Bola O"); + assert_string_equal(s, "bola-o"); + free(s); + s = blogc_slugify("bolaço"); + assert_string_equal(s, "bola--o"); + free(s); +} + + +static void test_is_ordered_list_item(void **state) { assert_true(blogc_is_ordered_list_item("1.bola", 2)); @@ -80,12 +107,12 @@ test_content_parse(void **state) assert_non_null(html); assert_int_equal(l, 0); assert_string_equal(html, - "<h1>um</h1>\n" - "<h2>dois</h2>\n" - "<h3>tres</h3>\n" - "<h4>quatro</h4>\n" - "<h5>cinco</h5>\n" - "<h6>seis</h6>\n" + "<h1 id=\"um\">um</h1>\n" + "<h2 id=\"dois\">dois</h2>\n" + "<h3 id=\"tres\">tres</h3>\n" + "<h4 id=\"quatro\">quatro</h4>\n" + "<h5 id=\"cinco\">cinco</h5>\n" + "<h6 id=\"seis\">seis</h6>\n" "<p>bola\n" "chunda</p>\n" "<blockquote><p>bola <br />\n" @@ -129,9 +156,9 @@ test_content_parse_with_excerpt(void **state) "guda\n" "lol", &l); assert_non_null(html); - assert_int_equal(l, 28); + assert_int_equal(l, 38); assert_string_equal(html, - "<h1>test</h1>\n" + "<h1 id=\"test\">test</h1>\n" "<p>chunda</p>\n" "<p>guda\n" "lol</p>\n"); @@ -147,9 +174,9 @@ test_content_parse_with_excerpt(void **state) "guda\n" "lol", &l); assert_non_null(html); - assert_int_equal(l, 28); + assert_int_equal(l, 38); assert_string_equal(html, - "<h1>test</h1>\n" + "<h1 id=\"test\">test</h1>\n" "<p>chunda</p>\n" "<p>guda\n" "lol</p>\n"); @@ -162,11 +189,11 @@ test_content_parse_header(void **state) { char *html = blogc_content_parse("## bola", NULL); assert_non_null(html); - assert_string_equal(html, "<h2>bola</h2>\n"); + assert_string_equal(html, "<h2 id=\"bola\">bola</h2>\n"); free(html); html = blogc_content_parse("## bola\n", NULL); assert_non_null(html); - assert_string_equal(html, "<h2>bola</h2>\n"); + assert_string_equal(html, "<h2 id=\"bola\">bola</h2>\n"); free(html); html = blogc_content_parse( "bola\n" @@ -177,7 +204,7 @@ test_content_parse_header(void **state) assert_non_null(html); assert_string_equal(html, "<p>bola</p>\n" - "<h2>bola</h2>\n" + "<h2 id=\"bola\">bola</h2>\n" "<p>guda</p>\n"); free(html); } @@ -526,7 +553,7 @@ test_content_parse_invalid_excerpt(void **state) assert_non_null(html); assert_int_equal(l, 0); assert_string_equal(html, - "<h1>test</h1>\n" + "<h1 id=\"test\">test</h1>\n" "<p>chunda\n" "..</p>\n" "<p>guda\n" @@ -544,7 +571,7 @@ test_content_parse_invalid_excerpt(void **state) assert_non_null(html); assert_int_equal(l, 0); assert_string_equal(html, - "<h1>test</h1>\n" + "<h1 id=\"test\">test</h1>\n" "<p>chunda</p>\n" "<p>...\n" "guda\n" @@ -561,7 +588,7 @@ test_content_parse_invalid_excerpt(void **state) assert_non_null(html); assert_int_equal(l, 0); assert_string_equal(html, - "<h1>test</h1>\n" + "<h1 id=\"test\">test</h1>\n" "<p>chunda..</p>\n" "<p>guda\n" "lol</p>\n"); @@ -577,7 +604,7 @@ test_content_parse_invalid_excerpt(void **state) assert_non_null(html); assert_int_equal(l, 0); assert_string_equal(html, - "<h1>test</h1>\n" + "<h1 id=\"test\">test</h1>\n" "<p>chunda</p>\n" "<p>...guda\n" "lol</p>\n"); @@ -1119,6 +1146,7 @@ int main(void) { const UnitTest tests[] = { + unit_test(test_slugify), unit_test(test_is_ordered_list_item), unit_test(test_content_parse), unit_test(test_content_parse_with_excerpt), diff --git a/tests/check_source_parser.c b/tests/check_source_parser.c index 94f75c9..145ba30 100644 --- a/tests/check_source_parser.c +++ b/tests/check_source_parser.c @@ -38,10 +38,10 @@ test_source_parse(void **state) 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, "EXCERPT"), - "<h1>This is a test</h1>\n" + "<h1 id=\"this-is-a-test\">This is a test</h1>\n" "<p>bola</p>\n"); assert_string_equal(b_trie_lookup(source, "CONTENT"), - "<h1>This is a test</h1>\n" + "<h1 id=\"this-is-a-test\">This is a test</h1>\n" "<p>bola</p>\n"); assert_string_equal(b_trie_lookup(source, "RAW_CONTENT"), "# This is a test\n" @@ -71,10 +71,10 @@ test_source_parse_with_spaces(void **state) 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, "EXCERPT"), - "<h1>This is a test</h1>\n" + "<h1 id=\"this-is-a-test\">This is a test</h1>\n" "<p>bola</p>\n"); assert_string_equal(b_trie_lookup(source, "CONTENT"), - "<h1>This is a test</h1>\n" + "<h1 id=\"this-is-a-test\">This is a test</h1>\n" "<p>bola</p>\n"); assert_string_equal(b_trie_lookup(source, "RAW_CONTENT"), "# This is a test\n" @@ -107,10 +107,10 @@ test_source_parse_with_excerpt(void **state) 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, "EXCERPT"), - "<h1>This is a test</h1>\n" + "<h1 id=\"this-is-a-test\">This is a test</h1>\n" "<p>bola</p>\n"); assert_string_equal(b_trie_lookup(source, "CONTENT"), - "<h1>This is a test</h1>\n" + "<h1 id=\"this-is-a-test\">This is a test</h1>\n" "<p>bola</p>\n" "<p>guda\n" "yay</p>\n"); |