Automated Gemini text conversion for Lighttpd

In my last article I adapted an awk script from Omar Polo to convert my Gemini text pages to HTML:

Convert Gemini text format to HTML.

Install and configure lighttpd server

In the meantime, I installed a lighttpd server on my Arch Linux server ...

pacman -S lighttpd

Now we should change the configuration /etc/lighttpd/lighttpd.conf of the webserver to fit our needs:

# This is a minimal example config
server.port = 80
server.username = "http
server.groupname = "http
server.document-root = "/srv/http"
server.errorlog = "/var/log/lighttpd/error.log"
dir-listing.activate = "enable"
index-file.names = ( "index.html" )
mimetype.assign = ( ".html" => "text/html" )

In this case, the web server can really only serve HTML files, nothing else.

Now we start the web server and configure it to start automatically on reboot:

systemctl start lighttpd.service
systemctl enable lighttpd.service

With this part we are done. Now, of course, we need some HTML files to display.

Customize wrapper script

I customized my gem2html converter script to drop the HTML right into the folder from lighttpd. This directory is by default /srv/http on Arch-Linux.

gmi2html shell script adapted for automation

My Gemini files are stored in /srv/gemini/content, but you can do that as you like. The customized shell script now looks like this, in the third and fourth line are the customizations:

#!/bin/bash

htmldir="/srv/http"
cd /srv/gemini/content/
for file in `find . -mtime -1 -type f -name "*.gmi"`
do
   barefile=`basename -s .gmi $file`
   outfile="$htmldir/$file"
   outdir=`dirname $outfile`
   mkdir -p $outdir
   outfile="$outdir/$barefile.html"

   title=$(cat $file|grep -m 1 '^# '|sed 's/#//'|awk '{$1=$1;print}')
   gem2html.awk $file | sed "s/TITLEEE/$title/" > $outfile
done

Likewise, "-mtime -1" was added to the find command so that only all HTML files newer than one day are rebuilt.

Automation with systemd

Systemd allows to set our gem2html script as a service with two lines and to execute it regularly with another three lines, completely without cron. In this example this is done hourly, but it could also be done once a day.

So we need to enter a service configuration and a timer configuration at systemd:

Write the service file:

# file /etc/system/system/gem2html.service

[Unit]
Description=Convert Gemini files to HTML

[Service]
ExecStart=/usr/local/bin/gem2html

However, this service is not called directly, but periodically, in this case hourly, by a timer:

# file /etc/system/system/gem2html.timer

[Unit]
Description=Rebuild html files from Gemini files every hour

[Timer]
OnCalendar=hourly

[Install]
WantedBy=basic.target

Now that we have both config files, let's just start the timer immediately and let it do its work every hour:

systemctl enable --now gem2html.timer

We can also see the status and see if everything is working:

systemctl status gem2html.timer

This should look something like this

● gem2html.timer - Rebuild html files from Gemini files every hour
     Loaded: loaded (/etc/system/system/gem2html.timer; enabled; vendor preset: disabled)
     Active: active (waiting) since Sat 2021-02-13 17:11:30 CET; 3h 43min ago
    Trigger: Sat 2021-02-13 21:00:00 CET; 4min 55s left
   Triggers: ● gem2html.service

Feb 13 17:11:30 hubbz systemd[1]: Started Rebuild html files from Gemini files every hour.

That's it. The conversion doesn't happen immediately, it happens to every hour. And so now you get a web site with a copy of your Gemini capsule (the insider's name for a Gemini web site). Have fun!

🌐 The scripts are licensed under the MIT license.

Back
Data Privacy