Skip to content

Instantly share code, notes, and snippets.

@jaymcgavren
Forked from stammy/Rakefile
Last active December 19, 2015 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaymcgavren/5984630 to your computer and use it in GitHub Desktop.
Save jaymcgavren/5984630 to your computer and use it in GitHub Desktop.
desc 'Create new post.'
task :new do
usage = 'rake new title="My title"'
title = ENV["title"]
time = Time.new
slug = title ? title.gsub(' ', '-').downcase : time.strftime('%H%M%S')
TARGET_DIR = "_posts"
filename = "#{time.strftime('%Y-%m-%d')}-#{slug}.markdown"
path = File.join(TARGET_DIR, filename)
post = <<-HTML
---
layout: post
title: #{title || "''"}
tags: []
published: true
---
HTML
File.open(path, File::WRONLY|File::CREAT|File::EXCL) do |file| # Open file only if it doesn't exist
file.puts post
end
puts "New post generated at: #{path}"
system "open #{path}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment