Created
January 24, 2011 08:01
-
-
Save stammy/792958 to your computer and use it in GitHub Desktop.
rake task for creating a new jekyll post.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ignore the "bit" stuff.. only relevant to my custom jekyll fork | |
desc 'create new post or bit. args: type (post, bit), title, future (# of days)' | |
# rake new type=(bit|post) future=0 title="New post title goes here" slug="slug-override-title" | |
task :new do | |
require 'rubygems' | |
require 'chronic' | |
type = ENV["type"] || "bit" | |
title = ENV["title"] || "New Title" | |
future = ENV["future"] || 0 | |
slug = ENV["slug"].gsub(' ','-').downcase || title.gsub(' ','-').downcase | |
if type == "bit" | |
TARGET_DIR = "_bits" | |
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 textmate #{path}" | |
end |
This gave me some ideas, thanks.
You can use string interpolation in heredocs as well so the gsub chaining at line #37 is unnecessary. You could do this:
post = <<-"HTML"
---
layout: #{type}
title: "#{title}"
date: #{date}
---
HTML
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
holy shit