Thoughts of a Dragon

A collection of cool random stuff

BasHTML and Social Media

January 24, 2021 — Billy

Although the plugin system makes BasHTML extremely powerful, there is one thing I was still wondering about. In several other platforms, like WordPress, there are plugins that make it possible to announce new writing using social media. I could very easily write a plugin to announce after the post command completes, but if you happen to write on the local computer, and wait for a few minutes to sync to the server, there would be a link to a nonexistent article until the sync actually took place. So, how to solve this problem?


There is a utility called incron that works similar to cron, but is based on file system events instead of time. Using this utility, it is possible using a small script to post to social media when a new file is added. Here is a small script that will post when a new html file is detected created by BasHTML.

#!/bin/bash

# If you want to post to GNU Social, please fill out the following 3 variables.
gsUser="" # Your GNU Social user name.
gsPassword="" # Your GNU Social password
gsNode="" # Your node address, do not add trailing slashes, for example, https://example.org

filePath="$1"
fileName="$2"
if [[ "${fileName##*.}" != "html" ]]; then
    exit 0
fi
title="$(grep -m1 '^<title>.*</title>$' "${filePath}/${fileName}")"
title="${title#*>}"
title="${title%<*}"
blogName="$(grep -m1 -P '^<h1 .*index.*>.*</a></h1>$' "${filePath}/${fileName}")"
blogName="${blogName%</a></h1>}"
blogLink="${blogName%index.html\">*}"
blogName="${blogName#*index.html\">}"
blogLink="${blogLink#*href=\"}${fileName}"

message="There is a new post on \"$blogName\" called \"$title\". read it now at ${blogLink}"

# Post to all possible  destinations.
# Mastodon, and possibly Pleroma but untested, requires madonctl.
command -v madonctl &> /dev/null && madonctl toot "$message"
# GNU Social
[[ -n "$gsUser" && -n "$gsPassword" && -n "$gsNode" ]] && curl -s -u "${gsUser}:${gsPassword}" --data-urlencode status="${message}" -d source="${blogName}" ${gsNode}/api/statuses/update.json &> /dev/null

exit 0

Here is a link to the script in plain text. Once the script is in place, in a non-web accessible location, make it executable.

chmod 755 post.sh

If you want a bit of extra security, so your GNU Social password is not in plain text, you could compile the script using shc.

shc -f post.sh
mv post.sh.x post.sh
rm post.sh.x.c

Now, if you open post.sh with a text editor, you will see the contents are not easily read by the casual user. The script should continue to work as expected though.

The last step is to create an entry for it using incrontab.

sudo -u YourWebUser incrontab -e

Then create an entry for the script like this.

/path/to/watch IN_CREATE /path/to/post.sh

Path to watch in the command above is where your html files will appear. I hope you find this script useful. Thanks for reading.

Composed to the sound of The Coffinshakers - The Coffinshakers - Walpurgis Night

Tags: bashtml, social-media