-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile.rb
103 lines (96 loc) · 3.22 KB
/
Rakefile.rb
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require "rubygems"
require 'rake'
require 'yaml'
require 'time'
SOURCE = "."
CONFIG = {
'layouts' => File.join(SOURCE, "_layouts"),
'post' => File.join(SOURCE, "_posts"),
'page' => File.join(SOURCE, "page"),
'life' => File.join(SOURCE, "life"),
'post_ext' => "md",
}
def ask(message, valid_options)
if valid_options
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
else
answer = get_stdin(message)
end
answer
end
def get_stdin(message)
print message
STDIN.gets.chomp
end
# Usage: rake post title="Post Name"
desc "Begin a new post in #{CONFIG['post']}"
task :post do
abort("rake aborted: '#{CONFIG['post']}' directory not found.") unless FileTest.directory?(CONFIG['post'])
title = ENV["title"] || "New-Post"
slug = title.gsub(' ', '-').gsub(/[^\w-]/, '')
begin
date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime('%Y-%m-%d')
rescue => e
puts "Error - date format must be YYYY-MM-DD!"
exit -1
end
filename = File.join(CONFIG['post'], "#{date}-#{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 "category: "
post.puts "date: #{date}"
post.puts "---"
post.puts "\n"
post.puts "<br/>"
post.puts "本文出自[Peach](/),转载时请注明出处及相应链接。"
end
end
# Usage: rake life title="Post Name"
desc "Begin a new life-post in #{CONFIG['life']}"
task :life do
abort("rake aborted: '#{CONFIG['life']}' directory not found.") unless FileTest.directory?(CONFIG['life'])
title = ENV["title"] || "New-Life"
filename = File.join(CONFIG['life'], "#{title.gsub(/ /,'-').gsub(/[^\w-]/, '')}.#{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 life-post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: life"
post.puts "title: #{title.gsub(/-/,' ')}"
post.puts "---"
end
end
# Usage: rake page title="Page Name"
desc "Begin a new page in #{CONFIG['page']}"
task :page do
abort("rake aborted: '#{CONFIG['page']}' directory not found.") unless FileTest.directory?(CONFIG['page'])
title = ENV["title"] || "New-Page"
filename = File.join(CONFIG['page'], "#{title.gsub(/ /,'-').gsub(/[^\w-]/, '')}.#{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 page: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: blog"
post.puts "title: #{title.gsub(/-/,' ')}"
post.puts "---"
end
end
#Usage: rake deploy
desc "Publishing the website via git"
task :deploy do
puts "Publish to Github..."
system "git push github master"
puts "Publish to GitCafe..."
system "git push gitcafe master:gitcafe-pages"
puts "Your website is now published"
end