From 95f973b3a4f612c991bdf40e14315ea3707b0663 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 11 May 2021 05:57:02 +0100 Subject: initial markdown support add generic functions --- README.md | 5 --- buetow.org.sh | 122 ++++++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 94 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 59fe9e2d..09baeb78 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,3 @@ This is the source code of my Gemini capsule. You can reach the site(s) here: ## HTTP fallback software used * Web server: [Apache HTTPD](https://httpd.apache.org) - -> This is a quote test. - -Jo jo jo. - diff --git a/buetow.org.sh b/buetow.org.sh index 4d54694d..ab2e84d9 100755 --- a/buetow.org.sh +++ b/buetow.org.sh @@ -185,6 +185,41 @@ ATOMFOOTER fi } +## Generic module + +generic::link () { + local -r what="$1"; shift + local -r line="${1/=> }"; shift + local link + local descr + + while read -r token; do + if [ -z "$link" ]; then + link="$token" + elif [ -z "$descr" ]; then + descr="$token" + else + descr="$descr $token" + fi + done < <(echo "$line" | tr ' ' '\n') + + if grep -E -q "$IMAGE_PATTERN" <<< "$link"; then + if [ $what == markdown ]; then + markdown::img "$link" "$descr" + else + html::img "$link" "$(html::special "$descr")" + fi + return + fi + + if [ $what == markdown ]; then + html::markdown "$link" "$descr" + else + html::link "$link" "$(html::special "$descr")" + fi +} + + ## HTML module html::special () { @@ -222,35 +257,16 @@ html::img () { echo -n "$descr:" echo -n "\"$descr\"" fi + echo "
" } html::link () { - local -r line="${1/=> }" - local link - local descr - - while read -r token; do - if [ -z "$link" ]; then - link="$token" - elif [ -z "$descr" ]; then - descr="$token" - else - descr="$descr $token" - fi - done < <(echo "$line" | tr ' ' '\n') - descr=$(html::special "$descr") - - if grep -E -q "$IMAGE_PATTERN" <<< "$link"; then - html::img "$link" "$descr" - return - fi + local link="$1"; shift + local descr="$1"; shift - # If relative link convert .gmi to .html grep -F -q '://' <<< "$link" || link=${link/.gmi/.html} - # If no description use link itself test -z "$descr" && descr="$link" - echo "$descr
" } @@ -302,7 +318,7 @@ html::gemini2html () { html::quote "$line" ;; '=> '*) - html::link "$line" + generic::link html "$line" ;; *) html::paragraph "$line" @@ -374,26 +390,74 @@ html::test () { assert::equals "$(html::quote "$line")" "
This is a quote
" line="=> https://example.org" - assert::equals "$(html::link "$line")" \ + assert::equals "$(generic::link html "$line")" \ "https://example.org
" line="=> index.gmi" - assert::equals "$(html::link "$line")" \ + assert::equals "$(generic::link html "$line")" \ "index.html
" line="=> http://example.org Description of the link" - assert::equals "$(html::link "$line")" \ + assert::equals "$(generic::link html "$line")" \ + "Description of the link
" + + line="=> http://example.org/image.png" + assert::equals "$(generic::link html "$line")" \ + "
" + + line="=> http://example.org/image.png Image description" + assert::equals "$(generic::link html "$line")" \ + "Image description:\"Image
" +} + +## Markdown module + +markdown::img () { + local link="$1"; shift + local descr="$1"; shift + + if [ -z "$descr" ]; then + echo -n "" + else + echo -n "$descr:" + echo -n "\"$descr\"" + fi + + echo "
" +} + +markdown::link () { + local link="$1"; shift + local -r descr="$1"; shift + + grep -F -q '://' <<< "$link" || link=${link/.gmi/.html} + test -z "$descr" && descr="$link" + echo "$descr
" +} + +markdown::test () { + local line="=> https://example.org" + assert::equals "$(generic::link markdown "$line")" \ + "https://example.org
" + + line="=> index.md" + assert::equals "$(generic::link markdown "$line")" \ + "index.md
" + + line="=> http://example.org Description of the link" + assert::equals "$(generic::link markdown "$line")" \ "Description of the link
" line="=> http://example.org/image.png" - assert::equals "$(html::link "$line")" \ + assert::equals "$(generic::link markdown "$line")" \ "
" line="=> http://example.org/image.png Image description" - assert::equals "$(html::link "$line")" \ + assert::equals "$(generic::link markdown "$line")" \ "Image description:\"Image
" } + ### MAIN module main::help () { @@ -416,8 +480,10 @@ case $ARG in ;; --generate) html::test + # markdown::test gemfeed::generate atomfeed::generate + # markdown::generate html::generate ;; --help|*) -- cgit v1.2.3