Post creator script

blog, ruby

Jekyll blog posts are simply text files written in Markdown. But, they need to be named a certain way and they need to include specific metadata, known as “front matter,” at the beginning of the file. I’ve been rekindling my love affair with Ruby lately and figured I’d write a silly little script to help me create new posts since I can never remember the file naming convention or how to craft the required front matter. I think there might already be a gem out there named Jekyll Compose that does something similar. But, since I’m looking for Ruby projects to work on I figured it would be more fun to write something from scratch.

For some work projects in the past I’ve used Python and Jinja templates for this kind of thing. So I figured the Ruby analog to Jinja – ERB – would work well. Here’s what I came up with.

First the blog_post_creator.rb script:

#!/usr/bin/env ruby

require 'date'
require 'erb'

# Ask the user for the blog post title and tags
print "Blog post title: "
blog_post_title = gets.chomp
print "Tags? (Space separated list; leave blank if none): "
tags =
tags = (gets.chomp).split

# Get today's date and format it so it's suitable for a filename
timestamp ="%Y-%m-%d")

# Turn the blog post title into a suitable filename
blog_post_filename = blog_post_title.gsub(" ", "-").downcase

# Read in the ERB template file
template ='')

# Execute the ERB code and produce the templated output
output =

# Write the templated output into a new post file"../drafts/#{timestamp}-#{blog_post_filename}.md", 'w').write(output)

Next the ERB template, which the above creator script uses:

layout: post
title: <%= blog_post_title %>
date: <%= timestamp %>
  twitter: mps75
tags: <%= tags %>

Here’s what it looks like to run the script:

Mac-mini:scripts mswanson$ ruby blog_post_creator.rb
Blog post title: OMG new post
Tags? (Space separated list; leave blank if none): blog test

Lastly, here’s what the outputted blog post file looks like (in the above example the file name is “”):

layout: post
title: OMG new post
date: 2018-11-21
  twitter: mps75
tags: ["blog", "test"]

Now the hard part – get some content in there….