💾 Archived View for tilde.team › ~benk › e9abcfc5.gmi captured on 2021-12-06 at 14:29:53. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-11-30)

-=-=-=-=-=-=-

Extending Diohsc With Bash

Authors: Ben K. <benk@tilde.team>

Date: 2021-05-02

This is a little bit heavy to be considered a "tip", but I thought it was interesting enough to share:

In Diohsc, when you invoke a system command it will provide the current document to it as a file argument along with a couple useful environment variables, namely URI and MIMETYPE. This means that you can extend the functionality of the client easily by providing your own programs that handle this information. These in turn can become part of command aliases in Diohsc itself.

My first example was to write a script called "diohsc-toot" which calls the program "toot" to post on Mastodon. I already had toot set up in advance, and what I wanted Diohsc to do is share or "toot" (think "tweet") pages when I tell it to. The resulting script went in ~/.local/bin and contains:

#!/bin/sh
title=`sed -n '/^##/b;s/^#\s*//p;tq;b;:q;q' $1`
echo $title
toot post "$title $URI"

This takes a file as an argument and tries to find a gemdoc title in it, tooting the result with the URI. The original version I wrote was more complicated and would check if there is a title or not and toot accordingly, but later I realized that the toot command ignores whitespace preceeding a post's content, so if no title is found just the URI will be posted as originally planned. Even if the file happens to be an incompatible type (like an image), we can assume grep will find nothing. (If one day it outputs a bunch of gibberish, I'll add a mime type check.)

Next, I put the alias definition in ~/.diohsc/diohscrc:

alias toot !diohsc-toot

That's about it. However, I was not content to stop there. As you might know from some of my earlier posts, this gemlog is made with the help of what started out as a very, very basic script that grew over time. Essentially what it does is create a post, allow me to edit it, and then adds it to index.gmi for the gemlog and invokes rsync to upload it to the server.

What I realized is that I could make just a small modification to the script so that, if called by Diohsc, it would start a reply post. These were the affected parts:

# Get Title
if ! [ -z "$1" ]; then
  if [ -f "$1" ]; then
    title="Re: `sed -n '/^##/b;s/^#\s*//p;tq;b;:q;q' $1`"
  else
    echo File not found.
    exit 1
  fi
fi

# Prepare Post
cd $logdir
echo -e "# $title\nAuthor: $author <$email>\n" > newpost.gmi
if [ ! -z $URI ]; then
  echo -e "=> $URI\n" >> newpost.gmi
fi

Basically, if it's given a file, it looks for its title and makes the subject of the post "Re: ...", and if URI exists then references it at the start of the post like I usually do. The beauty of it is that it works just fine on its own without Diohsc, but it knows when Diohsc has given it something. Then I can define its alias:

alias Reply !newpost

And now when I'm browsing Diohsc I can simply enter "R" to start a reply. I haven't officially posted anything yet using the added functionality, but that's going to happen soon!