commit d82339c31f855be9d840236b2a8e82aae653050f
parent 6cd69e0f5847ff94be0dc609f6b0197ca01c0414
Author: pyratebeard <root@pyratebeard.net>
Date: Thu, 20 Oct 2022 23:08:47 +0100
as_a_matter_of_course
Diffstat:
1 file changed, 184 insertions(+), 0 deletions(-)
diff --git a/entry/as_a_matter_of_course.md b/entry/as_a_matter_of_course.md
@@ -6,3 +6,187 @@ 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.
+
+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}
+
+# 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
+```
+
+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).
+```
+#!/bin/sh
+
+# use arg as title and set post file path
+TITLE=$1
+POST="entry/${TITLE}.md"
+
+# checkout the correct branch
+git branch | grep ${TITLE} && \
+ git checkout -q ${TITLE} || \
+ $(git checkout -q main && git checkout -q -b ${TITLE}) || exit 1
+
+# if post file does not exist yet touch it
+[ -f "${POST}" ] || touch "${POST}"
+
+# open post file in favourite editor
+vim "${POST}"
+
+# when vim closes add and commit the changes
+# if there are any
+git status | grep "${POST}" || exit 0
+git add "${POST}"
+git commit -S -m "${TITLE}"
+```
+
+```
+#!/bin/bash
+
+# use arg as title and set post file path
+TITLE=$1
+POST="entry/${TITLE}.md"
+
+# create a tmp dir
+mkdir demo
+
+# copy post file, post_template and css
+# to temp dir
+cp ${POST} demo/index.md
+cp post_template.html style.css demo/
+
+# small change to post_template for stylesheet
+sed -i 's%\.\./%%' demo/post_template.html
+
+# generate html file
+pandoc -s \
+ --template=demo/post_template.html \
+ --metadata title="demolog" \
+ -f markdown \
+ -t html \
+ -o demo/index.html \
+ demo/index.md
+
+# start web server and capture pid for later
+busybox httpd -f -h ./demo -p 8080 &
+busypid=$!
+
+# open demo post file in browser
+xdg-open http://localhost:8080
+
+# wait until ready to stop web server
+echo press any key to cancel
+read -n 1
+
+# kill the web server and remove temp dir
+kill -9 ${busypid}
+rm -rf demo/
+```
+
+```
+#!/bin/sh
+
+# use arg as title and set post file path
+# set published file path with date
+TITLE=$1
+POST="entry/${TITLE}.md"
+PUBLISH="entry/$(date +%Y%m%d)-${TITLE}.md"
+LINK="https://log.pyratebeard.net/entry/$(date +%Y%m%d)-${TITLE}.html"
+
+# toot command path
+TOOT="$HOME/src/warez/toot/bin/toot"
+
+# checkout the correct branch
+git branch | grep ${TITLE} && \
+ git checkout ${TITLE} || \
+ git checkout -b ${TITLE}
+
+# rename post to set date
+git mv "${POST}" "${PUBLISH}"
+
+# commit the rename as published
+git commit -S -m "publish ${TITLE}"
+
+# checkout main branch and merge published post
+git checkout main
+git merge "${TITLE}"
+
+# push new post
+git push
+```