Skip to content

Instantly share code, notes, and snippets.

@jakerobinson
Forked from stammy/Rakefile
Last active December 18, 2015 05:38
Show Gist options
  • Save jakerobinson/5733740 to your computer and use it in GitHub Desktop.
Save jakerobinson/5733740 to your computer and use it in GitHub Desktop.
# Jekyll Rake tasks
desc 'create new post or page. args: type (post, page), title, future (# of days)'
# rake new type=(page|post) future=0 title="New post title goes here" slug="slug-override-title"
task :new do
require 'rubygems'
require 'chronic'
type = ENV["type"] || "post"
title = ENV["title"] || "New Title"
future = ENV["future"] || 0
slug = ENV["slug"].gsub(' ','-').downcase || title.gsub(' ','-').downcase
if type == "page"
TARGET_DIR = "_pages"
elsif future.to_i < 3
TARGET_DIR = "_posts"
else
TARGET_DIR = "_drafts"
end
if future.to_i.zero?
filename = "#{Time.new.strftime('%Y-%m-%d')}-#{slug}.markdown"
else
stamp = Chronic.parse("in #{future} days").strftime('%Y-%m-%d')
filename = "#{stamp}-#{slug}.markdown"
end
path = File.join(TARGET_DIR, filename)
post = <<-HTML
---
layout: TYPE
title: "TITLE"
date: DATE
---
HTML
post.gsub!('TITLE', title).gsub!('DATE', Time.new.to_s).gsub!('TYPE', type)
File.open(path, 'w') do |file|
file.puts post
end
puts "new #{type} generated in #{path}"
system "open -a sublime\\ text\\ 2 #{path}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment