From 1605bf39ab342ea5d904fe96df81dd0f55bd3bcf Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Mon, 23 May 2016 02:38:22 +0200 Subject: content-parser: fixed DESCRIPTION variable. it is now built as a single line variable, that contains the full unparsed content of the first paragraph found in the source file, instead of just the first line of it. this also fixes a bug that prevented creating of DESCRIPTION variable, if the source file contained only a single line paragraph. --- src/content-parser.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- src/content-parser.h | 1 + 2 files changed, 44 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/content-parser.c b/src/content-parser.c index 4840a1d..3cb0ddb 100644 --- a/src/content-parser.c +++ b/src/content-parser.c @@ -75,6 +75,47 @@ blogc_htmlentities(const char *str) } +char* +blogc_fix_description(const char *paragraph) +{ + if (paragraph == NULL) + return NULL; + sb_string_t *rv = sb_string_new(); + bool last = false; + bool newline = false; + char *tmp = NULL; + size_t start = 0; + size_t current = 0; + while (true) { + switch (paragraph[current]) { + case '\0': + last = true; + case '\r': + case '\n': + if (newline) + break; + tmp = sb_strndup(paragraph + start, current - start); + sb_string_append(rv, sb_str_strip(tmp)); + free(tmp); + tmp = NULL; + if (!last) + sb_string_append_c(rv, ' '); + start = current + 1; + newline = true; + break; + default: + newline = false; + } + if (last) + break; + current++; + } + tmp = sb_strdup(sb_str_strip(rv->str)); + sb_string_free(rv, true); + return tmp; +} + + typedef enum { CONTENT_START_LINE = 1, CONTENT_EXCERPT, @@ -1018,8 +1059,6 @@ blogc_content_parse(const char *src, size_t *end_excerpt, char **description) state = CONTENT_PARAGRAPH_END; end = is_last && c != '\n' && c != '\r' ? src_len : (real_end != 0 ? real_end : current); - if (description != NULL && *description == NULL) - *description = sb_strndup(src + start, end - start); } if (!is_last) break; @@ -1027,6 +1066,8 @@ blogc_content_parse(const char *src, size_t *end_excerpt, char **description) case CONTENT_PARAGRAPH_END: if (c == '\n' || c == '\r' || is_last) { tmp = sb_strndup(src + start, end - start); + if (description != NULL && *description == NULL) + *description = blogc_fix_description(tmp); parsed = blogc_content_parse_inline(tmp); sb_string_append_printf(rv, "

%s

%s", parsed, line_ending); diff --git a/src/content-parser.h b/src/content-parser.h index 148d5ed..85bd063 100644 --- a/src/content-parser.h +++ b/src/content-parser.h @@ -14,6 +14,7 @@ char* blogc_slugify(const char *str); char* blogc_htmlentities(const char *str); +char* blogc_fix_description(const char *paragraph); 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, -- cgit v1.2.3-18-g5258