From 9dd4642bc4c9111d1d58f506f5065125c9e858ed Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Mon, 27 Dec 2021 12:29:31 -0500 Subject: [PATCH] Add support for multi-paragraph blockquotes This was harder than expected--it changes the paradigm of the blockquote being a typical "Block" type, since we need to contain other block types inside it (like paragraphs); instead, it's a new global state boolean, and we use that to determine when to start and end the blockquote. --- nihdoc.c | 26 ++++++++++++++++++++------ test/big.html | 13 ++++++++++++- test/big.txt | 4 ++++ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/nihdoc.c b/nihdoc.c index c1ace00..8ac5b3d 100644 --- a/nihdoc.c +++ b/nihdoc.c @@ -22,7 +22,6 @@ enum Block { HEADER_PARSE, PARAGRAPH, CODE, - QUOTE, LIST, LIST_PARSE, }; @@ -49,6 +48,7 @@ int listdepth = 0; int lastc = '0'; bool ol = false; bool linestarted = false; +bool blockquote = false; /* Helper functions */ @@ -100,11 +100,10 @@ void handle_lf() { } /* multi-line types (two lf to close) */ - if (lastc == '\n') { + if (lastc == '\n' || (lastc == '>' && blockquote)) { switch (in) { case PARAGRAPH: printf("

\n"); break; case CODE: printf("\n"); break; - case QUOTE: printf("\n"); break; case LIST: previndent = 0; while (endlist()) @@ -114,6 +113,10 @@ void handle_lf() { break; /* no op */ } in = NONE; + if (blockquote && lastc == '\n') { + printf("\n"); + blockquote = false; + } } } @@ -205,11 +208,22 @@ int parse() { toggle_format(); break; case '\t': + if (in == NONE) { + in = CODE; + printf("
");
+                } else if (lastc == '\n' && c == '\t') {
+                    /* no op */
+                } else {
+                    putesc(c);
+                }
+                break;
             case '>':
                 if (in == NONE) {
-                    in = c == '>' ? QUOTE : CODE;
-                    printf("%s", c == '>' ? "
" : "
");
-                } else if (lastc == '\n' && c == (in == CODE ? '\t' : '>')) {
+                    in = PARAGRAPH;
+                    /* only start a new blockquote block is not already in one */
+                    printf("%s", blockquote ? "

\n" : "

\n

\n"); + blockquote = true; + } else if (lastc == '\n' && c == '>') { /* no op */ } else { putesc(c); diff --git a/test/big.html b/test/big.html index 29e7f14..b16f617 100644 --- a/test/big.html +++ b/test/big.html @@ -54,8 +54,11 @@ This is a paragraph dangit with links to https://alexkarle.com and my gopherhole.

-
this is a block quote +
+

+ this is a block quote that wraps! +

$ this is a code block
 $ *bold* and _italics_ and `code` have no effect!
@@ -80,3 +83,11 @@ with a newline!
 this is another link
 with a newline!
 

+
+

+ This is a block quote +

+

+ With two paragraphs! +

+
diff --git a/test/big.txt b/test/big.txt index 6693f77..f1f5cc9 100644 --- a/test/big.txt +++ b/test/big.txt @@ -49,3 +49,7 @@ with a newline!] [https://example.com this is another link with a newline!] + +> This is a block quote +> +> With two paragraphs! -- libgit2 1.1.1