Created
April 13, 2013 06:26
-
-
Save hara/5377288 to your computer and use it in GitHub Desktop.
Rakefile to write a Jekyll post easily.
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 'yaml' | |
require 'time' | |
require 'fileutils' | |
require 'uuidtools' | |
CONFIG_YML = File.expand_path('../_config.yml', __FILE__) | |
CONFIG = YAML.load(File.read(CONFIG_YML, encoding: 'UTF-8')) | |
SOURCE_DIR = File.expand_path(CONFIG['source'] || File.dirname(__FILE__)) | |
desc 'Write a post' | |
task :write do | |
begin | |
print 'Title: ' | |
title = STDIN.gets.chop | |
end while title == '' | |
slug = title.gsub(/\s+/, '-').gsub(/[^\w-]+/, '').downcase | |
date = nil | |
print 'Date (yyyy-mm-dd, optional): ' | |
input = STDIN.gets.chop | |
unless input.empty? | |
begin | |
date = Time.iso8601(input) | |
rescue ArgumentError | |
puts 'The date format is invalid. Use current date instead.' | |
end | |
end | |
date = Time.now if date.nil? | |
post_dir = File.join(SOURCE_DIR, '_posts') | |
FileUtils.mkdir_p post_dir | |
path = "#{post_dir}/#{date.strftime('%Y-%m-%d')}-#{slug}.markdown" | |
abort "#{path} already exists" if File.exist?(path) | |
File.write(path, <<-EOS, encoding: 'UTF-8') | |
--- | |
layout: post | |
uuid: #{UUIDTools::UUID.random_create.to_s} | |
title: #{title} | |
tags: | |
--- | |
EOS | |
if ENV['EDITOR'] | |
system(ENV['EDITOR'], path) | |
else | |
puts "Edit '#{pat}'" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment