Skip to content

Instantly share code, notes, and snippets.

@Helw150
Created April 25, 2017 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Helw150/0a2bfb6eba288e568f07bb722e7e3177 to your computer and use it in GitHub Desktop.
Save Helw150/0a2bfb6eba288e568f07bb722e7e3177 to your computer and use it in GitHub Desktop.
A simple article creator for gatsby
import os
import re
from datetime import datetime
import errno
def titleToUrl(title):
lowerTitle = title.lower()
URL = lowerTitle.replace(" ", "-")
return "/blog/" + URL + "/"
def configEdit(URL, config):
with open(config, "r+") as f:
lines = f.readlines()
with open(config, "w") as file:
for line in lines:
if line[0] == "]":
line = ' "' + URL + '",\n' + line
file.write(line)
def createDateTime(tz):
return datetime.now().strftime("%Y-%m-%d %H:%M:%S " + tz)
def makePath(path):
if not os.path.exists(os.path.dirname(path)):
try:
os.makedirs(os.path.dirname(path))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
def writeHeader(file, title, date, description, categories, image):
file.write("---\n")
file.write('title: "' + title + '"\n')
file.write('date: ' + date + '\n')
file.write('categories: ' + categories + '\n')
file.write('description: "' + description + '"\n')
file.write('photo: "' + image + '"\n')
file.write("---\n\n")
file.write("# " + title)
def createFile(title, description, categories, image, timezone):
URL = titleToUrl(title)
configEdit(URL, "config.toml")
path = "./pages" + URL + "index.md"
makePath(path)
with open(path, "w") as file:
writeHeader(file, title, createDateTime(timezone), description, categories, image)
def requestInput():
title = raw_input("Article Title:")
description = raw_input("Description:")
categories = raw_input("Tags for this Article:")
while not re.match('(\w+, ?)+', categories):
categories = raw_input("Tags for this Article:")
image = raw_input("Image URL:")
while not (re.match('\w+\.(jpg|gif|png)', image) or "camo.githubusercontent.com" in image):
image = raw_input("Image URL:")
timezone = raw_input("Timezone (GMT Offset):")
while not re.match('(-[0-9][0-9][0-9][0-9])', timezone):
timezone = raw_input("Timezone (GMT Offset):")
return title, description, categories, image, timezone
title, description, categories, image, timezone = requestInput()
createFile(title, description, categories, image, timezone)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment