diff options
author | Pradeep Nayak <pradeep1288@gmail.com> | 2012-01-16 21:17:25 +0530 |
---|---|---|
committer | Pradeep Nayak <pradeep1288@gmail.com> | 2012-01-16 21:17:25 +0530 |
commit | 6ee3125c179e2f610b039b3ace9a3836a998a4ee (patch) | |
tree | 3f9e499024444afa56c05b6eaccc19ef9dada080 /Rakefile | |
parent | f3f08f9b151da2076d8318ca1fdec97530a4d9d8 (diff) |
adding the new_post task, with bugs fixed which were pointed out by @swanson
Diffstat (limited to 'Rakefile')
-rw-r--r-- | Rakefile | 43 |
1 files changed, 43 insertions, 0 deletions
@@ -1,6 +1,49 @@ require "rubygems" require 'rake' +## -- Misc Configs --## +jekyll_dir = "." +posts_dir = "_posts" +new_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}" +task :new_post, :title do |t, args| + raise "### Cannot locate the #{posts_dir}." unless File.directory?(posts_dir) + mkdir_p "#{jekyll_dir}/#{posts_dir}" + 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}" + 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 "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 + desc "Switch between Jekyll-bootstrap themes." task :switch_theme, :theme do |t, args| theme_path = File.join(File.dirname(__FILE__), "_includes", "themes", args.theme) |