From e4eac77887cd9c1dd010057d441691d030a851df Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Wed, 10 Jun 2015 23:14:50 -0300 Subject: content-parser: added "automatic" links --- src/content-parser.c | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) (limited to 'src') 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, "%s", 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++; } -- cgit v1.2.3-18-g5258