-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRakefile
35 lines (35 loc) · 1022 Bytes
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
require 'time'
# Usage: rake post title="A Title" [date="2014-04-14"]
desc "Create a new post"
task :post do
unless FileTest.directory?('./_posts')
abort("rake aborted: '_posts' directory not found.")
end
title = ENV["title"] || "new-post"
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
begin
date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now)
.strftime('%Y-%m-%d')
rescue Exception => e
puts "Error: date format must be YYYY-MM-DD!"
exit -1
end
filename = File.join('.', '_posts', "#{date}-#{slug}.md")
if File.exist?(filename)
abort("rake aborted: #{filename} already exists.")
end
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "published: ture"
post.puts "layout: post"
post.puts "date: #{date}"
post.puts "title: #{title.gsub(/-/,' ')}"
post.puts "author: Yu"
post.puts "category:"
post.puts "tags:"
post.puts "- "
post.puts "- "
post.puts "---"
end
end