aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/content-parser.c44
-rw-r--r--tests/check_content_parser.c35
2 files changed, 62 insertions, 17 deletions
diff --git a/src/content-parser.c b/src/content-parser.c
index 111109e..aa76777 100644
--- a/src/content-parser.c
+++ b/src/content-parser.c
@@ -51,7 +51,9 @@ typedef enum {
LINK_IMAGE,
LINK_TEXT,
LINK_TEXT_CLOSE,
- LINK_URL
+ LINK_URL,
+ LINK_AUTO,
+ LINK_AUTO_CLOSE,
} blogc_content_parser_link_state_t;
@@ -65,6 +67,7 @@ blogc_content_parse_inline(const char *src)
size_t current = 0;
size_t start = 0;
size_t start_state = 0;
+ size_t end = 0;
b_string_t *rv = b_string_new();
@@ -196,6 +199,11 @@ blogc_content_parse_inline(const char *src)
break;
}
if (state == LINK_TEXT) {
+ if (current == start) {
+ start = current + 1;
+ state = LINK_AUTO;
+ break;
+ }
open_bracket++;
break;
}
@@ -206,6 +214,21 @@ blogc_content_parse_inline(const char *src)
b_string_append_c(rv, c);
break;
}
+ if (state == LINK_AUTO) {
+ end = current;
+ state = LINK_AUTO_CLOSE;
+ break;
+ }
+ if (state == LINK_AUTO_CLOSE) {
+ state = LINK_CLOSED;
+ tmp = b_strndup(src + start, end - start);
+ b_string_append_printf(rv, "<a href=\"%s\">%s</a>", tmp, tmp);
+ end = 0;
+ free(tmp);
+ tmp = NULL;
+ is_image = false;
+ break;
+ }
if (state == LINK_TEXT) {
if (open_bracket-- == 0) {
state = LINK_TEXT_CLOSE;
@@ -315,20 +338,11 @@ blogc_content_parse_inline(const char *src)
}
if (is_last && state != LINK_CLOSED) {
- if (is_image) {
- b_string_append_c(rv, src[start_state]);
- if (start_state < (src_len - 1))
- b_string_append_c(rv, src[start_state + 1]);
- current = start_state + 2;
- is_image = false;
- }
- else {
- b_string_append_c(rv, src[start_state]);
- current = start_state + 1;
- }
- state = LINK_CLOSED;
- start_state = 0;
- continue;
+ b_string_append_c(rv, src[start_state]);
+ tmp = blogc_content_parse_inline(src + start_state + 1);
+ b_string_append(rv, tmp);
+ free(tmp);
+ tmp = NULL;
}
current++;
}
diff --git a/tests/check_content_parser.c b/tests/check_content_parser.c
index d549de9..9b1d318 100644
--- a/tests/check_content_parser.c
+++ b/tests/check_content_parser.c
@@ -609,13 +609,13 @@ test_content_parse_inline(void **state)
{
char *html = blogc_content_parse_inline(
"**bola***asd* [![lol](http://google.com/lol.png) **lol** "
- "\\[asd\\]\\(qwe\\)](http://google.com) ``chunda``");
+ "\\[asd\\]\\(qwe\\)](http://google.com) ``chunda`` [[bola]]");
assert_non_null(html);
assert_string_equal(html,
"<strong>bola</strong><em>asd</em> "
"<a href=\"http://google.com\"><img src=\"http://google.com/lol.png\" "
"alt=\"lol\"> <strong>lol</strong> [asd](qwe)</a> "
- "<code>chunda</code>");
+ "<code>chunda</code> <a href=\"bola\">bola</a>");
free(html);
html = blogc_content_parse_inline("*bola*");
assert_non_null(html);
@@ -781,6 +781,36 @@ test_content_parse_inline_link(void **state)
void
+test_content_parse_inline_link_auto(void **state)
+{
+ char *html = blogc_content_parse_inline("[[guda]]");
+ assert_non_null(html);
+ assert_string_equal(html, "<a href=\"guda\">guda</a>");
+ free(html);
+ html = blogc_content_parse_inline("[[guda]]\n");
+ assert_non_null(html);
+ assert_string_equal(html, "<a href=\"guda\">guda</a>\n");
+ free(html);
+ html = blogc_content_parse_inline("[[guda]asd]");
+ assert_non_null(html);
+ assert_string_equal(html, "<a href=\"guda\">guda</a>");
+ free(html);
+ html = blogc_content_parse_inline("[[guda]asd]\n");
+ assert_non_null(html);
+ assert_string_equal(html, "<a href=\"guda\">guda</a>\n");
+ free(html);
+ html = blogc_content_parse_inline("[[guda]asd");
+ assert_non_null(html);
+ assert_string_equal(html, "[[guda]asd");
+ free(html);
+ html = blogc_content_parse_inline("[[guda]asd\n");
+ assert_non_null(html);
+ assert_string_equal(html, "[[guda]asd\n");
+ free(html);
+}
+
+
+void
test_content_parse_inline_image(void **state)
{
char *html = blogc_content_parse_inline("![bola](http://example.org/)");
@@ -902,6 +932,7 @@ main(void)
unit_test(test_content_parse_inline_strong),
unit_test(test_content_parse_inline_code),
unit_test(test_content_parse_inline_link),
+ unit_test(test_content_parse_inline_link_auto),
unit_test(test_content_parse_inline_image),
unit_test(test_content_parse_inline_line_break),
};