commit eccd36177e862703877efab0a7f7358da6ad60f7
parent d82339c31f855be9d840236b2a8e82aae653050f
Author: pyratebeard <root@pyratebeard.net>
Date: Wed, 16 Nov 2022 23:37:42 +0000
as_a_matter_of_course
Diffstat:
1 file changed, 58 insertions(+), 82 deletions(-)
diff --git a/entry/as_a_matter_of_course.md b/entry/as_a_matter_of_course.md
@@ -1,93 +1,22 @@
This web log as been through a number of iterations. First I used Jekyll on Github Pages, then I switched to Hugo using a docker container and Gitlab's CI/CD pipeline, and finally the current setup.
-I moved away from the docker container as the build started failing and I hit too many issues trying to get it working. In an effort to simplify everything I thought why couldn't I write my posts in markdown then use [pandoc](https://pandoc.org/){target="_blank" rel="noreferrer"} to convert them to HTML.
+I moved away from the docker container as the build started failing and I hit too many issues trying to get it working. In an effort to simplify everything I thought why couldn't I write my posts in markdown then use [pandoc](https://pandoc.org/){target="_blank" rel="noreferrer"} to convert them to HTML, it is a oneliner
```
pandoc -f markdown -t html -o new_post.html new_post.md
```
-Sounds so easy, and it is in essence. That command is the basis for how my blog works.
+Sounds so easy, and it is in essence.
-The log centres around one script, [pyratelog.sh](https://git.pyratebeard.net/pyratelog/file/pyratelog.sh.html){target="_blank" rel="noreferrer"}.
-```
-#!/bin/bash
-
-WEBDIR="/var/www/html"
-
-cd ${WEBDIR}
+The main script, [pyratelog.sh](https://git.pyratebeard.net/pyratelog/file/pyratelog.sh.html){target="_blank" rel="noreferrer"} does all the heavy lifting. When a new entry is published the script does some prep then runs `pandoc` to convert the markdown file to html. I use a template file so my markdown only has to be the main body of content. This is how the command looks in my script
-# generate rss.xml file
-if [ -f rss.xml ] ; then
- if [[ $(head -n 1 rss.xml) == "<?xml version='1.0' encoding='UTF-8'?>" ]] ; then
- tail -n+7 rss.xml > rss.xml.bak
- mv rss.xml.bak rss.xml
- fi
-else
- echo -e '</channel>\n</rss>' > rss.xml
-fi
-
-# find all markdown files in 'entry' directory
-find_md=$(find entry/ -type f -name "*.md" | sort)
-
-# for each markdown file
-for md in ${find_md} ; do
-
- # get the title and date from the filename
- input=$(echo ${md} | cut -f2 -d '/' | rev | cut -f2- -d '.' | rev)
-
- # cut the date and turn into epoch time
- input_date=$(echo ${input} | cut -f1 -d '-' )
- if ls -l /bin/date | grep busybox >/dev/null ; then
- # busybox `date` works differently
- index_date=$(date -d ${input_date} -D "%Y%m%d" +%s)
- rss_pubdate="<pubDate>$(date -d ${input_date} -D '%a, %d %b %Y 00:00:00' +%s)</pubDate>"
- else
- index_date=$(date -d ${input_date} +%s)
- rss_pubdate="<pubDate>$(date -d ${input_date} +'%a, %d %b %Y 00:00:00')</pubDate>"
- fi
-
- # cut the title and replace underscores with spaces for menu
- input_title=$(echo ${input} | cut -f2- -d '-' | sed 's/_/\ /g')
- index_title=$(echo ${input_title})
-
- # create menu link for index page
- input_link="<li><a class='index' href='entry/${input}.html'><b>${index_date}</b> ${index_title}</a></li>"
-
- # if link already exists we can skip
- grep ${input} index.html >/dev/null && continue
-
- # for new files we turn the markdown into html
- # using a template file
- pandoc -s \
- --template=./post_template.html \
- --metadata title="${input_title}" \
- -f markdown \
- -t html \
- -o entry/${input}.html \
- entry/${input}.md
-
- # add the link to the top of the menu
- sed "/<ul class='index'>/a ${input_link}" index.html > index.html.bak
-
- # replace old index page
- cat index.html.bak > index.html
-
- # tidy up
- rm -f index.html.bak
-
- # create rss item
- rss_title="<title>${index_title}</title>"
- rss_link="<link>https://log.pyratebeard.net/entry/${input}.html</link>"
- sed "s/</\<\;/g; s/>/\>\;/g" entry/${input}.html > entry/${input}.html.rss
- rss_description=$(sed -n '/body/,/\/body/p' entry/${input}.html.rss)
-
- echo -e "\t<item>\n\t\t${rss_title}\n\t\t${rss_pubdate}\n\t\t${rss_link}\n\t\t<description>${rss_description}</description>\n\t</item>\n$(cat rss.xml)" > rss.xml
-
- rm -f entry/${input}.html.rss
-done
-
-if [[ $(head -n 1 rss.xml) != "<?xml version='1.0' encoding='UTF-8'?>" ]] ; then
- echo -e "<?xml version='1.0' encoding='UTF-8'?>\n<rss version='2.0'>\n<channel>\n\t<title>pyratelog</title>\n\t<link>https://log.pyratebeard.net</link>\n\t<description>another personal blog by another geek</description>\n$(cat rss.xml)" > rss.xml
-fi
+```
+pandoc -s \
+ --template=./post_template.html \
+ --metadata title="${input_title}" \
+ -f markdown \
+ -t html \
+ -o entry/${input}.html \
+ entry/${input}.md
```
Over time I have included three more scripts to improve my writing workflow, [draft](https://git.pyratebeard.net/pyratelog/file/scripts/draft.html){target="_blank" rel="noreferrer"}, [preview](https://git.pyratebeard.net/pyratelog/file/scripts/preview.html){target="_blank" rel="noreferrer"}, and [publish](https://git.pyratebeard.net/pyratelog/file/scripts/publish.html).
@@ -190,3 +119,50 @@ git merge "${TITLE}"
# push new post
git push
```
+
+To run these scripts I use a Makefile
+```
+#TITLE := $(shell git branch --show-current)
+# https://stackoverflow.com/a/14061796
+# If the first argument is "draft"
+ifeq (draft,$(firstword $(MAKECMDGOALS)))
+ # use the rest as arguments for "draft"
+ DRAFT_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
+ # ...and turn them into do-nothing targets
+ $(eval $(DRAFT_ARGS):;@:)
+else
+ifeq (preview,$(firstword $(MAKECMDGOALS)))
+ VIEW_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
+ $(eval $(VIEW_ARGS):;@:)
+else
+ifeq (publish,$(firstword $(MAKECMDGOALS)))
+ PUB_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
+ $(eval $(PUB_ARGS):;@:)
+endif
+endif
+endif
+
+ifeq ($(DRAFT_ARGS), )
+ DRAFT_ARGS := $(shell git branch --show-current)
+endif
+ifeq ($(VIEW_ARGS), )
+ VIEW_ARGS := $(shell git branch --show-current)
+endif
+ifeq ($(PUB_ARGS), )
+ PUB_ARGS := $(shell git branch --show-current)
+endif
+
+prog: # ...
+ # ...
+
+.PHONY: draft preview publish
+
+draft : prog
+ scripts/draft $(DRAFT_ARGS)
+
+preview : prog
+ scripts/preview $(VIEW_ARGS)
+
+publish : prog
+ scripts/publish $(PUB_ARGS)
+```