sh is for shell

a website can be just a bunch of files. we can write a script to mush text and images and links together, so we can focus on making more text and images and links instead of managing websites. writing your own script means you can maybe change it when needed.

this text assumes you've discovered the terminal and the very basics such as folder navigation and file management. our site will be written in markdown and converted to html. we'll be scripting the shell. text within the codeblocks below can be copy-pasted into the terminal, and at some point i'll put together a collection of example files so it's easier to follow along.

here we go.

there is a file

cat file.md

you will see the contents of file.md printed on the screen, if file.md exists.

say it isn't so

echo "it isn't so"

but it is.

stop all the copy-paste

echo "the start" > start.md
echo "the end" > end.md

cat start.md > new.md
echo "the middle" >> new.md
cat end.md >> new.md

the > operator redirects the output to a file (here, new.md) instead of the screen. to append an existing file use >>.

word

talisman="sprinkles!"
echo $talisman

here we assign the variable talisman a text string. to print the value of the variable, we add a $ to the front. shell really doesn't like extra spaces around the equals sign.

everything all at once

list=$(ls -r ./*.md)
echo $list

the pattern $(command) is a thing of interest.

a variable now contains a list of all of the markdown files in the current folder.

when do we actually make something

for file in $list ; do
  echo $file
done

one per line, that's something.

days pass

d=$date(date -r new.md +%y%m%d)
echo $d

you suddenly became aware of the modification date of a file. all the percentage signs are just opinionated date format stuff, to which you can avail your recreational time by executing man date to see the in depth manual page.

i'm somewhat obsessed with providing dates to published material on the internet. you should be too.

cut

file=new.md
echo ${file%.*}

i would be lying if i told you i wrote this from memory.

all these symbols together just remove the file extension. the usefulness of this trick will be more clear soon hopefully.

outsourcing

cmark --unsafe new.md

"the unix way" is to connect a bunch of small tools together. cmark is a tool that converts markdown to html. get it via apt-get install cmark or brew install cmark or however you do things. the --unsafe tag allows any html you write inside your markdown to be passed through.

the script

#!/bin/sh

echo "hello!"

by copying these lines into a file, say hello.sh, and then changing the permissions of this file to allow execution, say chmod +x hello.sh, will turn this textfile into a run-able command, a spell of your very own. call upon it by typing ./hello.sh. the line at the top instructs the command line to use shell as the means to evaluate the script.

tie a huge tiny knot

#!/bin/sh

echo ">> root .md to .html"

list=$(ls -r ./*.md)
for file in $list ; do
  d=$(date -r $file +%y%m%d)
  file=${file%.*}
  echo $file
  target=${file}.html
  cat start.htm_ > ${target}
  cmark --unsafe ${file}.md >> ${target}
  echo "<hr><p>$d &mdash; tehn@nnnnnnnn.co</p>" >> ${target}
  cat end.htm_ >> ${target}
done

based on everything that came before, this tiny script will go through every markdown file it finds, convert it to html, and attach a common header and footer. and the date it was modified. automatically. no copy-pasting. no remembering to change the edit date, or even what day it is. just edit some markdown files and and don't think about it.

if you need to rework your entire site, you can just edit the start.htm_ and end.htm_ files and run the script again. other systems call this templating, and devlop elaborate crazy ways of making this dumb basic thing very complex and opaque.

we'll add a few more features. and you may think of some you might need. and likely they're simple enough you can implement them yourself.

because you definitely don't need most of the features that are advertised at you by other systems.

TODO


231011 — tehn@nnnnnnnn.co