paritybit.ca

Snippets

Shell

Converting Gemtext to Markdown

This is a little snippet of shell script that I was using in sbs(1) until I removed it.

The first sed expression converts all local links that end in .gmi to .html (e.g. /blog/post1.gmi becomes /blog/post1.html). The second sed expression converts all gemini-style links to markdown-style links. The third sed expression fixes gemini links that only have a URL so that the URL will be displayed as the link text. The fourth sed expression converts links to images into Markdown’s image syntax, so images will be displayed with the <img> tag.

This wasn’t working on OpenBSD for some reason, but I didn’t bother to debug it since I removed the functionality from sbs anyways.

if [ "$(echo "$file" | awk -F\. '{print $NF}' )" = "gmi" ]; then
    printf "Converting: content%s/%s to markdown...\n" "$subDir" "$fileName"
    fileName=$(basename "$file" .gmi)
    sed -e 's/\(=>[ ]*\)\(.*\)\(.gmi\)\(.*\)/\1\2.html\4/g' \
        -e 's/=>[ ]*\([^ ]*\)\( \|\)\(.*\)/[\3](\1)\n/g' \
        -e 's/\[\](\(.*\))/[\1](\1)/g' \
        -e 's/\(\[.*\]\)\((\(.*.jpe\?g\|.*.png\))\)/!\1\2/g' \
        "$file" > /tmp/sbs/"$fileName".md
    file=/tmp/sbs/"$fileName".md
fi