Skip to content

Instantly share code, notes, and snippets.

@alenbasic
Created June 22, 2016 11:54
Show Gist options
  • Save alenbasic/bc51a37c8de26e04937d1fb6ab0d7390 to your computer and use it in GitHub Desktop.
Save alenbasic/bc51a37c8de26e04937d1fb6ab0d7390 to your computer and use it in GitHub Desktop.
I often find myself manually changing the title name and forgetting to append ".md" in hugo so I quickly wrote this up so I wouldn't have to remember anymore :)
#!/usr/bin/python
import subprocess, string, os, re
# Important variables up here to make it easier to change later on
_BLOG_DIR = "/path/to/blog/here"
_BLOG_DIR_CONTENT = _BLOG_DIR + "/content/"
_EDITOR = "mate" # I use textmate, but you can change this to whatever you want
# this check is done since you can only create a new post in hugo
# in the blog directory
if os.getcwd() != _BLOG_DIR:
print "You must be in %s for this to work" % _BLOG_DIR
else:
title = raw_input("title> ")
title = string.capwords(title) # Capitalises Each Word In Your Title
# takes your title and generates a lowercase filename with dashes instead
# of spaces and appends .md for your editor to easily pick up the file as
# being in markdown and also removes special characters
filename = re.sub('[^A-Za-z0-9\-\.]+', '', (title.lower().replace(" ", "-") + ".md"))
subprocess.call(["hugo","new", filename])
file_location = _BLOG_DIR_CONTENT + filename
# opens the file, changes the title to the pretty one we made earlier
# then writes it to file before finally opening it up in hugo
old_file = open(file_location,'r').readlines()
for i in range(len(old_file)):
if "title" in old_file[i]:
old_file[i] = "title = " + "\"" + title + "\"" + "\n"
break
blog_entry = open(file_location,'w')
for line in old_file:
blog_entry.write(line)
blog_entry.close()
subprocess.call([_EDITOR, file_location])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment