diff options
author | davidovski <david@davidovski.xyz> | 2023-02-18 01:19:59 +0000 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2023-02-18 01:19:59 +0000 |
commit | e8e91a50251993f40c1beb3db1eb5546958242b7 (patch) | |
tree | edbd079cca2cf10a039a038cd63353a4b5aef394 | |
parent | e13af149eaa5d8f83437e273b17a535d250a4cde (diff) |
Add url and image parsing
-rwxr-xr-x | src/md2html.sh | 50 | ||||
-rw-r--r-- | 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<img src=\"%s\"%s alt=\"%s\"></img>" + before="${before%!}" ;; + *) h="%s<a href=\"%s\"%s>%s</a>" ;; + 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 '__' "<strong>" "</strong>" \ | _emph '_' "<em>" "</em>" \ + | _emph '`' "<code>" "</code>" \ | _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 |