From 82881cffd47db18745d460ae10ec323c834e2a8a Mon Sep 17 00:00:00 2001 From: James Walker Date: Thu, 13 Mar 2014 16:19:30 -0400 Subject: [PATCH] Rake tasks for draft and publish --- Rakefile | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/Rakefile b/Rakefile index 9b89e77..8ad5ca5 100644 --- a/Rakefile +++ b/Rakefile @@ -1,10 +1,16 @@ +require 'fileutils' + +DRAFTS = "_drafts" +POSTS = "_posts" +DATE = Time.now.strftime("%Y-%m-%d") + task :default => :server - + desc 'Build site with Jekyll' task :build do jekyll 'build' end - + desc 'Build and start local server' task :server do jekyll 'serve -w -D' @@ -15,6 +21,49 @@ task :deploy => :build do sh 'rsync -rtzh --delete _site/ walkah.net:/var/www/walkah.net/' 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 = '') sh 'rm -rf _site' sh 'jekyll ' + opts