Skip to content

Commit 6ee3125

Browse files
committed
adding the new_post task, with bugs fixed which were pointed out by @swanson
1 parent f3f08f9 commit 6ee3125

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Rakefile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,49 @@
11
require "rubygems"
22
require 'rake'
33

4+
## -- Misc Configs --##
5+
jekyll_dir = "."
6+
posts_dir = "_posts"
7+
new_post_ext = "md"
8+
9+
# usage rake new_post[my-new-post] or rake new_post['my new post'] or rake new_post (defaults to "new-post")
10+
desc "Begin a new post in #{jekyll_dir}/#{posts_dir}"
11+
task :new_post, :title do |t, args|
12+
raise "### Cannot locate the #{posts_dir}." unless File.directory?(posts_dir)
13+
mkdir_p "#{jekyll_dir}/#{posts_dir}"
14+
args.with_defaults(:title => 'new-post')
15+
slug = args.title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
16+
title = args.title.gsub(/-/,' ')
17+
filename = "#{jekyll_dir}/#{posts_dir}/#{Time.now.strftime('%Y-%m-%d')}-#{slug}.#{new_post_ext}"
18+
if File.exist?(filename)
19+
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
20+
end
21+
puts "Creating new post: #{filename}"
22+
open(filename, 'w') do |post|
23+
post.puts "---"
24+
post.puts "layout: post"
25+
post.puts "title: \"#{title}\""
26+
post.puts "comments: true"
27+
post.puts "category: "
28+
post.puts "tags: []"
29+
post.puts "---"
30+
end
31+
end
32+
33+
def ask(message, valid_options)
34+
if valid_options
35+
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
36+
else
37+
answer = get_stdin(message)
38+
end
39+
answer
40+
end
41+
42+
def get_stdin(message)
43+
print message
44+
STDIN.gets.chomp
45+
end
46+
447
desc "Switch between Jekyll-bootstrap themes."
548
task :switch_theme, :theme do |t, args|
649
theme_path = File.join(File.dirname(__FILE__), "_includes", "themes", args.theme)

0 commit comments

Comments
 (0)