From 21faa99f847a4f88c6b3647158515564a7d1528f Mon Sep 17 00:00:00 2001 From: Linnnus Date: Sun, 4 Feb 2024 14:29:34 +0100 Subject: feat(creole): Add basic paragraphs In the future, we may need to keep track of state, if encountering block-level elements ends paragraphs. --- src/creole-test.c | 77 +++++++++++++++++++++++++++++-------------------------- src/creole.c | 26 ++++++++++++++++++- 2 files changed, 65 insertions(+), 38 deletions(-) diff --git a/src/creole-test.c b/src/creole-test.c index 23702a1..1e25a0f 100644 --- a/src/creole-test.c +++ b/src/creole-test.c @@ -10,6 +10,46 @@ struct { const char *name, *input, *output; } tests[] = { + { + .name = "Two paragraphs next to each other.", + .input = "Hello,\n\nworld!", + .output = "

Hello,

world!

" + }, + { + .name = "h1", + .input = "= Header =", + .output = "

Header

" + }, + { + .name = "h2", + .input = "== Header =", + .output = "

Header

" + }, + { + .name = "h3", + .input = "=== Header =", + .output = "

Header

" + }, + { + .name = "h4", + .input = "==== Header =", + .output = "

Header

" + }, + { + .name = "h5", + .input = "===== Header", + .output = "
Header
" + }, + { + .name = "h6", + .input = "====== Header =", + .output = "
Header
" + }, + { + .name = ">h6", + .input = "======= Header =", + .output = "

======= Header =

" + }, #if 0 { .name = "Basic paragraph markup", @@ -66,43 +106,6 @@ struct { .input = "{{{\nPreformatted block\n}}}\n{{{Block 2}}}", .output = "
Preformatted block\n
Block 2
" }, -#endif - { - .name = "h1", - .input = "= Header =", - .output = "

Header

" - }, - { - .name = "h2", - .input = "== Header =", - .output = "

Header

" - }, - { - .name = "h3", - .input = "=== Header =", - .output = "

Header

" - }, - { - .name = "h4", - .input = "==== Header =", - .output = "

Header

" - }, - { - .name = "h5", - .input = "===== Header", - .output = "
Header
" - }, - { - .name = "h6", - .input = "====== Header =", - .output = "
Header
" - }, - { - .name = ">h6", - .input = "======= Header =", - .output = "

======= Header =

" - }, -#if 0 { .name = "Tables", .input = "| A | B |\n| //C// | **D** \\\\ E |", diff --git a/src/creole.c b/src/creole.c index 7eb6ad5..b3d1a87 100644 --- a/src/creole.c +++ b/src/creole.c @@ -13,6 +13,7 @@ void process(const char *begin, const char *end, bool new_block, FILE *out); int do_headers(const char *begin, const char *end, bool new_block, FILE *out); +int do_paragraph(const char *begin, const char *end, bool new_block, FILE *out); // A parser takes a (sub)string and returns the number of characters consumed, if any. // @@ -20,7 +21,7 @@ int do_headers(const char *begin, const char *end, bool new_block, FILE *out); // The sign of the return value determines whether a new block should begin, after the consumed text. typedef int (* parser_t)(const char *begin, const char *end, bool new_block, FILE *out); -static parser_t parsers[] = { do_headers }; +static parser_t parsers[] = { do_headers, do_paragraph }; int do_headers(const char *begin, const char *end, bool new_block, FILE *out) { if (!new_block) { // Headers are block-level elements. @@ -63,6 +64,29 @@ int do_headers(const char *begin, const char *end, bool new_block, FILE *out) { return -(eol - begin); } +int do_paragraph(const char *begin, const char *end, bool new_block, FILE *out) { + if (!new_block) { // Paragraphs are block-level elements. + return 0; + } + + const char *stop = begin + 1; + while (stop + 1 < end) { + if (stop[0] == '\n' && stop[1] == '\n') { + goto found_double_newline; + } else { + stop += 1; + } + } + stop = end; +found_double_newline: + + fputs("

", out); + process(begin, stop, false, out); + fputs("

", out); + + return -(stop - begin); +} + void process(const char *begin, const char *end, bool new_block, FILE *out) { const char *p = begin; while (p < end) { -- cgit v1.2.3