From b1ffc4b5ba572a5e4070d162888c4c92b94a62c1 Mon Sep 17 00:00:00 2001 From: davidovki Date: Fri, 3 Mar 2023 04:55:27 +0000 Subject: Parse ordered lists and unordered lists --- src/md2html.sh | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- src/test.md | 19 ++++++++++++ 2 files changed, 108 insertions(+), 6 deletions(-) (limited to 'src') 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 "" + list=false + } + printf "%s\n" "$line" + ;; + esac + done + $list && printf "\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 "
    \n" + } + + [ "$indent_level" -lt "$indent" ] \ + && printf "
      \n" + [ "$indent_level" -gt "$indent" ] \ + && printf "
    \n" + indent_level=$indent + + printf "
  1. %s
  2. \n" "${line#*$1 }" + ;; + *) + $list && { + printf "
" + list=false + } + printf "%s\n" "$line" + ;; + esac + done + $list && printf "\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 '__' "" "" \ | _emph '_' "" "" \ | _emph '`' "" "" \ 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? -- cgit v1.2.1