Jekyll is great. But creating posts files with names like “2015-09-06-creating-posts-in-jekyll.md” calls out to be scripted.

This is an image

At one point I’d written a simple script to create new posts in jekyll.

For this reboot of the blog, I just googled and grabbed one that someone else had put in this gist.

There are other scripts. That do fancy things like take command line options. But this was fine. If I find I need more, I may rewrite it, or regoogle.

I made a couple of changes. I changed my path and my editor. And I added a timestamp to the frontmatter.

The timestamp in the frontmatter lets Jekyll sort posts chronologically if they were made on the same day.

So I ended up with this:

#!/usr/bin/env ruby

unless ARGV[0]
  puts 'Usage: newpost "the post title"'
  exit(-1)
end

date_prefix = Time.now.strftime("%Y-%m-%d")
postname = ARGV[0].strip.downcase.gsub(/ /, '-')
post = "/Users/jeff/dev/blog/jekyll/jeffroush.com/_posts/#{date_prefix}-#{postname}.md"

header = <<-END
---
layout: post
title: "#{ARGV[0]}"
date: #{Time.now.strftime("%Y-%m-%d %H:%M:%S")}
---
END

File.open(post, 'w') do |f|
  f << header
end

system("vim", post)