From f6deb276c7c5e7b3617d9ecd35193b43ae32ccbf Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 17 May 2021 21:19:35 +0100 Subject: refactor --- packages/html.source.sh | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'packages/html.source.sh') diff --git a/packages/html.source.sh b/packages/html.source.sh index d8d2fc6..d295c7a 100644 --- a/packages/html.source.sh +++ b/packages/html.source.sh @@ -1,4 +1,5 @@ -html::special () { +# Convert special characters to their HTML codes +html::encode () { $SED ' s|\&|\&|g; s|<|\<|g; @@ -6,22 +7,26 @@ html::special () { ' <<< "$@" } +# Make a HTML paragraph. html::make_paragraph () { local -r text="$1"; shift - test -n "$text" && echo "

$(html::special "$text")

" + test -n "$text" && echo "

$(html::encode "$text")

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

$(html::special "$quote")

" + echo "

$(html::encode "$quote")

" } +# Make a HTML image html::make_img () { local link="$1"; shift local descr="$1"; shift @@ -36,6 +41,7 @@ html::make_img () { echo "
" } +# Make a HTML hyperlink html::make_link () { local link="$1"; shift local descr="$1"; shift @@ -45,6 +51,7 @@ html::make_link () { echo "$descr
" } +## Convert Gemtext to HTML html::fromgmi () { local is_list=no local is_plain=no @@ -52,7 +59,7 @@ html::fromgmi () { while IFS='' read -r line; do if [[ "$is_list" == yes ]]; then if [[ "$line" == '* '* ]]; then - echo "
  • $(html::special "${line/\* /}")
  • " + echo "
  • $(html::encode "${line/\* /}")
  • " else is_list=no echo "" @@ -64,7 +71,7 @@ html::fromgmi () { echo "" is_plain=no else - html::special "$line" + html::encode "$line" fi continue fi @@ -101,6 +108,7 @@ html::fromgmi () { done } +## Test HTML package. html::test () { local line='Hello world! This is a paragraph.' assert::equals "$(html::make_paragraph "$line")" '

    Hello world! This is a paragraph.

    ' -- cgit v1.2.3