bajsicki.com/content/blog/hugo-setup-improvements.md
2024-12-08 04:05:28 +01:00

3.1 KiB

+++ title = "Some improvements for my ox-hugo set-up" publishDate = 2024-11-18T19:37:00+01:00 lastmod = 2024-12-08T04:05:27+01:00 tags = ["hugo", "web", "orgmode"] categories = ["tech", "emacs"] draft = false meta = true type = "list" [menu] [menu.posts] weight = 3013 identifier = "some-improvements-for-my-ox-hugo-set-up" +++

Probably not the most inventive thing I've done, but I slightly improved my set-up quite a bit.

It's all in this little snippet of elisp:

(delete-directory "~/git/phil/bajsicki.com/content" :recursive)
(org-hugo-export-wim-to-md :all-subtrees)
(cd "~/git/phil/bajsicki.com")
(pwd)
(magit-call-git "commit" "-a" "-m" "Update")
(magit-call-git "push")

One thing that bothered me somewhat about the way ox-hugo handles straggler files is that if I ever removed, renamed or otherwise messed with the structure that already existed, it wouldn't handle the redundant files for me.

E.g. if I deleted a post in my org-mode file (from which this website is generated), ox-hugo would happily leave it hanging in my `content/` directory, damning me to eternal suffering.

However, using this simple elisp, I can easily delete the content directory, and rebuild it from scratch to ensure that my website is always up to date.

I have yet to put it into a function, or do anything. C-c C-c works just fine for me, as it's sitting at the top of the file in a `:noexport:` block.

Another issue I solved was that the git post-receive hook wouldn't run properly. It took me a short bit but eventually I found that I need to run it in a subshell, and unset the `GIT_` env variables to ensure that git doesn't get confused about where to pull the updates from.

#!/bin/bash
# Directory on the server where the website will be mapped.
export GIT_WORK_TREE=/srv/bajsicki.com/
cd $GIT_WORK_TREE
echo "post-receive: Generating https://bajsicki.com with Hugo in $(pwd)"

# Remove any files already in the public directory, a fresh copy will be generated by hugo
echo "post-receive: Cleaning the /public and /content directory to ensure we're in line."
rm -rf public/ && echo "removing /public in $(pwd)"
rm -rf content/ && echo "removing /content in $(pwd)"
# Update the modules in case of changes and pull the new pages
echo "post-receive: pulling git repo and submodules in $(pwd) w/ subshell"
(
	unset $(env | sed -ne 's/^\(GIT_.*\)=.*/\1/p')
	cd /srv/bajsicki.com
	git pull origin main --recurse-submodules --force
)

# Generate the site with hugo
echo "post-receive: running hugo in $(pwd)"
hugo

# Fix any permission problems.
echo "post-receive: fixing permissions in $(pwd)"
find public -type f -print | xargs -d '\n' chmod 644
find public -type d -print | xargs -d '\n' chmod 755

echo "post-receive: Hugo site deployment in $(pwd) complete"

This is my current post-receive hook, and it works quite well, I think.

Anyway. Things are working, jankily yet so.

Next: I am praying that I find some time/ brainpower to fix up the CSS on this website. The misaligned sidenotes are quite irritating.

Join the FSF.