From 6457d823443f30053d507d53d66dffedb7a53a07 Mon Sep 17 00:00:00 2001 From: Linnnus Date: Thu, 4 Apr 2024 17:32:32 +0200 Subject: feat(creole): Add horizontal rule --- src/creole.c | 25 ++++++++++++++++++++++++- src/creole_test_main.c | 10 +++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/creole.c b/src/creole.c index c8847bb..dc37075 100644 --- a/src/creole.c +++ b/src/creole.c @@ -24,6 +24,7 @@ long do_bold(const char *begin, const char *end, bool new_block, FILE *out); long do_nowiki_inline(const char *begin, const char *end, bool new_block, FILE *out); long do_nowiki_block(const char *begin, const char *end, bool new_block, FILE *out); long do_list(const char *begin, const char *end, bool new_block, FILE *out); +long do_horizontal_rule(const char *begin, const char *end, bool new_block, FILE *out); // Prints string with special HTML characters escaped. // @@ -89,6 +90,7 @@ static parser_t parsers[] = { do_headers, do_nowiki_block, do_list, + do_horizontal_rule, do_paragraph, //

should be last as it eats anything // Inline-level elements @@ -97,7 +99,8 @@ static parser_t parsers[] = { do_link, do_raw_url, do_nowiki_inline, - do_replacements + do_replacements, + }; long do_headers(const char *begin, const char *end, bool new_block, FILE *out) { @@ -508,6 +511,26 @@ long do_list(const char *begin, const char *end, bool new_block, FILE *out) { return -(item_end - begin); } +long do_horizontal_rule(const char *begin, const char *end, bool new_block, FILE *out) { + if (!new_block) { + return 0; + } + + unsigned length = 0; + const char *q = begin; + while (q < end && *q == '-') { + q++; + } + + // Anything at least 4 hyphens long is a horizontal rule. + // See: http://www.wikicreole.org/wiki/HorizontalRuleReasoning + if (length >= 4) { + fputs("


", out); + } + + return q - end; +} + void process(const char *begin, const char *end, bool new_block, FILE *out) { assert(begin <= end); diff --git a/src/creole_test_main.c b/src/creole_test_main.c index 5c35e49..34ca07d 100644 --- a/src/creole_test_main.c +++ b/src/creole_test_main.c @@ -296,6 +296,11 @@ struct { .input = "## Sublist item", .output = "

## Sublist item

" }, + { + .name = "Horizontal rule", + .input = "Some text\n\n----\n\nSome more text", + .output = "

Some text


Some more text

" + }, #if 0 { .name = "Ordered item with ordered sublist", @@ -307,11 +312,6 @@ struct { .input = "* Item\n*# Subitem", .output = "" }, - { - .name = "Horizontal rule", - .input = "Some text\n----\nSome more text", - .output = "

Some text


Some more text

" - }, { .name = "Preformatted block", .input = "{{{\nPreformatted block\n}}}", -- cgit v1.2.3