Spool Five

Shell Script - Crosspost

In previous posts, I mentioned how I’ve started trying to learn a bit more about shell scripting in my free time.

First attempt

Second attempt

This was due to the simplicity of Gemini, and the ease of working with Gemini files in the command line. Gemini encourages this kind of tinkering and play with its surrounding apparatus (as can be seen by the many clients/servers). The second script was an example of something to improve my workflow. As it turned out, someone else found my post and showed me a much better/more sophisticated way to achieve what I want. I still use his solution all the time to compile my gemlog index.

Fixato - Writing an Index of Gemini Files

Anyway, in search of further optimisations to my workflow, I’ve tried to write another simple little script to automate crossposting my gemini posts to the web. Admittedly, most of the heavy lifting here is done by two other scripts, “Blop” and “gmi2mkd” by Drew/uoou/friendo and Tidux respectively.

Gemini to markdown at Gitlab

Blop at Gitlab

My main focus is Gemini/Gopher posts these days. So, I basically wanted a way to automatically add the gemini post I’d finished writing/publishing to my html website.

The Header

The website is generated from markdown files using Blop. Each markdown file has the following yaml-style header:

    ---
    title: 
    date: 
    ---

Blop then uses that information to print a header and date for each html post.

The Script

The script I made just adds this header to the gemini file, deletes the first gemini heading (otherwise it would be printed twice), and uses the gemini heading in the ’title’ field. It also then changes the ‘footer’ from a link to the ‘gemlog’ index, to a link to the ‘web’ posts index.

Next, I used the awk function from the ‘gmi2mkd’ script to change all the links from gemini syntax to markdown syntax.

Then, it changes the file extension from “.gmi.md” to just “.md”. This part had me scratching my head a bit. I’m sure there must be a simpler way to do it.

Finally, it asks whether it should recompile the website (run Blop). I just copied this section from a tutorial on how to use “yes/no” prompts in bash. I have no idea what it going on here with the parentheses.

Of course, the main thing it can’t do is fix the links to internal places on the site/capsule, like media links. That’s okay though, I don’t mind manually changing those for now.

#!/bin/sh

dest=/path/to/markdown/folder
head=/path/to/headerfile/header.txt
file=$1
title=$(head -n 1 "$file" | sed 's/\#\ //')
dt=$(date +%Y-%m-%d)

# Do the stuff. '5d' removes First Gemini Heading.
# Awk section from gmi2mkd script
cat $head "$file" |
    sed -e "s/\Shell Script - Crosspost/$title/" \
        -e "s/\2021-05-02/$dt/" \
        -e "5d" \
        -e "s/index\.gmi/\.\.\/archive\.html/" \
        -e "s/All Posts/All\ Posts/" |
    awk '{ if ($1 == "=>") {
        URL=$2;
        $1="";
        $2="";
        sub("  ", "");
        print "[" $0 "](" URL ")\n";
    }
    else {
        print;
    }
}' > "$dest/$file.md"

#Change file extension
for file in "$dest"/*.gmi.md ; do mv "$file" "$(echo "$file" | sed 's/\.gmi//')" ; done

# Run Blop
read -r -p "Recompile Weblog? [Y/n] " input

case $input in
    [yY])
        echo "Running Blop..."
        cd /path/to/webdir || exit
        blop
        ;;
    [nN])
        echo "Bye!"
        ;;
    *)
        echo "Invalid input..."
        exit 1
        ;;
esac

Sun May 2, 2021 - 553 Words

Tags: linux