aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2017-02-14 23:51:36 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2017-02-14 23:51:36 +0100
commit80017f034146b50fdc441c96636f1098adf7037e (patch)
treecbac45bca69cf635c2ad633ea1bcd6f40ba97a9f /src/blogc
parent99ef1c93f0359741fd18217cd81cf2c3e0beaf7b (diff)
downloadblogc-80017f034146b50fdc441c96636f1098adf7037e.tar.gz
blogc-80017f034146b50fdc441c96636f1098adf7037e.tar.bz2
blogc-80017f034146b50fdc441c96636f1098adf7037e.zip
content-parser: source-parser: extract TITLE from content header
this patch implements support to using the first header found in source file as the TITLE variable. please note that if the TITLE variable is defined on the source file's variable section it takes precedence. this patch changes the old behaviour and can break some users' websites. if you have some '{% ifdef TITLE %}' blocks in your template, they will evaluate to true if you don't defined TITLE manually, but have a header in your content.
Diffstat (limited to 'src/blogc')
-rw-r--r--src/blogc/content-parser.c9
-rw-r--r--src/blogc/content-parser.h2
-rw-r--r--src/blogc/source-parser.c15
3 files changed, 21 insertions, 5 deletions
diff --git a/src/blogc/content-parser.c b/src/blogc/content-parser.c
index 23586bb..415e5ee 100644
--- a/src/blogc/content-parser.c
+++ b/src/blogc/content-parser.c
@@ -674,7 +674,8 @@ blogc_is_ordered_list_item(const char *str, size_t prefix_len)
char*
-blogc_content_parse(const char *src, size_t *end_excerpt, char **description)
+blogc_content_parse(const char *src, size_t *end_excerpt, char **title,
+ char **description)
{
// src is always nul-terminated.
size_t src_len = strlen(src);
@@ -834,6 +835,8 @@ blogc_content_parse(const char *src, size_t *end_excerpt, char **description)
end = is_last && c != '\n' && c != '\r' ? src_len :
(real_end != 0 ? real_end : current);
tmp = bc_strndup(src + start, end - start);
+ if (title != NULL && *title == NULL)
+ *title = bc_strdup(tmp);
parsed = blogc_content_parse_inline(tmp);
slug = blogc_slugify(tmp);
if (slug == NULL)
@@ -915,10 +918,10 @@ blogc_content_parse(const char *src, size_t *end_excerpt, char **description)
for (bc_slist_t *l = lines; l != NULL; l = l->next)
bc_string_append_printf(tmp_str, "%s%s", l->data,
line_ending);
- // do not propagate description to blockquote parsing,
+ // do not propagate title and description to blockquote parsing,
// because we just want paragraphs from first level of
// content.
- tmp = blogc_content_parse(tmp_str->str, NULL, NULL);
+ tmp = blogc_content_parse(tmp_str->str, NULL, NULL, NULL);
bc_string_append_printf(rv, "<blockquote>%s</blockquote>%s",
tmp, line_ending);
free(tmp);
diff --git a/src/blogc/content-parser.h b/src/blogc/content-parser.h
index 37e38d7..39230c6 100644
--- a/src/blogc/content-parser.h
+++ b/src/blogc/content-parser.h
@@ -17,7 +17,7 @@ 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,
+char* blogc_content_parse(const char *src, size_t *end_excerpt, char **title,
char **description);
#endif /* _CONTENT_PARSER_H */
diff --git a/src/blogc/source-parser.c b/src/blogc/source-parser.c
index a3d1e09..f6f3471 100644
--- a/src/blogc/source-parser.c
+++ b/src/blogc/source-parser.c
@@ -150,8 +150,21 @@ blogc_source_parse(const char *src, size_t src_len, bc_error_t **err)
if (current == (src_len - 1)) {
tmp = bc_strndup(src + start, src_len - start);
bc_trie_insert(rv, "RAW_CONTENT", tmp);
+ char *title = NULL;
char *description = NULL;
- content = blogc_content_parse(tmp, &end_excerpt, &description);
+ content = blogc_content_parse(tmp, &end_excerpt, &title,
+ &description);
+ if (title != NULL) {
+ // do not override source-provided title.
+ if (NULL == bc_trie_lookup(rv, "TITLE")) {
+ // no need to free, because we are transfering memory
+ // ownership to the trie.
+ bc_trie_insert(rv, "TITLE", title);
+ }
+ else {
+ free(title);
+ }
+ }
if (description != NULL) {
// do not override source-provided description.
if (NULL == bc_trie_lookup(rv, "DESCRIPTION")) {