Last active
August 29, 2015 14:16
-
-
Save defli/a33b93ca179b889c5b36 to your computer and use it in GitHub Desktop.
Rake new_post for Jekyll based on octopress new post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "stringex" # gem install stringex | |
posts_dir = "_posts" # directory for blog files | |
new_post_ext = "md" | |
#extented by can: open editor after creating post, ex. Sublime Text, ia writer | |
editor = "Ia Writer" | |
def get_stdin(message) | |
print message | |
STDIN.gets.chomp | |
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 | |
# 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 #{posts_dir}" | |
task :new_post, :title do |t, args| | |
if args.title | |
title = args.title | |
else | |
title = get_stdin("Enter a title for your post: ") | |
end | |
mkdir_p "##{posts_dir}" | |
filename = "#{posts_dir}/#{Time.now.strftime('%Y-%m-%d')}-#{title.to_url}.#{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.gsub(/&/,'&')}\"" | |
post.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}" | |
post.puts "comments: true" | |
post.puts "categories: " | |
post.puts "---" | |
end | |
exec "open -a '#{editor}' #{filename}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment