From 3545ac1c169508870b8d577ce7646036023e74c5 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 17 May 2021 21:02:55 +0100 Subject: refactor code --- packages/html.source.sh | 145 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 packages/html.source.sh (limited to 'packages/html.source.sh') diff --git a/packages/html.source.sh b/packages/html.source.sh new file mode 100644 index 00000000..d8d2fc66 --- /dev/null +++ b/packages/html.source.sh @@ -0,0 +1,145 @@ +html::special () { + $SED ' + s|\&|\&|g; + s|<|\<|g; + s|>|\>|g; + ' <<< "$@" +} + +html::make_paragraph () { + local -r text="$1"; shift + test -n "$text" && echo "

$(html::special "$text")

" +} + +html::make_heading () { + local -r text=$($SED -E 's/^#+ //' <<< "$1"); shift + local -r level="$1"; shift + echo "$(html::special "$text")" +} + +html::make_quote () { + local -r quote="${1/> }" + echo "

$(html::special "$quote")

" +} + +html::make_img () { + local link="$1"; shift + local descr="$1"; shift + + if [ -z "$descr" ]; then + echo -n "" + else + echo -n "$descr:" + echo -n "\"$descr\"" + fi + + echo "
" +} + +html::make_link () { + local link="$1"; shift + local descr="$1"; shift + + grep -F -q '://' <<< "$link" || link=${link/.gmi/.html} + test -z "$descr" && descr="$link" + echo "$descr
" +} + +html::fromgmi () { + local is_list=no + local is_plain=no + + while IFS='' read -r line; do + if [[ "$is_list" == yes ]]; then + if [[ "$line" == '* '* ]]; then + echo "
  • $(html::special "${line/\* /}")
  • " + else + is_list=no + echo "" + fi + continue + + elif [[ "$is_plain" == yes ]]; then + if [[ "$line" == '```'* ]]; then + echo "" + is_plain=no + else + html::special "$line" + fi + continue + fi + + case "$line" in + '* '*) + is_list=yes + echo "