Skip to main content
Refactoring Gohugo Aliases

Refactoring Gohugo Aliases

GoHugo’s documentation recently started to show functions and methods with their “real” name, instead of the alias used in the GoHugo codebase. Did you for instance knew that apply is actually collections.Apply and append is collections.Append? Knowing these exact names seems to be important to me, because I think it’s easier to understand what functions are connected and what features are available. It also makes the search in the documentation work better. Shorter aliases like in were hard to find before, now, knowing it’s collections.In makes it easier to find..

So I decided to refactor my aliases in my GoHugo codebase. I have a lot of them, because I am using GoHugo for quite some time now and I have a lot of modules and partials I am using all over the place with the old syntax. I decided to write a script that does the refactoring for me. It’s a simple bash script that uses some RegExp magic to replace the aliases with the full names. It’s not perfect, but it works for me.

The file aliases.toml is a list of aliases and their full names. The script reads this file and then replaces the aliases in my GoHugo layouts. You might be asking yourself why I choose the TOML format for this file. The answer is, that I can use it shortcodes and layout files too later on.

 1dictionary = 'collections.Dictionary'
 2in = 'collections.In'
 3index = 'collections.Index'
 4isset = 'collections.IsSet'
 5merge = 'collections.Merge'
 6partial = 'partials.Include'
 7partialCached = 'partials.IncludeCached'
 8slice = 'collections.Slice'
 9sort = 'collections.Sort'
10where = 'collections.Where'
 1dictionary: collections.Dictionary
 2in: collections.In
 3index: collections.Index
 4isset: collections.IsSet
 5merge: collections.Merge
 6partial: partials.Include
 7partialCached: partials.IncludeCached
 8slice: collections.Slice
 9sort: collections.Sort
10where: collections.Where
 2   "dictionary": "collections.Dictionary",
 3   "in": "collections.In",
 4   "index": "collections.Index",
 5   "isset": "collections.IsSet",
 6   "merge": "collections.Merge",
 7   "partial": "partials.Include",
 8   "partialCached": "partials.IncludeCached",
 9   "slice": "collections.Slice",
10   "sort": "collections.Sort",
11   "where": "collections.Where"

The file is much longer of course, but you get the idea. The script reads the file and then replaces the aliases in the GoHugo layouts. It has a simple check that the alias is not part of a string and is surrounded either by spaces or brackets.

Here is where the magic happens:

1while IFS='=' read -r search_string replace_string; do
2  search_string=$(echo "$search_string" | tr -d '[:space:]' | tr -d '-')
3  replace_string=$(echo "$replace_string" | tr -d '[:space:]' | tr -d '"' | tr -d '-')
5  find "$directory" -type f -exec sed -i -E "s/(\s|\(|\{)($search_string)(\s|\)|\})/\1$replace_string\3/g" {} +
7  echo "Replaced '$search_string' with '$replace_string'"
8done <"$conversion_file"

The full script can be found in this Gist.

Now, let me add the caveats ;) The script is not perfect. The most obvious current bug I found is, that it replaces aliases that might be part of a simple string. If you for instance have a button “Search in this Blog” then the in will match and be replaced.

I am however sure, that we all are perfectionist GoHugo layout sculptors that moved all string occurences already into the i18n files, right? Right? ;)

Now goeth forth and refactor your GoHugo aliases for a better more understandable layout future!

Back to top
Back Forward