From dd541cc6dedf8a911486e326c68254f0da234e47 Mon Sep 17 00:00:00 2001 From: davidovki Date: Sun, 5 Mar 2023 03:41:32 +0000 Subject: Fix extra codeblock --- src/md2html.sh | 51 +++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/src/md2html.sh b/src/md2html.sh index afac651..776527b 100755 --- a/src/md2html.sh +++ b/src/md2html.sh @@ -1,12 +1,12 @@ #!/bin/sh -ESC_SEQ="ESCAPED!" +ESC_SEQ='\0' # replace all * with _ for easier processing # _pre_emph () { while IFS= read -r line; do - case "$line" in "$ESC_SEQ"*) printf "%s\n"" $line" && continue;; esac + case "$line" in "$ESC_SEQ"*) printf "%s\n" "$line" && continue;; esac while [ "$line" != "${line%%\**}" ]; do printf "%s_" "${line%%\**}" line="${line#*\*}" @@ -131,29 +131,29 @@ _a_img () { local open="[" mid="](" close=")" while IFS= read -r line; do case "$line" in "$ESC_SEQ"*) printf "%s\n" "$line" && continue;; esac - next="$line" - while [ "$next" != "${next#*$close}" ]; do - before="${next%%$open*}" - text=${next#*$open} text=${text%%$mid*} - url=${next#*$mid} url=${url%%$close*} - - title=${url#* } url=${url%% *} - - [ "$title" != "$url" ] \ - && title=" title=$title" \ - || title= - - case "$before" in - *!) h="%s\"%s\"" - before="${before%!}" ;; - *) h="%s%s" ;; - esac + next="$line" + while [ "$next" != "${next#*$close}" ]; do + before="${next%%$open*}" + text=${next#*$open} text=${text%%$mid*} + url=${next#*$mid} url=${url%%$close*} - printf "$h" "$before" "$url" "$title" "$text" + title=${url#* } url=${url%% *} - next="${next#*$close}" - done - printf "%s\n" "$next"; + [ "$title" != "$url" ] \ + && title=" title=$title" \ + || title= + + case "$before" in + *!) h="%s\"%s\"" + before="${before%!}" ;; + *) h="%s%s" ;; + esac + + printf "$h" "$before" "$url" "$title" "$text" + + next="${next#*$close}" + done + printf "%s\n" "$next"; done } @@ -270,8 +270,7 @@ _code () { " "*) $codeblock && printf "%s\n" "$ESC_SEQ${line# }" || - printf "%s\n" "$line" - $codeblock || $content || { + $content || { printf "
\n"
                     codeblock=true
                     printf "%s\n" "$ESC_SEQ${line#    }"
@@ -328,7 +327,7 @@ _post_escape () {
     while IFS= read -r line; do
         case "$line" in
             "$ESC_SEQ"*)
-                printf "%s\n" "${line#$ESC_SEQ}"
+                printf "%s\n" "${line#??}"
                 ;;
             *)
                 printf "%s\n" "$line"
-- 
cgit v1.2.1