Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Rakefile #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require "rubygems"
require 'rake'
require 'yaml'
require 'time'

SOURCE = "."
CONFIG = {
'version_file' => File.join(SOURCE, '_version.yml'),
'layouts' => File.join(SOURCE, '_layouts'),
'posts' => File.join(SOURCE, '_posts'),
'post_ext' => 'md',
}

def agree?(message)
valid_options = [ 'y', 'n' ]
answer = get_stdin("#{message} ").downcase[0]

if answer == 'y'
Copy link

@jamietanna jamietanna Sep 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be replaced with

-	if answer == 'y'
-		true
-	else
-		false
-	end
+	answer == 'y'

true
else
false
end
end

def get_stdin(message)
print message

STDIN.gets.chomp
end

desc "Begin a new post in #{CONFIG['posts']}"
task :post do
abort("rake aborted: '#{CONFIG['posts']}' directory not found.") unless FileTest.directory?(CONFIG['posts'])
title = ENV["title"] || "new-post"
tags = ENV["tags"] || "[]"
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')

begin
date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime('%Y-%m-%d')
rescue => e
puts "Error - date format must be YYYY-MM-DD, please check you typed it correctly!"
exit -1
end

filename = File.join(CONFIG['posts'], "#{date}-#{slug}.#{CONFIG['post_ext']}")
if File.exists?(filename)
abort('Rake task aborted.') if agree?("#{filename} already exists. Do you want to override it?") == false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be worth using unless to tidy up the statement:

-		abort('Rake task aborted.') if agree?("#{filename} already exists. Do you want to override it?") == false 
+		abort('Rake task aborted.') unless agree?("#{filename} already exists. Do you want to override it?")

end

puts "Creating a new post: #{filename}..."
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/-/,' ')}\""
post.puts 'description: ""'
post.puts "---"
post.puts ""
post.puts "Please edit this post!"
end
if File.size(filename) > 0
puts "Successfully created post file. Go edit it now!"
end
end