aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2015-05-04 22:34:37 -0300
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2015-05-04 22:34:37 -0300
commitaf37850094238ae94ee4a48588a8490a210f9ad6 (patch)
treeca1a254f08d829d5a358bc7d3449c247fba0153f /src
parent6c36460f20548850b0704a462dd8281c7f5868a7 (diff)
downloadblogc-af37850094238ae94ee4a48588a8490a210f9ad6.tar.gz
blogc-af37850094238ae94ee4a48588a8490a210f9ad6.tar.bz2
blogc-af37850094238ae94ee4a48588a8490a210f9ad6.zip
content-parser: minor fixes. added tests
Diffstat (limited to 'src')
-rw-r--r--src/content-parser.c35
1 files changed, 21 insertions, 14 deletions
diff --git a/src/content-parser.c b/src/content-parser.c
index 75c0f34..8249322 100644
--- a/src/content-parser.c
+++ b/src/content-parser.c
@@ -87,7 +87,7 @@ blogc_content_parse(const char *src, size_t src_len, blogc_error_t **err)
switch (state) {
case CONTENT_START_LINE:
- if (c == '\n' || c == '\r')
+ if (c == '\n' || c == '\r' || is_last)
break;
if (c == '#') {
header_level = 1;
@@ -140,18 +140,15 @@ blogc_content_parse(const char *src, size_t src_len, blogc_error_t **err)
case CONTENT_HEADER_TITLE_START:
if (c == ' ' || c == '\t')
break;
- if (c != '\n' || c != '\r') {
- start = current;
+ start = current;
+ if (c != '\n' && c != '\r') {
state = CONTENT_HEADER_TITLE;
break;
}
- *err = blogc_error_parser(BLOGC_ERROR_CONTENT_PARSER, src, src_len,
- current, "Empty header");
- break;
case CONTENT_HEADER_TITLE:
if (c == '\n' || c == '\r' || is_last) {
- end = is_last ? src_len : current;
+ end = is_last && c != '\n' && c != '\r' ? src_len : current;
tmp = b_strndup(src + start, end - start);
b_string_append_printf(rv, "<h%d>%s</h%d>\n", header_level,
tmp, header_level);
@@ -159,14 +156,13 @@ blogc_content_parse(const char *src, size_t src_len, blogc_error_t **err)
tmp = NULL;
state = CONTENT_START_LINE;
start = current;
- break;
}
break;
case CONTENT_HTML:
if (c == '\n' || c == '\r' || is_last) {
state = CONTENT_HTML_END;
- end = is_last ? src_len : current;
+ end = is_last && c != '\n' && c != '\r' ? src_len : current;
}
if (!is_last)
break;
@@ -189,10 +185,11 @@ blogc_content_parse(const char *src, size_t src_len, blogc_error_t **err)
break;
prefix = b_strndup(src + start, current - start);
state = CONTENT_BLOCKQUOTE_START;
+ break;
case CONTENT_BLOCKQUOTE_START:
if (c == '\n' || c == '\r' || is_last) {
- end = is_last ? src_len : current;
+ end = is_last && c != '\n' && c != '\r' ? src_len : current;
tmp = b_strndup(src + start, end - start);
if (b_str_starts_with(tmp, prefix)) {
lines = b_slist_append(lines, b_strdup(tmp + strlen(prefix)));
@@ -202,6 +199,10 @@ blogc_content_parse(const char *src, size_t src_len, blogc_error_t **err)
*err = blogc_error_parser(BLOGC_ERROR_CONTENT_PARSER, src, src_len,
current, "Malformed blockquote, must use same prefix "
"as previous line(s): %s", prefix);
+ free(prefix);
+ prefix = NULL;
+ b_slist_free_full(lines, free);
+ lines = NULL;
}
free(tmp);
tmp = NULL;
@@ -245,10 +246,11 @@ blogc_content_parse(const char *src, size_t src_len, blogc_error_t **err)
break;
prefix = b_strndup(src + start, current - start);
state = CONTENT_CODE_START;
+ break;
case CONTENT_CODE_START:
if (c == '\n' || c == '\r' || is_last) {
- end = is_last ? src_len : current;
+ end = is_last && c != '\n' && c != '\r' ? src_len : current;
tmp = b_strndup(src + start, end - start);
if (b_str_starts_with(tmp, prefix)) {
lines = b_slist_append(lines, b_strdup(tmp + strlen(prefix)));
@@ -258,6 +260,10 @@ blogc_content_parse(const char *src, size_t src_len, blogc_error_t **err)
*err = blogc_error_parser(BLOGC_ERROR_CONTENT_PARSER, src, src_len,
current, "Malformed code block, must use same prefix "
"as previous line(s): '%s'", prefix);
+ free(prefix);
+ prefix = NULL;
+ b_slist_free_full(lines, free);
+ lines = NULL;
}
free(tmp);
tmp = NULL;
@@ -316,7 +322,7 @@ blogc_content_parse(const char *src, size_t src_len, blogc_error_t **err)
case CONTENT_UNORDERED_LIST_START:
if (c == '\n' || c == '\r' || is_last) {
- end = is_last ? src_len : current;
+ end = is_last && c != '\n' && c != '\r' ? src_len : current;
tmp = b_strndup(src + start, end - start);
if (b_str_starts_with(tmp, prefix)) {
lines = b_slist_append(lines, b_strdup(tmp + strlen(prefix)));
@@ -367,10 +373,11 @@ blogc_content_parse(const char *src, size_t src_len, blogc_error_t **err)
break;
prefix_len = current - start;
state = CONTENT_ORDERED_LIST_START;
+ break;
case CONTENT_ORDERED_LIST_START:
if (c == '\n' || c == '\r' || is_last) {
- end = is_last ? src_len : current;
+ end = is_last && c != '\n' && c != '\r' ? src_len : current;
tmp = b_strndup(src + start, end - start);
if (strlen(tmp) >= prefix_len) {
tmp2 = b_strndup(tmp, prefix_len);
@@ -436,7 +443,7 @@ err_li:
case CONTENT_PARAGRAPH:
if (c == '\n' || c == '\r' || is_last) {
state = CONTENT_PARAGRAPH_END;
- end = is_last ? src_len : current;
+ end = is_last && c != '\n' && c != '\r' ? src_len : current;
}
if (!is_last)
break;