-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
49 lines (45 loc) · 1.52 KB
/
Rakefile
File metadata and controls
49 lines (45 loc) · 1.52 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require "rubygems"
require 'rake'
require 'yaml'
require 'time'
SOURCE = "."
CONFIG = {
version: "0.3.0",
posts: File.join(SOURCE, "_posts"),
post_ext: "md",
browser: "firefox"
}
desc "Preview site"
task :preview do
sh "start #{CONFIG[:browser]} http://localhost:4000"
sh "jekyll server --watch"
end
desc "Create 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"
tags = ENV["tags"] || "[]"
category = ENV["category"] || ""
category = "\"#{category.gsub(/-/,' ')}\"" if !category.empty?
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
begin
date = ENV['date'] ? Time.parse(ENV["date"]) : Time.now
rescue => e
puts "Error - date format must be YYYY-MM-DD, please check you typed it correctly!"
exit -1
end
filename = File.join(CONFIG[:posts], "#{date.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
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/-/,' ')}\""
post.puts "date: #{date.strftime('%Y-%m-%d %H:%M:%S')}"
post.puts "category: #{category}"
post.puts "tags: #{tags}"
post.puts "---"
end
end