Rake tasks for draft and publish

This commit is contained in:
James Walker 2014-03-13 16:19:30 -04:00
parent 5cbab2f065
commit 82881cffd4

View File

@ -1,10 +1,16 @@
require 'fileutils'
DRAFTS = "_drafts"
POSTS = "_posts"
DATE = Time.now.strftime("%Y-%m-%d")
task :default => :server task :default => :server
desc 'Build site with Jekyll' desc 'Build site with Jekyll'
task :build do task :build do
jekyll 'build' jekyll 'build'
end end
desc 'Build and start local server' desc 'Build and start local server'
task :server do task :server do
jekyll 'serve -w -D' jekyll 'serve -w -D'
@ -15,6 +21,49 @@ task :deploy => :build do
sh 'rsync -rtzh --delete _site/ walkah.net:/var/www/walkah.net/' sh 'rsync -rtzh --delete _site/ walkah.net:/var/www/walkah.net/'
end end
# rake draft["Title"]
desc "Create a post in _drafts"
task :draft, :title do |t, args|
title = args[:title]
filename = "#{slugify(title)}.md"
create_file(DRAFTS, filename, title)
end
# rake publish
desc "Move a post from _drafts to _posts"
task :publish, :file do |t, args|
filename = args[:file].split("/").last
draft = "#{DRAFTS}/#{filename}"
puts "Publishing #{draft}"
if File.exists?(draft)
FileUtils.mv(draft, "#{POSTS}/#{DATE}-#{filename}")
puts "#{filename} was moved to '#{POSTS}'."
else
puts "No draft found"
end
end
def slugify (title)
# strip characters and whitespace to create valid filenames, also lowercase
return title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
end
# Create a new file with YAML front matter
def create_file(directory, filename, title)
if File.exists?("#{directory}/#{filename}")
puts "The file already exists."
else
File.open("#{DRAFTS}/#{filename}", 'w') do |f|
f.puts "---"
f.puts "layout: post"
f.puts "title: \"#{title}\""
f.puts "tags: "
f.puts "---"
end
puts "#{filename} was created in '#{directory}'."
end
end
def jekyll(opts = '') def jekyll(opts = '')
sh 'rm -rf _site' sh 'rm -rf _site'
sh 'jekyll ' + opts sh 'jekyll ' + opts