Created
April 10, 2018 00:22
-
-
Save lydiolectal/05e4f18c5c1abb5161c8f8ab6611a694 to your computer and use it in GitHub Desktop.
Generates jekyll markdown for you based on post title and system time.
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
#!/usr/bin/env python3 | |
# To run: | |
# 1. After downloading, cd to the directory and'chmod -x generate_md.' This tells the shell to make generate_md | |
# into an executable. | |
# 2. 'echo My Awesome Post | ./generate_md' will generate a new markdown file with the post name "My Awesome Post" | |
# and name it "yyyy-mm-dd-my-awesome-post.markdown". | |
import glob, sys, re | |
from time import gmtime, strftime | |
# create file name. | |
def filename(postname): | |
# remove non-alphanumeric characters that are not whitespace. lowercase. | |
p = re.compile('[^\w\s]') | |
postname = p.sub("", postname).lower() | |
# split by whitespace and join with '-' | |
filename = "-".join(postname.split()) | |
# add date and file ending. | |
filename = strftime("%Y-%m-%d-", gmtime()) + filename + ".markdown" | |
return filename | |
# create file content | |
def content(postname): | |
timestamp = strftime("%Y-%m-%d %X %z", gmtime()) | |
content = "---\nlayout: post\ntitle: \"" + postname + "\"\ndate: " \ | |
+ timestamp + "\ncategories:\n---" | |
return content | |
# read the first line of input, strip lead/trail whitespace. | |
postname = sys.stdin.readline().strip() | |
filename = filename(postname) | |
# check that filename exists. If so, error: DO NOT OVERWRITE EXISTING FILE. | |
if glob.glob("_drafts/" + filename): | |
sys.stderr.write("Error: " + filename + " already exists.\n") | |
sys.exit(1) | |
else: | |
content = content(postname) | |
print(content) | |
with open("_drafts/" + filename, 'w') as f: | |
f.write(content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment