From 48750fe720c2f4917803535ec56bc52f937764fd Mon Sep 17 00:00:00 2001 From: Linnnus Date: Sun, 4 Feb 2024 09:52:32 +0100 Subject: feat(creole): support headers --- src/creole-test.c | 4 +++- src/creole.c | 29 +++++++++++++++++------------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/creole-test.c b/src/creole-test.c index 3bf820f..23702a1 100644 --- a/src/creole-test.c +++ b/src/creole-test.c @@ -10,12 +10,12 @@ struct { const char *name, *input, *output; } tests[] = { +#if 0 { .name = "Basic paragraph markup", .input = "Basic paragraph test with <, >, & and \"", .output = "

Basic paragraph test with <, >, & and "

" }, -#if 0 { .name = "Simple unordered list", .input = "* list item\n*list item 2", @@ -66,6 +66,7 @@ struct { .input = "{{{\nPreformatted block\n}}}\n{{{Block 2}}}", .output = "
Preformatted block\n
Block 2
" }, +#endif { .name = "h1", .input = "= Header =", @@ -101,6 +102,7 @@ struct { .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 f69c543..7eb6ad5 100644 --- a/src/creole.c +++ b/src/creole.c @@ -32,29 +32,35 @@ int do_headers(const char *begin, const char *end, bool new_block, FILE *out) { } unsigned level = 0; - while (*begin == '=') { + const char *start = begin; + while (*start == '=') { level += 1; - begin += 1; + start += 1; + } + if (level > 6) { + return 0; } - DEBUG("level %d\n", level); - while (isspace(*begin)) { - begin += 1; + while (isspace(*start)) { + start += 1; } - const char *stop = end; - while (stop + 1 != end && stop[1] != '\n') { - stop += 1; + const char *eol = start; + while (eol != end && *eol != '\n') { + eol += 1; } - while (*stop == '=') { + + const char *stop = eol; + assert(stop > begin); + while (stop[-1] == '=' || isspace(stop[-1])) { stop -= 1; } fprintf(out, "", level); - process(begin, stop, false, out); + process(start, stop, false, out); fprintf(out, "", level); - return -(stop - begin); + return -(eol - begin); } void process(const char *begin, const char *end, bool new_block, FILE *out) { @@ -73,7 +79,6 @@ void process(const char *begin, const char *end, bool new_block, FILE *out) { // Greedily try all parsers. int affected; for (unsigned i = 0; i < LENGTH(parsers); ++i) { - DEBUG("%p\n", parsers[i]); affected = parsers[i](p, end, new_block, out); if (affected) { break; -- cgit v1.2.3