/
Rakefile
34 lines (31 loc) · 1.16 KB
/
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
require 'rake'
require 'yaml'
SOURCE = "."
CONFIG = {
'posts' => File.join(SOURCE, "_posts/#{Time.now().year()}"),
'post_ext' => "md",
}
# Usage: rake post title="A Title"
desc "Begin a new post in #{CONFIG['posts']}"
task :post do
abort("rake aborted: '#{CONFIG['posts']}' directory not found.") unless FileTest.directory?(CONFIG['posts'])
title = ENV["title"] || "new-post"
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
filename = File.join(CONFIG['posts'], "#{Time.now.strftime('%Y-%m-%d')}-#{slug}.#{CONFIG['post_ext']}")
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
# post.puts "title: #{title.gsub(/-/,' ')}"
# post.puts "title: \"#{title.gsub(/-/,' ')}\""
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "title: \"#{title.gsub(/-/,' ')}\""
post.puts "date: #{Time.now}"
post.puts "author: hoochanlon"
post.puts "category: [#{Time.now().year()}.]"
post.puts "tags: [#{Time.now().year()}]"
post.puts "permalink: /:year/:month-:day/01"
post.puts "---"
end
end # task :post