diff --git a/markdown.awk b/markdown.awk
index 79c7399..a916d61 100644
--- a/markdown.awk
+++ b/markdown.awk
@@ -42,55 +42,86 @@ function startswith(str, s, sl, j) {
}
return 0;
}
-#function parse_list_item(str,
-function fold_lines(arr, i, result) {
- for (i in arr) {
- if (result != "")
- result = result " " arr[i];
- else
- result = arr[i];
- }
+function rstrip(str) {
+ gsub(/ *\n*$/, "", str);
+ return str;
}
-function parse_list(str, buf, result, i, ind, line, lines, indent) {
- result = "
\n";
+function lstrip(str) {
+ 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 = "";
- print "parse: " str ">" startswith(str, "* ")
split(str, lines, "\n");
+ str = ""
for (i in lines) {
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) {
line = lines[i];
- print "line: " line " " startswith(line, "* ") " " indent
- ind = startswith(line, "* ");
- if (indent == 0 && ind > 0) {
- indent = ind;
- }
- else if (indent > 0 && ind > 0 && ind <= indent) {
- if (length(buf) > 0) {
- result = result "- " parse_list(buf) "
\n";
+
+ if (match(line, "[^ ]") > indent)
+ buf = join_lines(buf, line, "\n");
+ else {
+ indent = match(line, "[^ ]");
+
+ if (buf != "") {
+ result = join_lines(result, parse_list(buf), "\n");
buf = "";
}
+ if (i > 1)
+ result = result "\n"
+ result = result "- " 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 (buf != "") {
+ result = join_lines(result, parse_list(buf), "\n")
}
- if (length(buf) > 0) {
- result = result "
- " parse_list(buf) "
\n";
- }
- result = result "
";
+ result = result "";
+
+ if (is_bullet)
+ result = "";
+ else
+ result = "\n" result "\n
";
+
return result;
}
@@ -187,21 +218,14 @@ function parse_body(str) {
/```/ {
if (startswith(body, "```") == 1) {
- if (body != "")
- body = body "\n";
-
- print "" substr(body, 4, length(body)-3) "
";
+ print "" substr(body, 4, length(body)-3) "
";
body = "";
next;
}
}
// {
- if (body != "")
- body = body "\n" $0;
- else
- body = $0;
-
+ body = join_lines(body, $0, "\n")
next;
}
diff --git a/test.sh b/test.sh
index 183e88e..f8f9e46 100755
--- a/test.sh
+++ b/test.sh
@@ -14,7 +14,7 @@ check() {
elif [[ "$current" == "input" ]]; then
echo "$line" >> "$input"
else
- echo $line >> "$expected_output"
+ echo "$line" >> "$expected_output"
fi
done
@@ -112,11 +112,10 @@ first line of code
second line of code
```
---
-
+
first line of code
-second line of code
-
+second line of code
EOF
check <<-"EOF"
@@ -154,5 +153,29 @@ asdf
bar qux
EOF
+
+check <<-"EOF"
+* first
+level 1
+ * second
+ level 1
+ * second level 2
+ * third level
+* first level
+ 2
+---
+
+- first level 1
+
+- second level 1
+- second level 2
+
+
+- first level 2
+
+EOF
+
echo
echo "All tests passed"