From 2a3fa5b8c749e631d2d7ed1bb01300f023c4b9ea Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 25 May 2021 22:10:09 +0100 Subject: rename packages to lib --- lib/html.source.sh | 162 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 lib/html.source.sh (limited to 'lib/html.source.sh') diff --git a/lib/html.source.sh b/lib/html.source.sh new file mode 100644 index 0000000..6049c2f --- /dev/null +++ b/lib/html.source.sh @@ -0,0 +1,162 @@ +# Convert special characters to their HTML codes +html::encode () { + $SED ' + s|\&|\&|g; + s|<|\<|g; + s|>|\>|g; + ' <<< "$@" +} + +# Make a HTML paragraph. +html::make_paragraph () { + local -r text="$1"; shift + + if [[ -n "$text" ]]; then + echo "

$(html::encode "$text")

" + fi +} + +# Make a HTML header. +html::make_heading () { + local -r text=$($SED -E 's/^#+ //' <<< "$1"); shift + local -r level="$1"; shift + echo "$(html::encode "$text")" +} + +# Make a HTML quotation +html::make_quote () { + local -r quote="${1/> }" + echo "

$(html::encode "$quote")

" +} + +# Make a HTML image +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 "
" +} + +# Make a HTML hyperlink +html::make_link () { + local link="$1"; shift + local descr="$1"; shift + + if ! $GREP -F -q '://' <<< "$link"; then + link=${link/.gmi/.html} + fi + + if [[ -z "$descr" ]]; then + descr="$link" + fi + + echo "$descr
" +} + +# Convert Gemtext to HTML +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::encode "${line/\* /}")
  • " + else + is_list=no + echo "" + fi + continue + + elif [[ "$is_plain" == yes ]]; then + if [[ "$line" == '```'* ]]; then + echo "" + is_plain=no + else + html::encode "$line" + fi + continue + fi + + case "$line" in + '* '*) + is_list=yes + echo "