Make bullet lists work

This commit is contained in:
Konstantin Nazarov 2021-07-10 17:13:17 +00:00
parent 632c454116
commit d38a5ccf1c
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
2 changed files with 91 additions and 44 deletions

View file

@ -42,55 +42,86 @@ function startswith(str, s, sl, j) {
} }
return 0; return 0;
} }
#function parse_list_item(str,
function fold_lines(arr, i, result) { function rstrip(str) {
for (i in arr) { gsub(/ *\n*$/, "", str);
if (result != "") return str;
result = result " " arr[i];
else
result = arr[i];
}
} }
function parse_list(str, buf, result, i, ind, line, lines, indent) { function lstrip(str) {
result = "<ul>\n"; gsub(/^ *\n*/, "", str);
return str;
}
function join_lines(first, second, sep) {
if (sep == "")
sep = " ";
if (second == "")
return first;
if (first == "")
return second;
return first sep second;
}
function strip_list(str) {
gsub(/^ *\* /, "", str);
gsub(/^ *[[:digit:]]*\. /, "", str);
return str;
}
function parse_list(str, buf, result, i, ind, line, lines, indent, is_bullet) {
result = "";
buf = ""; buf = "";
print "parse: " str ">" startswith(str, "* ")
split(str, lines, "\n"); split(str, lines, "\n");
str = ""
for (i in lines) { for (i in lines) {
line = lines[i]; line = lines[i];
if (match(line, / *\* /) || match(line, / *[[:digit:]]+\. /))
str = join_lines(str, line, "\n");
else
str = join_lines(rstrip(str), lstrip(line), " ");
} }
indent = 0; split(str, lines, "\n")
indent = match(str, /[^ ]/);
is_bullet = match(str, /^ *\* /)
for (i in lines) { for (i in lines) {
line = lines[i]; line = lines[i];
print "line: " line " " startswith(line, "* ") " " indent
ind = startswith(line, "* "); if (match(line, "[^ ]") > indent)
if (indent == 0 && ind > 0) { buf = join_lines(buf, line, "\n");
indent = ind; else {
} indent = match(line, "[^ ]");
else if (indent > 0 && ind > 0 && ind <= indent) {
if (length(buf) > 0) { if (buf != "") {
result = result "<li>" parse_list(buf) "</li>\n"; result = join_lines(result, parse_list(buf), "\n");
buf = ""; buf = "";
} }
if (i > 1)
result = result "</li>\n"
result = result "<li>" strip_list(line)
} }
if (length(buf) > 0)
buf = buf "\n";
if (ind > 0 && ind <= indent) {
buf = buf substr(line, ind+2, length(line) - 2);
}
else
buf = buf line;
} }
if (length(buf) > 0) {
result = result "<li>" parse_list(buf) "</li>\n"; if (buf != "") {
result = join_lines(result, parse_list(buf), "\n")
} }
result = result "</ul>"; result = result "</li>";
if (is_bullet)
result = "<ul>\n" result "\n</ul>";
else
result = "<ol>\n" result "\n</ol>";
return result; return result;
} }
@ -187,21 +218,14 @@ function parse_body(str) {
/```/ { /```/ {
if (startswith(body, "```") == 1) { if (startswith(body, "```") == 1) {
if (body != "") print "<pre><code>" substr(body, 4, length(body)-3) "</code></pre>";
body = body "\n";
print "<code>" substr(body, 4, length(body)-3) "</code>";
body = ""; body = "";
next; next;
} }
} }
// { // {
if (body != "") body = join_lines(body, $0, "\n")
body = body "\n" $0;
else
body = $0;
next; next;
} }

31
test.sh
View file

@ -14,7 +14,7 @@ check() {
elif [[ "$current" == "input" ]]; then elif [[ "$current" == "input" ]]; then
echo "$line" >> "$input" echo "$line" >> "$input"
else else
echo $line >> "$expected_output" echo "$line" >> "$expected_output"
fi fi
done done
@ -112,11 +112,10 @@ first line of code
second line of code second line of code
``` ```
--- ---
<code> <pre><code>
first line of code first line of code
second line of code second line of code</code></pre>
</code>
EOF EOF
check <<-"EOF" check <<-"EOF"
@ -154,5 +153,29 @@ asdf
<li>bar qux</li> <li>bar qux</li>
</ul> </ul>
EOF EOF
check <<-"EOF"
* first
level 1
* second
level 1
* second level 2
* third level
* first level
2
---
<ul>
<li>first level 1
<ul>
<li>second level 1</li>
<li>second level 2
<ul>
<li>third level</li>
</ul></li>
</ul></li>
<li>first level 2</li>
</ul>
EOF
echo echo
echo "All tests passed" echo "All tests passed"