-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRakefile
More file actions
186 lines (162 loc) · 4.97 KB
/
Rakefile
File metadata and controls
186 lines (162 loc) · 4.97 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
require 'rubygems'
require 'rake'
require 'yaml'
require 'time'
require 'fileutils'
require 'jekyll'
SOURCE = "./src"
CONFIG = {
'posts' => File.join(SOURCE, "_posts"),
'images' => File.join(SOURCE, "images"),
'images_templates' => File.join(SOURCE, "_images_templates"),
'post_ext' => "markdown",
}
# Path configuration helper
module JB
class Path
Paths = {
:posts => "_posts"
}
def self.base
SOURCE
end
# build a path relative to configured path settings.
def self.build(path, opts = {})
opts[:root] ||= SOURCE
path = "#{opts[:root]}/#{Paths[path.to_sym]}/#{opts[:node]}".split("/")
path.compact!
File.__send__ :join, path
end
end #Path
end #JB
# Usage: rake post title="A Title" [date="2012-02-09"]
desc "Begin a new post in #{CONFIG['posts']} [lang=(en|nl)][date=YYYY-mm-dd][title=TITLE]"
task :post do
abort("rake aborted: '#{CONFIG['posts']}' directory not found.") unless FileTest.directory?(CONFIG['posts'])
abort("rake aborted: 'ENV['title']' is not set. Consider title='my title'") unless ENV.key?('title')
lang = ENV['lang'] || 'en'
title = ENV['title']
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
begin
now = (ENV['date'] ? Time.parse(ENV['date']) : Time.now)
date = now.strftime('%Y-%m-%d')
rescue Exception => 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.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 "title: \"#{title.gsub(/-/,' ')}\""
post.puts "tags: []"
post.puts "lang: #{lang}"
post.puts "---"
end
imagefile = File.join(CONFIG['images'], now.year.to_s, "%02d" % now.month, "%02d" % now.day, "#{slug}.png");
if File.exist?(imagefile)
abort("rake aborted!") if ask("#{imagefile} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
template_imagefile = next_image!
puts "Copying image: #{template_imagefile} to #{imagefile}"
mkdir_p File.dirname(imagefile)
FileUtils.copy(template_imagefile, imagefile)
end # task :post
desc "Launch preview environment"
task :preview do
limit = ENV["limit"] || 20
host = ENV["HOSTNAME"] || "127.0.0.1"
system "jekyll serve --future --watch --limit-posts #{limit} --source #{SOURCE} --host #{host}"
end # task :preview
desc "List tags used"
task "tags:graph" do
jekyll = Jekyll::Site.new(Jekyll.configuration({}))
jekyll.read_posts('')
tags = {}
jekyll.posts.each do |post|
post.tags.each {|t| tags.has_key?(t) ? tags[t] += 1 : tags[t] = 1 }
end
tags.sort_by{|k,v| v }.each do |tag|
(20 - tag[0].to_s.size).times { print " " }
print "#{tag[0].to_s.slice(0,20)}: "
tag[1].times { print "#"}
print "#{tag[1]}\n"
end
end
desc "Remove a tag name=tagname"
task "tags:remove" do
clean_posts = []
jekyll = Jekyll::Site.new(Jekyll.configuration({}))
jekyll.read_posts('')
jekyll.posts.select {|p| p.tags.include? ENV['name'] }.each do |post|
post.tags.reject! {|t| t == ENV['name']}
clean_posts << post
end
clean_posts.each{|p| puts "#{p.slug} #{p.tags.join(',')}"}
end
desc "Build site"
task "build" do
system "jekyll build --future --source #{SOURCE}"
end
desc "Publish to production"
task publish: :build do
system "cap production deploy"
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
def get_stdin(message)
print message
STDIN.gets.chomp
end
# Gives the name of the next to-be-used image.
def next_image(layout)
number = last_used(layout)+1
imagefile = File.join(CONFIG["images_templates"], "#{layout}-#{number}.png")
imagefile if File.exist?(imagefile)
end
def first_image(layout)
glob = File.join(CONFIG["images_templates"], "#{layout}-*.png")
Dir[glob][0]
end
# Same as next_image, but increments the pointer.
def next_image!(layout="portrait")
if image = next_image(layout)
inc_last_used!(layout)
image
else
set_last_used(0, layout)
first_image(layout)
end
end
# Finds the last used image of a certain layout.
def inc_last_used!(layout)
number = last_used layout
set_last_used(number+1, layout)
end
# Finds the last used imagenumber of a certain layout.
def last_used(layout)
number = "0"
filename = File.join(CONFIG["images_templates"], "last_#{layout}")
if File.exist?(filename)
number = File.open(filename, 'r').readline || number
else
last_used = number
end
number.to_i
end
# sets the last used image.
def set_last_used(number, layout)
filename = File.join(CONFIG["images_templates"], "last_#{layout}")
File.open(filename, 'w') do |pointer|
pointer.puts number
end
end