diff options
author | Linnnus <[email protected]> | 2024-02-04 09:52:32 +0100 |
---|---|---|
committer | Linnnus <[email protected]> | 2024-02-04 20:26:23 +0100 |
commit | 48750fe720c2f4917803535ec56bc52f937764fd (patch) | |
tree | 2912ce65c5361061202fcf3f883527312ca99493 | |
parent | de89d156ba203298d0c831f86e15b6354fbf8ed5 (diff) |
feat(creole): support headers
-rw-r--r-- | src/creole-test.c | 4 | ||||
-rw-r--r-- | 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 = "<p>Basic paragraph test with <, >, & and "</p>" }, -#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 = "<pre>Preformatted block\n</pre><pre>Block 2</pre>" }, +#endif { .name = "h1", .input = "= Header =", @@ -101,6 +102,7 @@ struct { .input = "======= Header =", .output = "<p>======= Header =</p>" }, +#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, "<h%u>", level); - process(begin, stop, false, out); + process(start, stop, false, out); fprintf(out, "</h%u>", 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; |