diff options
author | davidovki <david@davidovski.xyz> | 2023-03-03 04:55:27 +0000 |
---|---|---|
committer | davidovki <david@davidovski.xyz> | 2023-03-03 04:55:27 +0000 |
commit | b1ffc4b5ba572a5e4070d162888c4c92b94a62c1 (patch) | |
tree | ec67f003f563c5a7ddb5078a0e6f82e99b7fd106 | |
parent | 7bcc93b621c4886f5a5f5aa72b22fe423cd3a777 (diff) |
Parse ordered lists and unordered lists
-rwxr-xr-x | src/md2html.sh | 95 | ||||
-rw-r--r-- | src/test.md | 19 |
2 files changed, 108 insertions, 6 deletions
diff --git a/src/md2html.sh b/src/md2html.sh index 7387fb2..86ebc3d 100755 --- a/src/md2html.sh +++ b/src/md2html.sh @@ -110,8 +110,8 @@ _p () { # parse ref-style links # -_ref () { -} +#_ref () { +#} # parse links # @@ -144,25 +144,106 @@ _a_img () { done } + +_get_indent () { + indent=0 + l="$*" + while [ "$l" ]; do + c="${l%*${l#?}}" + case "$c" in + " ") indent=$((indent+1)) ;; + *) + l="${l#?}" + break + ;; + esac + l="${l#?}" + done + printf "$indent" +} + + # parse unordered lists # _ul () { + local list=false + local indent_level=0 + while IFS= read -r line; do + set -- $line + case "$1" in + "-"|"_"|"+") + indent=$(_get_indent "$line") + + $list || { + list=true + printf "<ul>\n" + } + + [ "$indent_level" -lt "$indent" ] \ + && printf "<ul>\n" + [ "$indent_level" -gt "$indent" ] \ + && printf "</ul>\n" + indent_level=$indent + + printf "<li>%s</li>\n" "${line#*$1 }" + ;; + *) + $list && { + printf "</ul>" + list=false + } + printf "%s\n" "$line" + ;; + esac + done + $list && printf "</ul>\n" } # parse ordered lists # _ol () { + local list=false + local indent_level=0 + while IFS= read -r line; do + set -- $line + case "$1" in + *.|*\)) + indent=$(_get_indent "$line") + + $list || { + list=true + printf "<ol>\n" + } + + [ "$indent_level" -lt "$indent" ] \ + && printf "<ol>\n" + [ "$indent_level" -gt "$indent" ] \ + && printf "</ol>\n" + indent_level=$indent + + printf "<li>%s</li>\n" "${line#*$1 }" + ;; + *) + $list && { + printf "</ol>" + list=false + } + printf "%s\n" "$line" + ;; + esac + done + $list && printf "</ol>\n" } # parse mutliline codeblocks # -_code () { -} +#_code () { +#} # parse quotes # -_quote () { -} +#_quote () { +#} # convert the markdown from stdin into html @@ -171,6 +252,8 @@ md2html () { _p \ | _pre_emph \ + | _ul \ + | _ol \ | _emph '__' "<strong>" "</strong>" \ | _emph '_' "<em>" "</em>" \ | _emph '`' "<code>" "</code>" \ diff --git a/src/test.md b/src/test.md index 6aacbed..d527b96 100644 --- a/src/test.md +++ b/src/test.md @@ -9,3 +9,22 @@ click [here](http) for stuff and [over here](http12 "my title") for more this is a paragraph haha + +- this is a list + - of items + - please dont +- break + + +* star list +* this is a star + ++ plus now wow ++ plus wow so cool + +1. hello +2. world +3. lOL + + +ok that worked? |