|
1 | 1 | require "rubygems" |
2 | 2 | require 'rake' |
3 | 3 |
|
| 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 | + |
4 | 47 | desc "Switch between Jekyll-bootstrap themes." |
5 | 48 | task :switch_theme, :theme do |t, args| |
6 | 49 | theme_path = File.join(File.dirname(__FILE__), "_includes", "themes", args.theme) |
|
0 commit comments