summaryrefslogtreecommitdiff
path: root/src/creole.c
diff options
context:
space:
mode:
authorLinnnus <[email protected]>2024-02-04 14:29:34 +0100
committerLinnnus <[email protected]>2024-02-04 20:26:23 +0100
commit21faa99f847a4f88c6b3647158515564a7d1528f (patch)
tree2c65fb3e9626aa3e29c95111bc430944b299384c /src/creole.c
parent48750fe720c2f4917803535ec56bc52f937764fd (diff)
feat(creole): Add basic paragraphs
In the future, we may need to keep track of state, if encountering block-level elements ends paragraphs.
Diffstat (limited to 'src/creole.c')
-rw-r--r--src/creole.c26
1 files changed, 25 insertions, 1 deletions
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("<p>", out);
+ process(begin, stop, false, out);
+ fputs("</p>", out);
+
+ return -(stop - begin);
+}
+
void process(const char *begin, const char *end, bool new_block, FILE *out) {
const char *p = begin;
while (p < end) {