Skip to content

Instantly share code, notes, and snippets.

@thbar
Last active December 22, 2015 21:39
Show Gist options
  • Save thbar/6534992 to your computer and use it in GitHub Desktop.
Save thbar/6534992 to your computer and use it in GitHub Desktop.
A rake task to create a new jekyll post (with title, slug, date pre-filled)

Here is the little rake task I use to create new posts for the WiseCash jekyll-based blog.

To use, just run:

rake blog:new["Your Blog Post Title"]
namespace :blog do
task :new, [:title] => :environment do |t, args|
today = Date.today.to_s
title = args[:title]
if title.blank?
abort("\nPlease specify a title, eg:\n\nrake blog:new[\"Freelance Cash Flow: 10 Tips for a Happier Freelancing\"]\n\n")
end
slug = title.parameterize
folder = File.join(File.dirname(__FILE__), '..', '..', 'blog', 'src', '_posts')
filename = File.join(folder, [today, slug].join('-')) + '.markdown'
config = {
'layout' => 'post',
'title' => title,
'date' => today
}
if File.exist?(filename)
abort("File #{filename} already exists! Aborting.")
else
File.open(filename, 'w') do |file|
file << config.to_yaml
file << "---\n"
file << "\n\nPut the summary here\n\n"
file << "<!--more-->"
file << "\n\nPut the next part here\n\n"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment