Skip to content

Instantly share code, notes, and snippets.

@starmer
Created July 26, 2013 10:55
Show Gist options
  • Select an option

  • Save starmer/6088014 to your computer and use it in GitHub Desktop.

Select an option

Save starmer/6088014 to your computer and use it in GitHub Desktop.
Rake task to create a new post for a jekyll blog
task :new, :title do |t, args|
if args.to_hash.empty?
puts 'usage: rake new["New Post Title"]'
next
end
title = args.title
slug = title.downcase.gsub(/\s/, '-')
file_name = "_posts/" + Time.new().strftime("%Y-%m-%d") + "-#{slug}.md"
if Pathname.new(file_name).exist?
puts "Error: post already exsists"
next
end
file_content = <<-eos
---
layout: post
title: "#{title}"
date: #{Time.new().strftime("%Y-%m-%d %H:%M:%S")}
slug: #{slug}
---
eos
post = File.open(file_name, "w") do |f|
f.write(file_content)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment