Skip to content

Instantly share code, notes, and snippets.

@IanMulvany
Created September 13, 2017 15:01
Show Gist options
  • Save IanMulvany/fe2a728166e471537058d222a545304c to your computer and use it in GitHub Desktop.
Save IanMulvany/fe2a728166e471537058d222a545304c to your computer and use it in GitHub Desktop.
small script to generate Hugo metadata
import subprocess
import datetime
today = datetime.date.today()
"""
A script to support creating yaml metadata for my blog posts
while using Hugo as a static blog posting engine.
Suggested workflow:
create a draft post in /Users/ianm/Dropbox/blog/drafts with the name of the post as the filename, but no date in the filename.
When I feel the post is readyish then move the filename to
partiallyattended/content/post/ with the date prepended to the blog post
publish the file to medium using ByWord
use this script to generate the yaml header
add the yaml header to the file
run hugo
post to github
Much of this could actually be automated, but for now it's a reasonable start.
"""
def write_to_clipboard(output):
process = subprocess.Popen(
'pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE)
process.communicate(output.encode('utf-8'))
string = "---"
title = input("title:")
tags = input("tags (comma separated):").split(",")
# set title
string = string + "\ntitle: " + title + "\n"
# set date
# date: 2017-09-12T14:47:22+01:00
string = string + "date: " + today.strftime('%Y-%m-%dT%H:%M:%SZ') + "\n"
# set url
# date: 2017/09/12/words_from_title/
string = string + "url: " + today.strftime('%Y/%m/%d/') + title.replace(" ","_") + "/\n"
# set categories
string = string + "categories:"
for tag in tags:
string = string + "\n- " + tag # for loop implicitly adds newline
# close out string:
string = string + "\n---"
write_to_clipboard(string)
f = open("yaml_frontmatter.yaml", "w")
f.write(string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment