Skip to content

Commit

Permalink
content-parser: source-parser: rename TITLE to FIRST_HEADER
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelmartins committed Feb 15, 2017
1 parent bc23293 commit 1d790b8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
10 changes: 5 additions & 5 deletions man/blogc-source.7.ronn
Expand Up @@ -52,11 +52,11 @@ that stores the name of the source file, without its extension. This is useful
for building permalinks in templates. This variable can't be overriden by an
explicit definition in source file.

The variable `TITLE` is created by the source parser by default, containing the
unparsed value of the first header found in the source file. The content is not
parsed but HTML entities are encoded. Headers inside blockquotes are ignored.
This variable can be overriden by an explicit definition in source file, that
must have the HTML entities escaped manually.
The variable `FIRST_HEADER` is created by the source parser by default,
containing the unparsed value of the first header found in the source file.
The content is not parsed but HTML entities are encoded. Headers inside
blockquotes are ignored. This variable can be overriden by an explicit
definition in source file, that must have the HTML entities escaped manually.

Another variable, `DESCRIPTION`, will be automatically created by the source
parser. It contains the unparsed content of the first paragraph found in the
Expand Down
6 changes: 3 additions & 3 deletions src/blogc/content-parser.c
Expand Up @@ -674,7 +674,7 @@ blogc_is_ordered_list_item(const char *str, size_t prefix_len)


char*
blogc_content_parse(const char *src, size_t *end_excerpt, char **title,
blogc_content_parse(const char *src, size_t *end_excerpt, char **first_header,
char **description)
{
// src is always nul-terminated.
Expand Down Expand Up @@ -835,8 +835,8 @@ blogc_content_parse(const char *src, size_t *end_excerpt, char **title,
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 = blogc_htmlentities(tmp);
if (first_header != NULL && *first_header == NULL)
*first_header = blogc_htmlentities(tmp);
parsed = blogc_content_parse_inline(tmp);
slug = blogc_slugify(tmp);
if (slug == NULL)
Expand Down
4 changes: 2 additions & 2 deletions src/blogc/content-parser.h
Expand Up @@ -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 **title,
char **description);
char* blogc_content_parse(const char *src, size_t *end_excerpt,
char **first_header, char **description);

#endif /* _CONTENT_PARSER_H */
16 changes: 8 additions & 8 deletions src/blogc/source-parser.c
Expand Up @@ -150,19 +150,19 @@ 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 *first_header = NULL;
char *description = NULL;
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")) {
content = blogc_content_parse(tmp, &end_excerpt,
&first_header, &description);
if (first_header != NULL) {
// do not override source-provided first_header.
if (NULL == bc_trie_lookup(rv, "FIRST_HEADER")) {
// no need to free, because we are transfering memory
// ownership to the trie.
bc_trie_insert(rv, "TITLE", title);
bc_trie_insert(rv, "FIRST_HEADER", first_header);
}
else {
free(title);
free(first_header);
}
}
if (description != NULL) {
Expand Down
8 changes: 4 additions & 4 deletions tests/blogc/check_content_parser.c
Expand Up @@ -1162,7 +1162,7 @@ test_content_parse_ordered_list_crlf(void **state)


static void
test_content_parse_title(void **state)
test_content_parse_first_header(void **state)
{
char *t = NULL;
char *html = blogc_content_parse("# foo", NULL, &t, NULL);
Expand Down Expand Up @@ -1240,7 +1240,7 @@ test_content_parse_title(void **state)


static void
test_content_parse_title_crlf(void **state)
test_content_parse_first_header_crlf(void **state)
{
char *t = NULL;
char *html = blogc_content_parse("# foo\r\n", NULL, &t, NULL);
Expand Down Expand Up @@ -2355,8 +2355,8 @@ main(void)
unit_test(test_content_parse_unordered_list_crlf),
unit_test(test_content_parse_ordered_list),
unit_test(test_content_parse_ordered_list_crlf),
unit_test(test_content_parse_title),
unit_test(test_content_parse_title_crlf),
unit_test(test_content_parse_first_header),
unit_test(test_content_parse_first_header_crlf),
unit_test(test_content_parse_description),
unit_test(test_content_parse_description_crlf),
unit_test(test_content_parse_invalid_excerpt),
Expand Down
18 changes: 9 additions & 9 deletions tests/blogc/check_source_parser.c
Expand Up @@ -43,7 +43,7 @@ test_source_parse(void **state)
"# This is a test\n"
"\n"
"bola\n");
assert_string_equal(bc_trie_lookup(source, "TITLE"), "This is a test");
assert_string_equal(bc_trie_lookup(source, "FIRST_HEADER"), "This is a test");
assert_string_equal(bc_trie_lookup(source, "DESCRIPTION"), "bola");
bc_trie_free(source);
}
Expand Down Expand Up @@ -76,7 +76,7 @@ test_source_parse_crlf(void **state)
"# This is a test\r\n"
"\r\n"
"bola\r\n");
assert_string_equal(bc_trie_lookup(source, "TITLE"), "This is a test");
assert_string_equal(bc_trie_lookup(source, "FIRST_HEADER"), "This is a test");
assert_string_equal(bc_trie_lookup(source, "DESCRIPTION"), "bola");
bc_trie_free(source);
}
Expand Down Expand Up @@ -111,7 +111,7 @@ test_source_parse_with_spaces(void **state)
"# This is a test\n"
"\n"
"bola\n");
assert_string_equal(bc_trie_lookup(source, "TITLE"), "This is a test");
assert_string_equal(bc_trie_lookup(source, "FIRST_HEADER"), "This is a test");
assert_string_equal(bc_trie_lookup(source, "DESCRIPTION"), "bola");
bc_trie_free(source);
}
Expand Down Expand Up @@ -156,19 +156,19 @@ test_source_parse_with_excerpt(void **state)
"\n"
"guda\n"
"yay");
assert_string_equal(bc_trie_lookup(source, "TITLE"), "This is a test");
assert_string_equal(bc_trie_lookup(source, "FIRST_HEADER"), "This is a test");
assert_string_equal(bc_trie_lookup(source, "DESCRIPTION"), "bola");
bc_trie_free(source);
}


static void
test_source_parse_with_title(void **state)
test_source_parse_with_first_header(void **state)
{
const char *a =
"VAR1: asd asd\n"
"VAR2: 123chunda\n"
"TITLE: THIS IS CHUNDA!\n"
"FIRST_HEADER: THIS IS CHUNDA!\n"
"----------\n"
"# This is a test\n"
"\n"
Expand All @@ -190,7 +190,7 @@ test_source_parse_with_title(void **state)
"# This is a test\n"
"\n"
"bola\n");
assert_string_equal(bc_trie_lookup(source, "TITLE"), "THIS IS CHUNDA!");
assert_string_equal(bc_trie_lookup(source, "FIRST_HEADER"), "THIS IS CHUNDA!");
assert_string_equal(bc_trie_lookup(source, "DESCRIPTION"), "bola");
bc_trie_free(source);
}
Expand Down Expand Up @@ -224,7 +224,7 @@ test_source_parse_with_description(void **state)
"# This is a test\n"
"\n"
"bola\n");
assert_string_equal(bc_trie_lookup(source, "TITLE"), "This is a test");
assert_string_equal(bc_trie_lookup(source, "FIRST_HEADER"), "This is a test");
assert_string_equal(bc_trie_lookup(source, "DESCRIPTION"), "huehuehuebrbr");
bc_trie_free(source);
}
Expand Down Expand Up @@ -556,7 +556,7 @@ main(void)
unit_test(test_source_parse_crlf),
unit_test(test_source_parse_with_spaces),
unit_test(test_source_parse_with_excerpt),
unit_test(test_source_parse_with_title),
unit_test(test_source_parse_with_first_header),
unit_test(test_source_parse_with_description),
unit_test(test_source_parse_config_empty),
unit_test(test_source_parse_config_invalid_key),
Expand Down

0 comments on commit 1d790b8

Please sign in to comment.