From 1079f927a27db9d194c8e25eb3a188396fdf8eab 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 0000000..d8d2fc6 --- /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 "" @@ -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 From 6aee04354e443e2121bf6c558be227388a10e7b2 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 17 May 2021 21:23:06 +0100 Subject: reword comments --- packages/html.source.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'packages/html.source.sh') diff --git a/packages/html.source.sh b/packages/html.source.sh index d295c7a..cc79e61 100644 --- a/packages/html.source.sh +++ b/packages/html.source.sh @@ -51,7 +51,7 @@ html::make_link () { echo "$descr
    " } -## Convert Gemtext to HTML +# Convert Gemtext to HTML html::fromgmi () { local is_list=no local is_plain=no @@ -108,7 +108,7 @@ html::fromgmi () { done } -## Test HTML package. +# 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 From 641a95de7bdd64963666cca6b96387ab5d9245e2 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 19 May 2021 10:06:02 +0100 Subject: some refactoring and also ensured that it works on macOS (given Bash 5 is installed) --- packages/html.source.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/html.source.sh') diff --git a/packages/html.source.sh b/packages/html.source.sh index cc79e61..5a292f1 100644 --- a/packages/html.source.sh +++ b/packages/html.source.sh @@ -46,7 +46,7 @@ html::make_link () { local link="$1"; shift local descr="$1"; shift - grep -F -q '://' <<< "$link" || link=${link/.gmi/.html} + $GREP -F -q '://' <<< "$link" || link=${link/.gmi/.html} test -z "$descr" && descr="$link" echo "$descr
    " } -- cgit v1.2.3 From 65f8a2497bad58619d0cfdc76e4a0efeb15e0306 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 20 May 2021 17:51:07 +0100 Subject: initial macos support --- packages/html.source.sh | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'packages/html.source.sh') diff --git a/packages/html.source.sh b/packages/html.source.sh index 5a292f1..a258781 100644 --- a/packages/html.source.sh +++ b/packages/html.source.sh @@ -10,7 +10,10 @@ html::encode () { # Make a HTML paragraph. html::make_paragraph () { local -r text="$1"; shift - test -n "$text" && echo "

    $(html::encode "$text")

    " + + if [[ -n "$text" ]]; then + echo "

    $(html::encode "$text")

    " + fi } # Make a HTML header. @@ -46,8 +49,13 @@ html::make_link () { local link="$1"; shift local descr="$1"; shift - $GREP -F -q '://' <<< "$link" || link=${link/.gmi/.html} - test -z "$descr" && descr="$link" + if $GREP -F -q '://' <<< "$link"; then + link=${link/.gmi/.html} + fi + if [[ -z "$descr" ]]; then + descr="$link" + fi + echo "$descr
    " } -- cgit v1.2.3 From 5eba1760c0db9b01f7498a1efbfb4d969b6f3051 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 20 May 2021 18:08:36 +0100 Subject: works now on macOS --- packages/html.source.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/html.source.sh') diff --git a/packages/html.source.sh b/packages/html.source.sh index a258781..efb9bf6 100644 --- a/packages/html.source.sh +++ b/packages/html.source.sh @@ -143,7 +143,7 @@ html::test () { assert::equals "$(generate::make_link html "$line")" \ 'https://example.org
    ' - line='=> index.gmi' + line='=> index.html' assert::equals "$(generate::make_link html "$line")" \ 'index.html
    ' -- cgit v1.2.3 From bd1adc6f9e2b22b5ea887519c4cc73cc08e36328 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 20 May 2021 18:16:51 +0100 Subject: fix macOS links --- packages/html.source.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'packages/html.source.sh') diff --git a/packages/html.source.sh b/packages/html.source.sh index efb9bf6..6049c2f 100644 --- a/packages/html.source.sh +++ b/packages/html.source.sh @@ -49,9 +49,10 @@ html::make_link () { local link="$1"; shift local descr="$1"; shift - if $GREP -F -q '://' <<< "$link"; then + if ! $GREP -F -q '://' <<< "$link"; then link=${link/.gmi/.html} fi + if [[ -z "$descr" ]]; then descr="$link" fi -- cgit v1.2.3