14 Aug 2011

Automating New Post Creation in Jekyll

After automating deployment, my next goal was to automate creating a new post. After looking around a bit I decided to slightly modify the rake task used by Octopress, a Jekyll based blogging framework.

  # adapted from https://github.com/imathis/octopress/blob/master/Rakefile   
  # usage rake new_post['My New Post'] or rake new_post (defaults to "My New Post")
  desc "Start a new post"
  task :new_post, :title do |t, args|
   args.with_defaults(:title => 'My New Post')
   title = args.title
   filename = "_posts/#{Time.now.strftime('%Y-%m-%d')}-#{title.downcase.gsub(/&/,'and').gsub(/[,'":\?!\(\)\[\]]/,'').gsub(/[\W\.]/, '-').gsub(/-+$/,'')}.html"
   puts "Creating new post: #{filename}"
   open(filename, 'w') do |post|
     system "mkdir -p _posts";
     post.puts "---"
     post.puts "layout: post"
     post.puts "title: \"#{title.gsub(/&/,'&')}\""
     post.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
     post.puts "published: false"
     post.puts "---"

Now that the above is in my rakefile, I'm just a rake new_post['This is Awesome'] away from starting a new post.