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/md.source.sh | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 packages/md.source.sh (limited to 'packages/md.source.sh') diff --git a/packages/md.source.sh b/packages/md.source.sh new file mode 100644 index 0000000..197bdcf --- /dev/null +++ b/packages/md.source.sh @@ -0,0 +1,55 @@ +md::make_img () { + local link="$1"; shift + local descr="$1"; shift + + if [ -z "$descr" ]; then + echo "[![$link]($link)]($link) " + else + echo "[![$descr]($link \"$descr\")]($link) " + fi +} + +md::make_link () { + local link="$1"; shift + local descr="$1"; shift + + grep -F -q '://' <<< "$link" || link=${link/.gmi/.md} + test -z "$descr" && descr="$link" + + echo "[$descr]($link) " +} + +md::test () { + local line='=> https://example.org' + assert::equals "$(generate::make_link md "$line")" \ + '[https://example.org](https://example.org) ' + + line='=> index.md' + assert::equals "$(generate::make_link md "$line")" \ + '[index.md](index.md) ' + + line='=> http://example.org Description of the link' + assert::equals "$(generate::make_link md "$line")" \ + '[Description of the link](http://example.org) ' + + line='=> http://example.org/image.png' + assert::equals "$(generate::make_link md "$line")" \ + '[![http://example.org/image.png](http://example.org/image.png)](http://example.org/image.png) ' + + line='=> http://example.org/image.png Image description' + assert::equals "$(generate::make_link md "$line")" \ + '[![Image description](http://example.org/image.png "Image description")](http://example.org/image.png) ' +} + +md::fromgmi () { + while IFS='' read -r line; do + case "$line" in + '=> '*) + generate::make_link md "$line" + ;; + *) + echo "$line" + ;; + esac + done +} -- cgit v1.2.3 From f6deb276c7c5e7b3617d9ecd35193b43ae32ccbf Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 17 May 2021 21:19:35 +0100 Subject: refactor --- packages/md.source.sh | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'packages/md.source.sh') diff --git a/packages/md.source.sh b/packages/md.source.sh index 197bdcf..2f85b3c 100644 --- a/packages/md.source.sh +++ b/packages/md.source.sh @@ -1,3 +1,4 @@ +# Make a Markdown image. md::make_img () { local link="$1"; shift local descr="$1"; shift @@ -9,6 +10,7 @@ md::make_img () { fi } +# Make a Markdown hyperlink. md::make_link () { local link="$1"; shift local descr="$1"; shift @@ -19,6 +21,21 @@ md::make_link () { echo "[$descr]($link) " } +# Convert Gemtext to Markdown. +md::fromgmi () { + while IFS='' read -r line; do + case "$line" in + '=> '*) + generate::make_link md "$line" + ;; + *) + echo "$line" + ;; + esac + done +} + +# Test the Markdown package. md::test () { local line='=> https://example.org' assert::equals "$(generate::make_link md "$line")" \ @@ -40,16 +57,3 @@ md::test () { assert::equals "$(generate::make_link md "$line")" \ '[![Image description](http://example.org/image.png "Image description")](http://example.org/image.png) ' } - -md::fromgmi () { - while IFS='' read -r line; do - case "$line" in - '=> '*) - generate::make_link md "$line" - ;; - *) - echo "$line" - ;; - esac - done -} -- 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/md.source.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/md.source.sh') diff --git a/packages/md.source.sh b/packages/md.source.sh index 2f85b3c..957b9cf 100644 --- a/packages/md.source.sh +++ b/packages/md.source.sh @@ -15,7 +15,7 @@ md::make_link () { local link="$1"; shift local descr="$1"; shift - grep -F -q '://' <<< "$link" || link=${link/.gmi/.md} + $GREP -F -q '://' <<< "$link" || link=${link/.gmi/.md} test -z "$descr" && descr="$link" echo "[$descr]($link) " -- 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/md.source.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'packages/md.source.sh') diff --git a/packages/md.source.sh b/packages/md.source.sh index 957b9cf..ce190dd 100644 --- a/packages/md.source.sh +++ b/packages/md.source.sh @@ -15,8 +15,12 @@ md::make_link () { local link="$1"; shift local descr="$1"; shift - $GREP -F -q '://' <<< "$link" || link=${link/.gmi/.md} - test -z "$descr" && descr="$link" + if $GREP -F -q '://' <<< "$link"; then + link=${link/.gmi/.md} + fi + if [[ -z "$descr" ]]; then + descr="$link" + fi echo "[$descr]($link) " } -- cgit v1.2.3 From 997f742161d509794043c173af2c1d0cae059f79 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 20 May 2021 18:19:00 +0100 Subject: fix md links --- packages/md.source.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/md.source.sh') diff --git a/packages/md.source.sh b/packages/md.source.sh index ce190dd..e7bfae1 100644 --- a/packages/md.source.sh +++ b/packages/md.source.sh @@ -15,7 +15,7 @@ md::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/.md} fi if [[ -z "$descr" ]]; then -- cgit v1.2.3