forked from kronik3r/daktilo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
74 lines (63 loc) · 1.8 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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'
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
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
desc "Get the current version of Daktilo"
task :version do
version_file_contents = YAML.load_file(CONFIG['version_file'])
if version_file_contents['version']
puts "Daktilo version #{version_file_contents['version']}."
else
puts "Could not find version file."
end
end