-
Notifications
You must be signed in to change notification settings - Fork 3
/
Rakefile
113 lines (97 loc) · 2.71 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#
## Customize post location and post extensions
#
baseurl = `cat ./_config.yml | awk '/baseurl/ { print $2 }' | sed 's/\"//g'`
SOURCE = "."
CONFIG = {
'posts' => File.join(SOURCE, "_posts"),
'post_ext' => "md",
'theme_package_version' => "0.1.0"
}
#
## Color console outout support just because :D
#
module Colors
def colorize(text, color_code)
"\033[#{color_code}m#{text}\033[0m"
end
{
:black => 30,
:red => 31,
:green => 32,
:yellow => 33,
:blue => 34,
:cyan => 36,
}.each do |key, color_code|
define_method key do |text|
colorize(text, color_code)
end
end
end
include Colors
#
## Just typing `rake` will invoke `rake preview`
#
task :default => :preview
load '_rake-configuration.rb' if File.exist?('_rake-configuration.rb')
desc 'Preview with livereload on local machine'
task :preview => :clean do
puts green "Starting livereload server"
jekyll('serve -L')
end
task :serve => :preview
desc 'Clean up generated site'
task :clean do
cleanup
end
desc 'Check links for generated site'
task :check do
STDOUT.sync = true
cleanup
jekyll("build -d _site#{baseurl}")
puts cyan "Running html proofer..."
puts `htmlproofer --assume-extension --alt-ignore '/.*/' ./_site`
end
# Usage: rake post title="A Title" [date="2012-02-09"] [tags=[tag1,tag2]] [category="category"]
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"] || "[]"
category = ENV["category"] || ""
category = "\"#{category.gsub(/-/,' ')}\"" if !category.empty?
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
begin
date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime('%Y-%m-%d')
rescue => e
puts red "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.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts cyan "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/-/,' ')}\""
post.puts 'description: ""'
post.puts "category: #{category}"
post.puts "tags: #{tags}"
post.puts "---"
post.puts "{% include JB/setup %}"
end
end # task :post
#
## General support functions
#
def cleanup
sh 'rm -rf _site'
end
def jekyll(directives = '')
sh 'jekyll ' + directives
end
def rake_running
`ps | grep 'rake' | grep -v 'grep' | wc -l`.to_i > 1
end