|
| 1 | +require "rubygems" |
| 2 | +require 'rake' |
| 3 | +require 'yaml' |
| 4 | +require 'time' |
| 5 | + |
| 6 | +SOURCE = "." |
| 7 | +CONFIG = { |
| 8 | + 'version' => "12.3.2", |
| 9 | + 'themes' => File.join(SOURCE, "_includes", "themes"), |
| 10 | + 'layouts' => File.join(SOURCE, "_layouts"), |
| 11 | + 'posts' => File.join(SOURCE, "_posts"), |
| 12 | + 'post_ext' => "md", |
| 13 | + 'theme_package_version' => "0.1.0" |
| 14 | +} |
| 15 | + |
| 16 | +# Usage: rake post title="A Title" subtitle="A sub title" |
| 17 | +desc "Begin a new post in #{CONFIG['posts']}" |
| 18 | +task :post do |
| 19 | + abort("rake aborted: '#{CONFIG['posts']}' directory not found.") unless FileTest.directory?(CONFIG['posts']) |
| 20 | + title = ENV["title"] || "new-post" |
| 21 | + subtitle = ENV["subtitle"] || "This is a subtitle" |
| 22 | + slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '') |
| 23 | + begin |
| 24 | + date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime('%Y-%m-%d') |
| 25 | + rescue Exception => e |
| 26 | + puts "Error - date format must be YYYY-MM-DD, please check you typed it correctly!" |
| 27 | + exit -1 |
| 28 | + end |
| 29 | + filename = File.join(CONFIG['posts'], "#{date}-#{slug}.#{CONFIG['post_ext']}") |
| 30 | + if File.exist?(filename) |
| 31 | + abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n' |
| 32 | + end |
| 33 | + |
| 34 | + puts "Creating new post: #{filename}" |
| 35 | + open(filename, 'w') do |post| |
| 36 | + post.puts "---" |
| 37 | + post.puts "layout: post" |
| 38 | + post.puts "title: \"#{title.gsub(/-/,' ')}\"" |
| 39 | + post.puts "subtitle: \"#{subtitle.gsub(/-/,' ')}\"" |
| 40 | + post.puts "date: #{date}" |
| 41 | + post.puts "author: \"Hux\"" |
| 42 | + post.puts "header-img: \"img/post-bg-2015.jpg\"" |
| 43 | + post.puts "tags: []" |
| 44 | + post.puts "---" |
| 45 | + end |
| 46 | +end # task :post |
| 47 | + |
| 48 | +desc "Launch preview environment" |
| 49 | +task :preview do |
| 50 | + system "jekyll --auto --server" |
| 51 | +end # task :preview |
| 52 | + |
| 53 | +#Load custom rake scripts |
| 54 | +Dir['_rake/*.rake'].each { |r| load r } |
0 commit comments