aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2016-05-23 02:38:22 +0200
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2016-05-23 02:41:11 +0200
commit1605bf39ab342ea5d904fe96df81dd0f55bd3bcf (patch)
tree04a8edf1609888cedeb284efa2bc041fd2d40b60 /src
parentcc0c4d6866bb876e1ba098bc7f70fb8f312e9fc2 (diff)
downloadblogc-1605bf39ab342ea5d904fe96df81dd0f55bd3bcf.tar.gz
blogc-1605bf39ab342ea5d904fe96df81dd0f55bd3bcf.tar.bz2
blogc-1605bf39ab342ea5d904fe96df81dd0f55bd3bcf.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/content-parser.c45
-rw-r--r--src/content-parser.h1
2 files changed, 44 insertions, 2 deletions
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, "<p>%s</p>%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,