ksh tutorial for managing the simplest imaginable blog with nothing but base OpenBSD

web ksh tutorial

A rerun of one's favorite show in lower definition

ksh tutorial for managing the simplest imaginable blog with nothing but base OpenBSD

The cyber piglin's howl dies down. The Techno-Mage reaches for the bloodied sword planted in the beast's back. Heavy blue and html elements begin pouring from the wound. She fixes her hair and tightens the hydraulic pressure valve on her MilTech arm. "Should've used gopher, amirite?" OpenBlade remarks.

Maybe this time is going to be different.

For the past few months I've been neglecting this website. The reader might already know I do not like html. Well, it's not quite true - I don't like the modern web - html is fine, or in the very least, it's not to blame. Finally I finished the 7th version of my korn web management system after vastly reducing my dependency on anything that's not in OpenBSD's base. I took some inspiration from werc, though I never read the code. I liked the way it looks and the simple philosophy behind it.

As stated in the previous article, the following is not how this website is managed, but it's the fundamental building block that you can improve upon. Perhaps you'll learn something new. This tutorial expects you to have a webserver running.

The minimal

Have a directory for your local content. Place in it your barebone html files which will be individual blog posts. Write them in html, but do not include head or body tags. See examples. Follow the traditional: yyyy-mm-dd-title-title-title.html naming convention. The index file will be generated through the script. For the sake of this tutorial, you only want the blog post files. We'll use sed in a function rm_html, which will strip html from the bits of the files and use it as h1 tag and meta description.

korn shell alchemy


#!/bin/ksh
database=/home/techomage/web
wroot=/var/www/htdocs/my.web.site

rm_html() {
  sed -e 's/<[^>]*>//g'
}

html_sandwich() {
cat <<eof_
<!doctype html>
<html lang="en-US">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta charset="utf-8">
  <meta name="description" content="$post_desc">
  <title>$post_title</title>
</head>
<body>
eof_

cat $file

cat <<eof_
</body>
</html>
eof_
}

cd $database

cat <<eof_ > tmp
<h1>My super simple website</h1>
<p>Whatever you want the index of your website to say.</p>
<p>The archive will be placed below:</p>
eof_

print "<ul>" >> tmp
for file in $(ls -r *html); do

 post_title=$(head -n1 $file | rm_html) 
 post_desc=$(sed -n 2p $file | rm_html)
 post_date=$(print $file | cut -d '-' -f -3)

   html_sandwich > $wroot/$file
  
  print "<li><a href=\"/$file\">$post_date - $post_title</a></li>" >> tmp

done

print "</ul>" >> tmp

file=tmp
html_sandwich > $wroot/index.html

Get the file for the script here

What

Firstly we set two variables. database is the location of the directory filled with nothing but the blog posts in the format yyyy-mm-dd-title.html. The second, wroot, is the root directory of our webserver.

The html_sandwich function glues together the barebones blog posts in html with proper head and body. This is the block you can edit to change html for the entire website, add a footer, styles, icons, whatever.

Next we do a single loop around the database, which takes all the *html files (we use ls -r, to list them in reverse, since we want the oldest ones at the bottom), extract from them a title, description and date and parse them through the sandwicher and send it to our website directory.

Finally we send in the generated index.html file to the root of the web server directory (which can be edited above the for loop). Beneath this block will be placed a list of all the blog posts.

examples

2022-01-24-this-is-a-post.html

This is what your posts in the database directory should look like, you only edit/make new ones here and upload them by running the script.

To make sure we extract the title of the post and the description according to the previous code, make sure the first line is always h1, and the second line a description (the type of tags don't matter).

<h1>This is a title</h1>
<p>This is a description.</p>

<p>This is the body of the post</p>
...

Conclusion

Of course your database directory could have other files, like about.html that you can exclude from the archive list and provide urls to the html_sandwicher. This is meant to show you how very simple it can be to not have to write your website by hand and not to use software which is not a part of OpenBSD.

Yes, you're still writing the posts in html. Next time we'll write our own plain text to html convertor, so we can live the gophernaut lifestyle in the surface world.

Look out below!