From e8e91a50251993f40c1beb3db1eb5546958242b7 Mon Sep 17 00:00:00 2001 From: davidovski Date: Sat, 18 Feb 2023 01:19:59 +0000 Subject: Add url and image parsing --- src/md2html.sh | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- src/test.md | 5 ++++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/md2html.sh b/src/md2html.sh index f675399..1c06e2d 100755 --- a/src/md2html.sh +++ b/src/md2html.sh @@ -8,7 +8,7 @@ _pre_emph () { printf "%s_" "${line%%\**}" line="${line#*\*}" done - printf "${line}\n" + printf "%s\n" "${line}" done } @@ -25,7 +25,7 @@ _post_emph () { printf "%s${right}" "${line%%${wrong}*}" line="${line#*${wrong}}" done - printf "${line}\n" + printf "%s\n" "${line}" done } @@ -82,7 +82,6 @@ _h () { # # p _p () { - local num=$1 empty=true while IFS= read -r line; do case "$line" in @@ -109,15 +108,60 @@ _p () { } } +# parse links +# +_a_img () { + local open="[" mid="](" close=")" + while IFS= read -r line; do + 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 + + printf "$h" "$before" "$url" "$title" "$text" + + next="${next#*$close}" + done + printf "%s\n" "$next"; + done +} + +# parse unordered lists +# +_ul () { +} + +# parse ordered lists +# +_ol () { +} + + # convert the markdown from stdin into html # md2html () { + _p \ | _pre_emph \ | _emph '__' "" "" \ | _emph '_' "" "" \ + | _emph '`' "" "" \ | _post_emph \ + | _a_img \ | _h 6 \ | _h 5 \ | _h 4 \ diff --git a/src/test.md b/src/test.md index 15bd4a1..6aacbed 100644 --- a/src/test.md +++ b/src/test.md @@ -2,7 +2,10 @@ This is *italics* this is **bold** this is ***both*** wow -so hold tight +click [here](http) for stuff and [over here](http12 "my title") for more + +![this is an image](httpsomething) + this is a paragraph haha -- cgit v1.2.1