~chambln/gmi

ecf56432259c77b590e65d1b7c3330d5d831b27b — Gregory Chamberlain 2 months ago 66a75cf
Rewrite gmi2md in shell with some changes

* ‘gemini://’ URI schemes and ‘.gmi’ file name extensions are now
  converted to ‘http://’ and ‘.html’ respectively.

* Fixes an issue where link lines would be printed with trailing
  spaces (hence the patch herein for README.md).

* Probably slower than awk(1), but I don't know the Awk language well
  enough to write a correct implementation.
2 files changed, 27 insertions(+), 17 deletions(-)

M README.md
M contrib/gmi2md
M README.md => README.md +2 -2
@@ 2,8 2,8 @@

Tiny Gemini browser written in POSIX-compliant shell.

* [Project Gemini (on Gemini)](gemini://gemini.circumlunar.space)  
* [Project Gemini (on the Web)](https://gemini.circumlunar.space)  
* [Project Gemini (on Gemini)](http://gemini.circumlunar.space)
* [Project Gemini (on the Web)](https://gemini.circumlunar.space)

## Dependencies


M contrib/gmi2md => contrib/gmi2md +25 -15
@@ 1,15 1,25 @@
#!/bin/awk -f
# Usage: $0 < example.gmi > example.md
#
# Known issues:
#
# * Sometimes prints trailing spaces at the end of links.

$1 == "=>" {
    printf("* [%s", $3);
    for (i=4; i<=NF; i++) {
        printf("%s%s", FS, $i);
    }
    printf("](%s)  \n", $2);
}
$1 != "=>"
#!/bin/sh

while IFS= read -r line; do
    case $line in
        '=> '*)
            # Parse $line for the URI and link text.
            uri=${line#=> }; uri=${uri%% *}
            text=${line#=> $uri}; text=${text# }

            # Convert URI scheme and file name extension.
            case $uri in gemini://*) uri=http://${uri#gemini://}; esac
            case $uri in *.gmi) uri=${uri%.gmi}.html; esac

            # Print the link as a Markdown list item.
            case $text in
                '') printf '* <%s>\n' "$uri" ;;
                *)  printf '* [%s](%s)\n' "$text" "$uri" ;;
            esac
            ;;

        *)
            printf '%s\n' "$line"
            ;;
    esac
done