From 3aeccf252c8c266b51c1a2cc1dc9e9e3d5bf2f90 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sat, 13 Jun 2015 03:32:21 -0300 Subject: content-parser: implemented excerpt, and now for good! --- src/content-parser.c | 41 +++++++++++++++++++++++++++++++++++++++-- src/content-parser.h | 2 +- src/source-parser.c | 9 ++++++++- 3 files changed, 48 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/content-parser.c b/src/content-parser.c index aa76777..fa17ab8 100644 --- a/src/content-parser.c +++ b/src/content-parser.c @@ -22,6 +22,8 @@ typedef enum { CONTENT_START_LINE = 1, + CONTENT_EXCERPT, + CONTENT_EXCERPT_END, CONTENT_HEADER, CONTENT_HEADER_TITLE_START, CONTENT_HEADER_TITLE, @@ -355,7 +357,7 @@ blogc_content_parse_inline(const char *src) char* -blogc_content_parse(const char *src) +blogc_content_parse(const char *src, size_t *end_excerpt) { // src is always nul-terminated. size_t src_len = strlen(src); @@ -364,6 +366,7 @@ blogc_content_parse(const char *src) size_t start = 0; size_t start2 = 0; size_t end = 0; + size_t eend = 0; unsigned int header_level = 0; char *prefix = NULL; @@ -393,6 +396,13 @@ blogc_content_parse(const char *src) if (c == '\n' || c == '\r' || is_last) break; start = current; + if (c == '.') { + if (end_excerpt != NULL) { + eend = rv->len; // fuck it + state = CONTENT_EXCERPT; + break; + } + } if (c == '#') { header_level = 1; state = CONTENT_HEADER; @@ -426,6 +436,33 @@ blogc_content_parse(const char *src) state = CONTENT_PARAGRAPH; break; + case CONTENT_EXCERPT: + if (end_excerpt != NULL) { + if (c == '.') + break; + if (c == '\n' || c == '\r') { + //*end_excerpt = eend; + //state = CONTENT_START_LINE; + state = CONTENT_EXCERPT_END; + break; + } + } + eend = 0; + state = CONTENT_PARAGRAPH; + break; + + case CONTENT_EXCERPT_END: + if (end_excerpt != NULL) { + if (c == '\n' || c == '\r') { + *end_excerpt = eend; + state = CONTENT_START_LINE; + break; + } + } + eend = 0; + state = CONTENT_PARAGRAPH_END; + break; + case CONTENT_HEADER: if (c == '#') { header_level += 1; @@ -521,7 +558,7 @@ blogc_content_parse(const char *src) else b_string_append_printf(tmp_str, "%s\n", l->data); } - tmp = blogc_content_parse(tmp_str->str); + tmp = blogc_content_parse(tmp_str->str, NULL); b_string_append_printf(rv, "
%s
\n", tmp); free(tmp); diff --git a/src/content-parser.h b/src/content-parser.h index d0a0732..ec441c6 100644 --- a/src/content-parser.h +++ b/src/content-parser.h @@ -13,6 +13,6 @@ #include "error.h" char* blogc_content_parse_inline(const char *src); -char* blogc_content_parse(const char *src); +char* blogc_content_parse(const char *src, size_t *end_excerpt); #endif /* _CONTENT_PARSER_H */ diff --git a/src/source-parser.c b/src/source-parser.c index f0fca29..050082f 100644 --- a/src/source-parser.c +++ b/src/source-parser.c @@ -38,6 +38,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) size_t current = 0; size_t start = 0; + size_t end_excerpt = 0; char *key = NULL; char *tmp = NULL; @@ -148,7 +149,13 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) if (current == (src_len - 1)) { tmp = b_strndup(src + start, src_len - start); b_trie_insert(rv, "RAW_CONTENT", tmp); - b_trie_insert(rv, "CONTENT", blogc_content_parse(tmp)); + b_trie_insert(rv, "CONTENT", blogc_content_parse(tmp, &end_excerpt)); + if (end_excerpt != 0) + b_trie_insert(rv, "EXCERPT", + b_strndup(b_trie_lookup(rv, "CONTENT"), end_excerpt)); + else + b_trie_insert(rv, "EXCERPT", + b_strdup(b_trie_lookup(rv, "CONTENT"))); } break; } -- cgit v1.2.3-18-g5258