Updates on my blog system

Philip Thomas K.

27 December 19

Back

So there are a few updates to my blog system. As the number of blog posts have increased, I have realised that my current system is highly inefficient. Every time I make any changes to style.css I have to make sure the same changes are in all the subdirectories. Then I need to run the make command in each subdirectory. A lot of work. So I removed the style.css and makefile files from each subdirectory, put the main style.css in the parent directory and wrote a shellscript to do the labour called sync. And here are the contents:

#!/bin/zsh

rm index.md
echo "---" >> index.md
echo "title: Philip's Blog" >> index.md
echo "---" >> index.md
echo >> index.md
echo "## Home" >> index.md

for i in */; do
    cd $i
    echo >> ../index.md
    echo "[$(grep -m 1 'title:' index.md | sed -n 's/title: //p')](${i%?}/)" >> ../index.md
    pandoc -sc ../style.css index.md -o 
    cd ..
done

pandoc -sc style.css index.md -o 

The meat of the code is in the for loop. It loops over all of the subdirectories and greps out the title of the blog post. Then we pipe that into sed to format into the link formatting that markdown uses. That is put into the index.md file in the parent directory which is the markdown file that produces the homepage. Not only does the for loop achieve that, it also uses pandoc to produce the required .html file. So now there is no need for the makefile. This “Code” is not exactly the most efficient piece I have written. But it achieves the following:

  1. No need for multiple style.css files. I now just need one in the parent directory.
  2. Complete removal of makefile. All the instructions required are run by the shellscript.
  3. Completely takes care of the homepage without any input from me even if I create a new post.

And all I have to run in the terminal is ./sync.

Best regards,
Philip
Back