From a5fcbde9b233ab2af45ab0c204da1c764d33547a Mon Sep 17 00:00:00 2001 From: davidovki Date: Sat, 11 Mar 2023 17:04:50 +0000 Subject: [PATCH] Fix inline code from being formatted --- src/md2html.sh | 102 ++++++++++++++++++++++++++++++++++++------------- src/test.md | 20 +++++++--- 2 files changed, 90 insertions(+), 32 deletions(-) diff --git a/src/md2html.sh b/src/md2html.sh index 776527b..1edc6a1 100755 --- a/src/md2html.sh +++ b/src/md2html.sh @@ -2,6 +2,29 @@ ESC_SEQ='\0' +# remove traling whitespace from empty lines +# +_pre_strip () { + while IFS= read -r line; do + set -- $line + [ "$*" ] && { + local l c + l="$line" + line= + while [ "$l" != "${l#?}" ]; do + c="${l%*"${l#?}"}" + case "$c" in + " ") line="$line ";; + *) line="$line$c" ;; + esac + l="${l#?}" + done + + printf "%s\n" "$line" + } || printf "\n" + done +} + # replace all * with _ for easier processing # _pre_emph () { @@ -107,8 +130,8 @@ _p () { ;; *) $empty && - printf "

\n%s\n " "$line" || - printf "%s\n " "$line" + printf "

\n%s\n" "$line" || + printf "%s\n" "$line" empty=false ;; esac @@ -133,6 +156,10 @@ _a_img () { case "$line" in "$ESC_SEQ"*) printf "%s\n" "$line" && continue;; esac next="$line" while [ "$next" != "${next#*$close}" ]; do + case "$next" in + *"["*"]("*")"*);; + *) break; + esac before="${next%%$open*}" text=${next#*$open} text=${text%%$mid*} url=${next#*$mid} url=${url%%$close*} @@ -144,12 +171,12 @@ _a_img () { || title= case "$before" in - *!) h="%s\"%s\"" + *!) h="%s\n%s\"%s\"\n" before="${before%!}" ;; - *) h="%s%s" ;; + *) h="%s\n%s\n%s\n" ;; esac - printf "$h" "$before" "$url" "$title" "$text" + printf "$h" "$before" "$ESC_SEQ" "$url" "$title" "$text" next="${next#*$close}" done @@ -163,7 +190,7 @@ _get_indent () { indent=0 l="$*" while [ "$l" ]; do - c="${l%*${l#?}}" + c="${l%*"${l#?}"}" case "$c" in " ") indent=$((indent+1)) ;; *) @@ -261,6 +288,14 @@ _ol () { print_x $to_close "\n" } +# parse inline codeblocks +# +_inline_code () { + _emph '`' " +$ESC_SEQ" " +" +} + # parse multiline codeblocks # _code () { @@ -268,14 +303,19 @@ _code () { while IFS= read -r line; do case "$line" in " "*) + # prefix lines with newline to avoid trailing newline $codeblock && - printf "%s\n" "$ESC_SEQ${line# }" || + printf "\n%s" "$ESC_SEQ${line# }" || $content || { - printf "

\n"
+                    printf "%s
%s" "$ESC_SEQ" "${line#    }"
                     codeblock=true
-                    printf "%s\n" "$ESC_SEQ${line#    }"
                 }
                 ;;
+            "") 
+                $codeblock \
+                    && printf "\n%s" "$ESC_SEQ" \
+                    || printf "\n"
+                    ;;
             *)
                 $codeblock && {
                     printf "
\n" @@ -323,19 +363,8 @@ _blockquote () { print_x $((indent_level)) "\n" } -_post_escape () { - while IFS= read -r line; do - case "$line" in - "$ESC_SEQ"*) - printf "%s\n" "${line#??}" - ;; - *) - printf "%s\n" "$line" - ;; - esac - done -} - +# add html header +# _html () { printf "\n" while IFS= read -r line; do @@ -343,30 +372,51 @@ _html () { done } +# remove all unecessary newlines +# +_squash () { + while IFS= read -r line; do + case "$line" in + "$ESC_SEQ"*) + printf "\n%s" "${line#??}" + ;; + *) + printf "%s" "$line" + ;; + esac + done + printf "\n" +} + + # convert the markdown from stdin into html # md2html () { # the order of these somewhat matters - _pre_emph \ + _pre_strip \ | _code \ + | _pre_emph \ | _blockquote \ | _ul \ | _ol \ | _p \ + | _a_img \ + | _inline_code \ | _emph '__' "" "" \ | _emph '_' "" "" \ - | _emph '`' "" "" \ | _post_emph \ - | _a_img \ | _h 6 \ | _h 5 \ | _h 4 \ | _h 3 \ | _h 2 \ | _h 1 \ - | _post_escape \ + | _squash \ | _html + + cat > /dev/null << EOF +EOF } md2html diff --git a/src/test.md b/src/test.md index a4a3f67..203d425 100644 --- a/src/test.md +++ b/src/test.md @@ -1,18 +1,22 @@ # This is a test md file hello -This is *italics* this is **bold** this is ***both*** wow +This is *italics* this is **bold** this is ***both*** wow (this is in brackets ssh) and [this is in square brackets not a anchor lol] click [here](http) for stuff and [over here](http12 "my title") for more +and [click here](http://this_has_stuffinside) too + ![this is an image](httpsomething) this is a paragraph +with many lines +that are joined together > this is a quote hi ok that was a quote -> this quote has a list inside i +> this quote has a list inside it > - this is a list in a quote > - it was quoted > @@ -29,8 +33,12 @@ haha ok - int main() { - printf("hello %s\n", "world"); + int main() { + printf("hello %s\n", "world"); + } + + int func(int* a) { + return a; } - list @@ -44,10 +52,10 @@ ok + plus now wow + plus wow so cool - + 1. hello 2. world 3. lOL - + > ok that worked?