Last active
January 15, 2020 10:21
-
-
Save GiovanniMoretti/8c874652689960c9c11d81952c0ecbd6 to your computer and use it in GitHub Desktop.
If UglyURLs = True, Hugo version 0.15 produced broken links to the category and tag pages. This Python 3 script moves and renames the category and tag index files, so that for these pages, we're pretending the UglyURLs=False.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Currently the Hugo Static Site Generator (v0.15) | |
# http://gohugio.io | |
# doesn't get the category and tag links correct if the | |
# uglyurls option is set in the config file ( "uglyurls=true" | |
# | |
# The generated site has category and tag links that point to a | |
# directory rather than the html file, but the directory doesn't | |
# contain an index.html file so the user ends up with a blank | |
# screen. | |
# | |
# e.g. | |
# /categories/linux (which contains only an .xml file) | |
# instead of | |
# /categories/linux.html | |
# | |
# This program moves and renames the file, like this: | |
# | |
# /categories/linux.html --> /categories/linux/index.html | |
# | |
# and similarly for all categories and tags. | |
# So, after generating the site, just run this program from | |
# the hugo directory. | |
# | |
# It's a hack, but it'll do until the Hugo maintainers (who are | |
# doing a great job :-) get around the fixing the bug. | |
# | |
# Licence - BSD | |
import os, os.path, sys | |
def moveAndRenameIndex(pathname): | |
dir, indexFilename = os.path.split(pathname) | |
indexDir, ext = os.path.splitext(pathname) | |
newName = os.path.join(indexDir, "index.html") | |
try: | |
os.rename(pathname, newName) | |
print("Moved %s\n\t%s" %( pathname, newName) ) | |
except: | |
print("Failed to relocate:", pathname) | |
#====================================================== | |
# Main starts here | |
# If a root directory is specified use it else current directory | |
try: | |
hugoRoot = sys.argv[1] | |
except: | |
hugoRoot = os.getcwd() | |
publicDir = os.path.join(hugoRoot, "public") | |
if not os.path.isdir(publicDir): | |
print("can't find public directory:", publicDir) | |
sys.exit(1) | |
for whichDir in ['categories', 'tags']: | |
path = os.path.join(hugoRoot, "public", whichDir) | |
toMove = [os.path.join(path, f) | |
for f in os.listdir(path) | |
if f.endswith('.html') and | |
f != "index.html" and f != '.html' ] | |
for f in toMove: | |
moveAndRenameIndex(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment