Skip to content

Commit 9d7d4eb

Browse files
committed
reorganize rakefile
Namely standardized some logic and put config variables in hash
1 parent 246e91a commit 9d7d4eb

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

Rakefile

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,48 @@
11
require "rubygems"
22
require 'rake'
33

4-
## -- Misc Configs --##
5-
jekyll_dir = "."
6-
posts_dir = "_posts"
7-
new_post_ext = "md"
4+
SOURCE = "."
5+
CONFIG = {
6+
'themes' => File.join(SOURCE, "_includes", "themes"),
7+
'layouts' => File.join(SOURCE, "_layouts"),
8+
'posts' => File.join(SOURCE, "_posts"),
9+
'post_ext' => "md"
10+
}
811

912
# 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}"
13+
desc "Begin a new post in #{CONFIG['posts']}"
1114
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}"
15+
abort("rake aborted: '#{CONFIG['posts']}' directory not found.") unless FileTest.directory?(CONFIG['posts'])
1416
args.with_defaults(:title => 'new-post')
1517
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+
filename = File.join(CONFIG['posts'], "#{Time.now.strftime('%Y-%m-%d')}-#{slug}.#{CONFIG['post_ext']}")
1819
if File.exist?(filename)
1920
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
2021
end
22+
2123
puts "Creating new post: #{filename}"
2224
open(filename, 'w') do |post|
2325
post.puts "---"
2426
post.puts "layout: post"
25-
post.puts "title: \"#{title}\""
27+
post.puts "title: \"#{args.title.gsub(/-/,' ')}\""
2628
post.puts "comments: true"
2729
post.puts "category: "
2830
post.puts "tags: []"
2931
post.puts "---"
3032
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
33+
end # task :new_post
4634

4735
desc "Switch between Jekyll-bootstrap themes."
4836
task :switch_theme, :theme do |t, args|
49-
theme_path = File.join(File.dirname(__FILE__), "_includes", "themes", args.theme)
50-
layouts_path = File.join(File.dirname(__FILE__), "_layouts")
37+
theme_path = File.join(CONFIG['themes'], args.theme)
5138

52-
abort("rake aborted: './_includes/themes/#{args.theme}' directory not found.") unless FileTest.directory?(theme_path)
53-
abort("rake aborted: './_layouts' directory not found.") unless FileTest.directory?(layouts_path)
39+
abort("rake aborted: '#{CONFIG['themes']}/#{args.theme}' directory not found.") unless FileTest.directory?(theme_path)
40+
abort("rake aborted: '#{CONFIG['layouts']}' directory not found.") unless FileTest.directory?(CONFIG['layouts'])
5441

5542
Dir.glob("#{theme_path}/*") do |filename|
5643
puts "Generating '#{args.theme}' layout: #{File.basename(filename)}"
5744

58-
open("#{layouts_path}/#{File.basename(filename)}", 'w') do |page|
45+
open(File.join(CONFIG['layouts'], File.basename(filename)), 'w') do |page|
5946
if File.basename(filename, ".html").downcase == "default"
6047
page.puts "---"
6148
page.puts "---"
@@ -73,4 +60,18 @@ end # task :switch_theme
7360
desc "Launch preview environment"
7461
task :preview do
7562
system "jekyll --auto --server"
63+
end # task :preview
64+
65+
def ask(message, valid_options)
66+
if valid_options
67+
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
68+
else
69+
answer = get_stdin(message)
70+
end
71+
answer
7672
end
73+
74+
def get_stdin(message)
75+
print message
76+
STDIN.gets.chomp
77+
end

0 commit comments

Comments
 (0)