Rake tasks for draft and publish
This commit is contained in:
parent
5cbab2f065
commit
82881cffd4
49
Rakefile
49
Rakefile
@ -1,3 +1,9 @@
|
||||
require 'fileutils'
|
||||
|
||||
DRAFTS = "_drafts"
|
||||
POSTS = "_posts"
|
||||
DATE = Time.now.strftime("%Y-%m-%d")
|
||||
|
||||
task :default => :server
|
||||
|
||||
desc 'Build site with Jekyll'
|
||||
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user