Tags with Text Processing in Lua


date: 2023-01-03


One of the features that I wanted to add to my website after migrating from Hugo to Lichen was "tags". Being able to look at posts grouped by topics as defined by tags is useful.


I knew roughly how I wanted to do this, but knew that I couldn't quickly write it out myself. I talked with one of my sons and together we got to work on it.


To be clear, my son did all of the coding at the keyboard. I was his pair programming partner, explaining the concepts, guiding him and pointing out bugs or unexpected behaviors.


We are quite pleased with what we were able to accomplish today!


He chose Lua because he's used that language quite a bit and it seemed a natural choice for the text processing that we wanted to do. Lua is also one of our preferred RetroEdge programming lanugages (C, Lua, PHP, Forth, Zig, V, Scheme and Go).


Logic


We look through each .gmi gemtext file and find the line that contains "tags:<space>", then identify and store in a Lua table the tags listed on that line. Once we have that, we create a .gmi gemtext page for each tag and a page that contains all tags.


Index of all tags

Tag: linux

Tag: lua


We create the files as .gmi gemtext files and not html because we let the Lichen CMS build script do the work of converting .gmi to .html for us.


After that, my son wrote a post-processor to go over the html files that Lichen generates and transform the hashtags at the bottom of each post to links to the html page for that tag. We didn't do this as gemtext because in gemtext each link has to be on its own line and I wanted all the tags to remain on a single line in the html version of the page.


This does mean that the link to tags feature won't be present in the gemini version, but that's okay as I will still have an index of all the tags as a gemtext document. I also haven't yet published these pages on a gemini server, but I plan to.


We do also plan to publish the Lua code for this tag feature extension to Lichen. We edited the Lichen build script to call the first Lua script that generates the gemtext tag pages before Lichen converts the .gmi files to .html and then called the html link transform Lua script after Lichen creates the html files. So I can build the whole website with the same one ./build.sh command.


Conclusion


I am very pleased with how quickly we went from an idea to a fully working implementation of that idea. It just took a few hours of working together on it. Text processing is powerful. I hope to continue to work with the idea of data as plain text and then writing programs to analyze and transform that data. These blog posts, written in gemtext, but transformed to html is an example of that. The tagging system we created is a useful extension.


tags: #lua #plaintext #gemtext #programming