Lists within blockquotes and tabs in lists

This commit is contained in:
Konstantin Nazarov 2021-07-28 22:31:44 +00:00
parent 2f982164ef
commit b54918c9b0
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
2 changed files with 55 additions and 15 deletions

View file

@ -84,8 +84,8 @@ function join_lines(first, second, sep) {
} }
function strip_list(str) { function strip_list(str) {
gsub(/^ *[-+*] /, "", str); gsub(/^[[:space:]]*[-+*][[:space:]]/, "", str);
gsub(/^ *[[:digit:]]*\. /, "", str); gsub(/^[[:space:]]*[[:digit:]]*\.[[:space:]]/, "", str);
return str; return str;
} }
@ -99,7 +99,8 @@ function parse_list(str, buf, result, i, ind, line, lines, indent, is_bullet)
for (i in lines) { for (i in lines) {
line = lines[i]; line = lines[i];
if (match(line, /^ *[-+*] /) || match(line, /^ *[[:digit:]]+\. /)) if (match(line, /^[[:space:]]*[-+*][[:space:]]/) ||
match(line, /^[[:space:]]*[[:digit:]]+\.[[:space:]]/))
str = join_lines(str, line, "\n"); str = join_lines(str, line, "\n");
else else
str = join_lines(rstrip(str), lstrip(line), " "); str = join_lines(rstrip(str), lstrip(line), " ");
@ -108,7 +109,7 @@ function parse_list(str, buf, result, i, ind, line, lines, indent, is_bullet)
split(str, lines, "\n") split(str, lines, "\n")
indent = match(str, /[^ ]/); indent = match(str, /[^ ]/);
is_bullet = match(str, /^ *[-+*] /) is_bullet = match(str, /^[[:space:]]*[-+*][[:space:]]/)
if (is_bullet) if (is_bullet)
result = "<ul>\n" result = "<ul>\n"
@ -132,11 +133,11 @@ function parse_list(str, buf, result, i, ind, line, lines, indent, is_bullet)
if (i > 1) if (i > 1)
result = result "</li>\n" result = result "</li>\n"
if (is_bullet && match(line, / *[[:digit:]]+\. /)) { if (is_bullet && match(line, /[[:space:]]*[[:digit:]]+\.[[:space:]]/)) {
is_bullet = 0; is_bullet = 0;
result = result "</ul>\n<ol>\n"; result = result "</ul>\n<ol>\n";
} }
if (is_bullet == 0 && match(line, / *[-+*] /)) { if (is_bullet == 0 && match(line, /[[:space:]]*[-+*][[:space:]]/)) {
is_bullet = 1; is_bullet = 1;
result = result "</ol>\n<ul>\n"; result = result "</ol>\n<ul>\n";
} }
@ -304,17 +305,14 @@ function parse_blockquote(str, i, lines, line, buf, result) {
line = lines[i]; line = lines[i];
gsub(/^> ?/, "", line); gsub(/^> ?/, "", line);
if (match(line, /^ *$/)) { if (buf != "")
result = join_lines(result, parse_block(buf), "\n"); buf = buf "\n" line;
buf = ""; else
} buf = line;
else {
buf = join_lines(buf, line, "\n");
}
} }
if (buf != "") if (buf != "")
result = join_lines(result, parse_block(buf), "\n"); result = join_lines(result, parse_body(buf), "\n");
result = result "\n</blockquote>" result = result "\n</blockquote>"
@ -356,7 +354,7 @@ function parse_block(str) {
else if (substr(str, 1, 1) == ">") { else if (substr(str, 1, 1) == ">") {
return parse_blockquote(str); return parse_blockquote(str);
} }
else if (match(str, /^[-+*] /) || match(str, /^[[:digit:]]\. /)) { else if (match(str, /^[-+*][[:space:]]/) || match(str, /^[[:digit:]]\.[[:space:]]/)) {
return parse_list(str); return parse_list(str);
} }
else { else {
@ -364,6 +362,31 @@ function parse_block(str) {
} }
} }
function parse_body(str, body, line, lines, result, i) {
split(str, lines, "\n");
result = "";
body = "";
for (i in lines) {
line = lines[i];
if (line_continues(body, line)) {
if (body != "")
body = body "\n" line;
else
body = line;
}
else if (body != "") {
result = join_lines(result, parse_block(body), "\n");
body = "";
}
}
if (body != "")
result = join_lines(result, parse_block(body), "\n");
return result;
}
function line_continues(body, line) { function line_continues(body, line) {
if (match(body, /^ /) && (match(line, /^ /) || line == "")) if (match(body, /^ /) && (match(line, /^ /) || line == ""))
return 1; return 1;

17
test.sh
View file

@ -313,6 +313,23 @@ in blockquotes</code></pre>
</blockquote> </blockquote>
EOF EOF
check <<-"EOF"
> A list within a blockquote:
>
> * asterisk 1
> * asterisk 2
> * asterisk 3
---
<blockquote>
<p>A list within a blockquote:</p>
<ul>
<li>asterisk 1</li>
<li>asterisk 2</li>
<li>asterisk 3</li>
</ul>
</blockquote>
EOF
check <<-"EOF" check <<-"EOF"
foo&bar foo&bar