Skip to content

Commit

Permalink
reorganize rakefile
Browse files Browse the repository at this point in the history
Namely standardized some logic and put config variables in hash
  • Loading branch information
plusjade committed Jan 18, 2012
1 parent 246e91a commit 9d7d4eb
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,61 +1,48 @@
require "rubygems"
require 'rake'

## -- Misc Configs --##
jekyll_dir = "."
posts_dir = "_posts"
new_post_ext = "md"
SOURCE = "."
CONFIG = {
'themes' => File.join(SOURCE, "_includes", "themes"),
'layouts' => File.join(SOURCE, "_layouts"),
'posts' => File.join(SOURCE, "_posts"),
'post_ext' => "md"
}

# usage rake new_post[my-new-post] or rake new_post['my new post'] or rake new_post (defaults to "new-post")
desc "Begin a new post in #{jekyll_dir}/#{posts_dir}"
desc "Begin a new post in #{CONFIG['posts']}"
task :new_post, :title do |t, args|
raise "### Cannot locate the #{posts_dir}." unless File.directory?(posts_dir)
mkdir_p "#{jekyll_dir}/#{posts_dir}"
abort("rake aborted: '#{CONFIG['posts']}' directory not found.") unless FileTest.directory?(CONFIG['posts'])
args.with_defaults(:title => 'new-post')
slug = args.title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
title = args.title.gsub(/-/,' ')
filename = "#{jekyll_dir}/#{posts_dir}/#{Time.now.strftime('%Y-%m-%d')}-#{slug}.#{new_post_ext}"
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}\""
post.puts "title: \"#{args.title.gsub(/-/,' ')}\""
post.puts "comments: true"
post.puts "category: "
post.puts "tags: []"
post.puts "---"
end
end

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
end # task :new_post

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

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

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

open("#{layouts_path}/#{File.basename(filename)}", 'w') do |page|
open(File.join(CONFIG['layouts'], File.basename(filename)), 'w') do |page|
if File.basename(filename, ".html").downcase == "default"
page.puts "---"
page.puts "---"
Expand All @@ -73,4 +60,18 @@ end # task :switch_theme
desc "Launch preview environment"
task :preview do
system "jekyll --auto --server"
end # task :preview

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

0 comments on commit 9d7d4eb

Please sign in to comment.