From 9b2a563b4931ca39cd6dd14bf85cda627714a4b2 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Wed, 20 Apr 2016 02:50:20 +0200 Subject: content-parser: extract post description from content description is the first line of the first paragraph parsed from content file. users can override it declaring DESCRIPTION variable in source file itself. this also fixes a bug with line endings when using single line blockquotes. --- src/content-parser.c | 16 +++++++--------- src/content-parser.h | 3 ++- src/source-parser.c | 14 +++++++++++++- 3 files changed, 22 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/content-parser.c b/src/content-parser.c index 782a85d..795cc34 100644 --- a/src/content-parser.c +++ b/src/content-parser.c @@ -460,7 +460,7 @@ blogc_is_ordered_list_item(const char *str, size_t prefix_len) char* -blogc_content_parse(const char *src, size_t *end_excerpt) +blogc_content_parse(const char *src, size_t *end_excerpt, char **description) { // src is always nul-terminated. size_t src_len = strlen(src); @@ -698,14 +698,10 @@ blogc_content_parse(const char *src, size_t *end_excerpt) case CONTENT_BLOCKQUOTE_END: if (c == '\n' || c == '\r' || is_last) { tmp_str = sb_string_new(); - for (sb_slist_t *l = lines; l != NULL; l = l->next) { - if (l->next == NULL) - sb_string_append_printf(tmp_str, "%s", l->data); - else - sb_string_append_printf(tmp_str, "%s%s", l->data, - line_ending); - } - tmp = blogc_content_parse(tmp_str->str, NULL); + for (sb_slist_t *l = lines; l != NULL; l = l->next) + sb_string_append_printf(tmp_str, "%s%s", l->data, + line_ending); + tmp = blogc_content_parse(tmp_str->str, NULL, description); sb_string_append_printf(rv, "
%s
%s", tmp, line_ending); free(tmp); @@ -1019,6 +1015,8 @@ blogc_content_parse(const char *src, size_t *end_excerpt) 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; diff --git a/src/content-parser.h b/src/content-parser.h index 6617bb4..148d5ed 100644 --- a/src/content-parser.h +++ b/src/content-parser.h @@ -16,6 +16,7 @@ char* blogc_slugify(const char *str); char* blogc_htmlentities(const char *str); 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); +char* blogc_content_parse(const char *src, size_t *end_excerpt, + char **description); #endif /* _CONTENT_PARSER_H */ diff --git a/src/source-parser.c b/src/source-parser.c index 1047f06..6e026d2 100644 --- a/src/source-parser.c +++ b/src/source-parser.c @@ -154,7 +154,19 @@ blogc_source_parse(const char *src, size_t src_len, sb_error_t **err) if (current == (src_len - 1)) { tmp = sb_strndup(src + start, src_len - start); sb_trie_insert(rv, "RAW_CONTENT", tmp); - content = blogc_content_parse(tmp, &end_excerpt); + char *description = NULL; + content = blogc_content_parse(tmp, &end_excerpt, &description); + if (description != NULL) { + // do not override source-provided description. + if (NULL == sb_trie_lookup(rv, "DESCRIPTION")) { + // no need to free, because we are transfering memory + // ownership to the trie. + sb_trie_insert(rv, "DESCRIPTION", description); + } + else { + free(description); + } + } sb_trie_insert(rv, "CONTENT", content); sb_trie_insert(rv, "EXCERPT", end_excerpt == 0 ? sb_strdup(content) : sb_strndup(content, end_excerpt)); -- cgit v1.2.3-18-g5258