Add support for numbered lists

This commit is contained in:
Konstantin Nazarov 2021-07-10 17:33:33 +00:00
parent d38a5ccf1c
commit dba2572a08
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
2 changed files with 80 additions and 24 deletions

View file

@ -93,12 +93,19 @@ function parse_list(str, buf, result, i, ind, line, lines, indent, is_bullet)
indent = match(str, /[^ ]/); indent = match(str, /[^ ]/);
is_bullet = match(str, /^ *\* /) is_bullet = match(str, /^ *\* /)
if (is_bullet)
result = "<ul>\n"
else
result = "<ol>\n"
for (i in lines) { for (i in lines) {
line = lines[i]; line = lines[i];
if (match(line, "[^ ]") > indent) if (match(line, "[^ ]") > indent) {
buf = join_lines(buf, line, "\n"); buf = join_lines(buf, line, "\n");
else { continue
}
indent = match(line, "[^ ]"); indent = match(line, "[^ ]");
if (buf != "") { if (buf != "") {
@ -107,9 +114,17 @@ 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"
result = result "<li>" strip_list(line)
if (is_bullet && match(line, / *[[:digit:]]+\. /)) {
is_bullet = 0;
result = result "</ul>\n<ol>\n";
}
if (is_bullet == 0 && match(line, / *\* /)) {
is_bullet = 1;
result = result "</ol>\n<ul>\n";
} }
result = result "<li>" parse_block(strip_list(line))
} }
if (buf != "") { if (buf != "") {
@ -118,9 +133,9 @@ function parse_list(str, buf, result, i, ind, line, lines, indent, is_bullet)
result = result "</li>"; result = result "</li>";
if (is_bullet) if (is_bullet)
result = "<ul>\n" result "\n</ul>"; result = result "\n</ul>";
else else
result = "<ol>\n" result "\n</ol>"; result = result "\n</ol>";
return result; return result;
} }
@ -129,9 +144,6 @@ function parse_block(str, result, end, i) {
#print "block '" str "'" #print "block '" str "'"
result = "" result = ""
if (substr(str, 1, 2) == "* ") {
return parse_list(str);
}
for (i=1; i<=length(str); i++) { for (i=1; i<=length(str); i++) {
if (substr(str, i, 2) == "**") { if (substr(str, i, 2) == "**") {
@ -176,8 +188,8 @@ function parse_block(str, result, end, i) {
} }
function parse_paragraph(str) { function parse_paragraph(str) {
if (substr(str, 1, 2 ) == "* ") { if (match(str, /^\* /) || match(str, /^[[:digit:]]\. /)) {
return parse_block(str); return parse_list(str);
} }
else { else {
return "<p>" parse_block(str) "</p>"; return "<p>" parse_block(str) "</p>";

54
test.sh
View file

@ -143,14 +143,38 @@ EOF
check <<-"EOF" check <<-"EOF"
asdf asdf
* foo * bullet 1
* bar * bullet
qux 2
--- ---
<p>asdf</p> <p>asdf</p>
<ul> <ul>
<li>foo</li> <li>bullet 1</li>
<li>bar qux</li> <li>bullet 2</li>
</ul>
EOF
check <<-"EOF"
asdf
1. number 1
2. number
2
---
<p>asdf</p>
<ol>
<li>number 1</li>
<li>number 2</li>
</ol>
EOF
check <<-"EOF"
* **basic formatting**
* ```in list```
---
<ul>
<li><strong>basic formatting</strong></li>
<li><code>in list</code></li>
</ul> </ul>
EOF EOF
@ -177,5 +201,25 @@ level 1
</ul> </ul>
EOF EOF
check <<-"EOF"
* bullet1
1. number1
* bullet2
2. number2
---
<ul>
<li>bullet1</li>
</ul>
<ol>
<li>number1</li>
</ol>
<ul>
<li>bullet2</li>
</ul>
<ol>
<li>number2</li>
</ol>
EOF
echo echo
echo "All tests passed" echo "All tests passed"