commit 9dd4642bc4c9111d1d58f506f5065125c9e858ed (patch)
parent 94c419801a05f1442182b0752fef0d89489b75b7
Author: Alex Karle <alex@alexkarle.com>
Date: Mon, 27 Dec 2021 12:29:31 -0500
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.
Diffstat:
3 files changed, 36 insertions(+), 7 deletions(-)
diff --git 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("</p>\n"); break;
case CODE: printf("</code></pre>\n"); break;
- case QUOTE: printf("</blockquote>\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("</blockquote>\n");
+ blockquote = false;
+ }
}
}
@@ -205,11 +208,22 @@ int parse() {
toggle_format();
break;
case '\t':
+ if (in == NONE) {
+ in = CODE;
+ printf("<pre><code>");
+ } else if (lastc == '\n' && c == '\t') {
+ /* no op */
+ } else {
+ putesc(c);
+ }
+ break;
case '>':
if (in == NONE) {
- in = c == '>' ? QUOTE : CODE;
- printf("%s", c == '>' ? "<blockquote>" : "<pre><code>");
- } else if (lastc == '\n' && c == (in == CODE ? '\t' : '>')) {
+ in = PARAGRAPH;
+ /* only start a new blockquote block is not already in one */
+ printf("%s", blockquote ? "<p>\n" : "<blockquote>\n<p>\n");
+ blockquote = true;
+ } else if (lastc == '\n' && c == '>') {
/* no op */
} else {
putesc(c);
diff --git a/test/big.html b/test/big.html
@@ -54,8 +54,11 @@ This is a <em>paragraph</em> <strong>dangit</strong> with
links to <a href="https://alexkarle.com">https://alexkarle.com</a> and
my <a href="gopher://alexkarle.com">gopherhole</a>.
</p>
-<blockquote> this is a block quote
+<blockquote>
+<p>
+ this is a block quote
that wraps!
+</p>
</blockquote>
<pre><code>$ this is a code block
$ *bold* and _italics_ and `code` have no effect!
@@ -80,3 +83,11 @@ with a newline!</a>
this is another link
with a newline!</a>
</p>
+<blockquote>
+<p>
+ This is a block quote
+</p>
+<p>
+ With two paragraphs!
+</p>
+</blockquote>
diff --git 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!