/
blog.rake
76 lines (61 loc) · 2.65 KB
/
blog.rake
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
Loquacious.configuration_for(:webby) {
desc "The default directory where new blog posts will be created."
blog_dir 'blog'
}
namespace :blog do
# iterate over all the files in the "templates/blog" folder and create a
# rake task corresponding to each file found
FileList["#{Webby.site.template_dir}/blog/*"].each do |template|
next unless test(?f, template)
name = template.pathmap('%n')
next if name =~ %r/^(month|year)$/ # skip month/year blog entries
desc "Create a new blog #{name}"
task name => [:create_year_index, :create_month_index] do |t|
page, title, dir = Webby::Builder.new_page_info
# if no directory was given use the default blog directory (underneath
# the content directory)
dir = Webby.site.blog_dir if dir.empty?
dir = File.join(dir, Time.now.strftime('%Y/%m/%d'))
page = File.join(dir, File.basename(page))
page = Webby::Builder.create(page, :from => template,
:locals => {:title => title, :directory => dir})
Webby.exec_editor(page)
end
end # each
# this task is used to create the year index file (blog/2008/index.txt)
task :create_year_index do |t|
# parse out information about the page to create
_, _, dir = Webby::Builder.new_page_info
year = Time.now.strftime '%Y'
# if no directory was given use the default blog directory (underneath
# the content directory)
dir = Webby.site.blog_dir if dir.empty?
dir = File.join(dir, year)
# determine the filename and template name
fn = File.join(dir, 'index.txt')
tmpl = Dir.glob(File.join(Webby.site.template_dir, 'blog/year.*')).first.to_s
if test(?f, tmpl) and not test(?f, File.join(Webby.site.content_dir, fn))
Webby::Builder.create(fn, :from => tmpl,
:locals => {:title => year, :directory => dir})
end
end
# this task is used to create the month index file (blog/2008/04/index.txt)
task :create_month_index do |t|
# parse out information about the page to create
_, _, dir = Webby::Builder.new_page_info
now = Time.now
month = now.strftime '%m'
# if no directory was given use the default blog directory (underneath
# the content directory)
dir = Webby.site.blog_dir if dir.empty?
dir = File.join(dir, now.strftime('%Y/%m'))
# determine the filename and template name
fn = File.join(dir, 'index.txt')
tmpl = Dir.glob(File.join(Webby.site.template_dir, 'blog/month.*')).first.to_s
if test(?f, tmpl) and not test(?f, File.join(Webby.site.content_dir, fn))
Webby::Builder.create(fn, :from => tmpl,
:locals => {:title => month, :directory => dir})
end
end
end # namespace :blog
# EOF