The .Files

Automate Hugo Locally

Previous posts have covered how to automate the remote part of having one or more blogs using Hugo. The last (I hope) post in this series will do the same for your local machine.

Writing a new post involves going to the blog’s root directory, creating the blog post file with hugo new, then opening it in a text editor and start actually writing it.
That’s one step too many, so I made a shell script instead. Or rather, I made two: one for creating content and the other for pushing it to the repo.

Keep in mind I post semi regularly to 4 different blogs (and a couple local note files) so my current solution is a little more involved than what I’m presenting here, but it’s the same principle.

This is the relevant part of the script I use for creating posts:

articulo() {
    menuArticulo=$(printf "Fuckup\\nMuert.o\\nDotfiles\\nTextArt" | listar -p " ")
    case $menuArticulo in
    "Fuckup") cd "$ARTDIR/fuckup/blog" || exit ;;
    "Dotfiles") cd "$ARTDIR/club/the-dot-files" || exit ;;
    "TextArt") cd "$ARTDIR/town/textarrrgh" || exit ;;
    "Muert.o") cd "$ARTDIR/texto-plano/muert.o" || exit ;;
    esac
    [ -z "$menuArticulo" ] && exit
    nombre="$(listar -theme dprompt -p "Titulo: ")" 2>/dev/null
    [ -z "$nombre" ] && exit
    nombreForm="$(echo "$nombre" | tr ' ' '-')"
    if [ "$PWD" = "$ARTDIR/texto-plano/muert.o" ]; then
        hugo new articulos/"$nombreForm".md
        sleep 1
        abrir "$PWD/content/articulos/$nombreForm.md"
    else
        hugo new posts/"$nombreForm".md
        sleep 1
        abrir "$PWD/content/posts/$nombreForm.md"
    fi
}

$ARTDIR is the immutable part of the path where I keep all my Hugo files.
listar is a small function I use to pass variables to Rofi1, which I use to generate GUI menus. With minor modifications, it could be replaced by FZF or perhaps a pure shell solution using select or something like that. abrir is another function that opens the created file in text editor in a floating terminal emulator window.
As mentioned before, this function is part of a larger script that can be found linked below.

The posting script is considerably simpler:

#!/bin/sh

MAINDIR="$HOME/doc/paginas/tilde"

subir() {
	if [ "$PWD" = "$MAINDIR/texto-plano/muert.o" ] || [ "$PWD" = "$MAINDIR/town/textarrrgh" ]; then
		hugo
	fi
	git add . && git commit -m "nuevo post"
}

listar() { rofi -dmenu "$@"; }

MENU="$(printf "Fuckup\\nMuerto\\nDotfiles\\nTextArt" | listar -p " ")"
case $MENU in
'Fuckup') cd "$MAINDIR/fuckup/blog" && subir || exit ;;
'Muerto') cd "$MAINDIR/texto-plano/muert.o" && subir || exit ;;
'Dotfiles') cd "$MAINDIR/club/the-dot-files" && subir || exit ;;
'TextArt') cd "$MAINDIR/town/textarrrgh" && subir || exit ;;
esac

It’s not the prettiest, but it gets the job done. At some point I imagine I will look up how to write better, less repetitive scripts. Haven’t found a way so far.
The way it works is pretty straightforward, cd into the directory and call a helper function that mostly does git stuff.
The git push command is conspicuously missing because I have set up a post-commit hook on my blog repos that automatically pushes after writing a commit message.


  1. https://github.com/davatorium/rofi ↩︎