aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2015-06-10 23:14:50 -0300
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2015-06-10 23:14:50 -0300
commite4eac77887cd9c1dd010057d441691d030a851df (patch)
treebe15e5a2f0e68ab838a2df33e9a9e1b43a6a6777 /src
parentd096b0f56c67836dceba8478ff2f9fe3cc09aaa8 (diff)
downloadblogc-e4eac77887cd9c1dd010057d441691d030a851df.tar.gz
blogc-e4eac77887cd9c1dd010057d441691d030a851df.tar.bz2
blogc-e4eac77887cd9c1dd010057d441691d030a851df.zip
content-parser: added "automatic" links
Diffstat (limited to 'src')
-rw-r--r--src/content-parser.c44
1 files changed, 29 insertions, 15 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++;
}