-
-
Save JasonNeel/8374280 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Jekyll Rake tasks | |
desc 'create new post or page. args: type (post, page), title, future (# of days), layout' | |
# rake new type=(page|post) future=0 title="New post title goes here" slug="slug-override-title" layout="post" | |
task :new do | |
require 'rubygems' | |
require 'chronic' | |
type = ENV["type"] || "post" | |
title = ENV["title"] || "New Title" | |
future = ENV["future"] || 0 | |
slug = ENV["slug"] ? ENV["slug"].gsub(' ','-').downcase : title.gsub(' ', '-').downcase | |
layout = ENV["layout"] || "post" | |
extrayaml = "" | |
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: #{layout} | |
title: "TITLE" | |
date: DATE | |
#{extrayaml} | |
--- | |
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} with a layout of #{layout} 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