-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrakefile.rb
More file actions
109 lines (92 loc) · 3.24 KB
/
Copy pathrakefile.rb
File metadata and controls
109 lines (92 loc) · 3.24 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
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
104
105
106
107
108
109
require 'rake'
require 'yaml'
def ask(question, valid_options)
print question + " "
answer = STDIN.gets.chomp.downcase
return answer if valid_options.include?(answer)
abort("rake aborted!")
end
SOURCE = "."
CONFIG = {
'posts' => File.join(SOURCE, "_posts"),
'drafts' => File.join(SOURCE, "_drafts"),
'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
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts 'title: "' + title.gsub(/-/,' ') + '"'
# post.puts "subtitle: \"\""
post.puts "date: #{Time.now.strftime('%Y-%m-%d')}"
# post.puts "cover: "
post.puts "category: "
post.puts "tags: "
post.puts "---"
end
end # task :post
## 第二个命令
desc "Begin a new post in #{CONFIG['drafts']}"
task :draft do
abort("rake aborted: '#{CONFIG['drafts']}' directory not found.") unless FileTest.directory?(CONFIG['drafts'])
title = ENV["title"] || "new-post"
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
filename = File.join(CONFIG['drafts'], "#{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
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts 'title: "' + title.gsub(/-/,' ') + '"'
# post.puts "subtitle: \"\""
post.puts "date: #{Time.now.strftime('%Y-%m-%d')}"
# post.puts "cover: "
post.puts "category: "
post.puts "tags: "
post.puts "---"
end
end
#-- Tasks for starting the server and creating new posts
desc "Serve the site locally with livereload"
task :serve do
puts "Starting Jekyll server..."
system("bundle exec jekyll serve --livereload --port 4000")
end
task :start => :serve
desc "Create a new post with an interactive prompt"
task :new_post do
print "Enter post title: "
title = STDIN.gets.chomp
if title.strip.empty?
abort("rake aborted: title cannot be empty.")
end
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)
print "#{filename} already exists. Overwrite? [y/n]: "
overwrite = STDIN.gets.chomp.downcase
abort("rake aborted!") unless overwrite == 'y'
end
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts 'title: "' + title + '"'
post.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}"
post.puts "category: "
post.puts "tags: "
post.puts "---"
end
end