-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rules.rb
200 lines (178 loc) · 5.42 KB
/
Rules.rb
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
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env ruby
# A few helpful tips about the Rules file:
#
# * The string given to #compile and #route are matching patterns for
# identifiers--not for paths. Therefore, you can’t match on extension.
#
# * The order of rules is important: for each item, only the first matching
# rule is applied.
#
# * Item identifiers start and end with a slash (e.g. “/about/” for the file
# “content/about.html”). To select all children, grandchildren, … of an
# item, use the pattern “/about/*/”; “/about/*” will also select the parent,
# because “*” matches zero or more characters.
require 'sass'
require 'compass'
Compass.add_configuration "#{File.dirname(__FILE__)}/.compass/config.rb"
sass_options = Compass.sass_engine_options
#preprocess --------------------------------------------------------------------
preprocess do
puts "preprocess..."
puts ">tweetまとめページの自動生成"
begin
source = File.read "tweets.xml"
listener = TweetListener.new
REXML::Parsers::StreamParser.new(source, listener).parse
listener.items.each {|item|
@items << item
}
rescue
puts "tweets.xml doesn't exist"
end
puts ">Read related_posts.txt"
related_posts = Hash.new()
begin
open("./related_posts.txt", "r") { |file|
file.each_line do |line|
id,rp=line.split("\t")
if(id!=nil&&rp!=nil) then
related_posts[id]=rp
end
end
}
rescue
puts "failed to read related_posts.txt (doesn't exist?)"
end
#articles以下にある各ファイルを処理する
articles = items.select {|item| item.identifier =~ %r|^/articles/.*/|}
articles.each do |item|
item.attributes[:kind] ||= "article"
date = item.identifier.match(/(\d\d\d\d)-(\d\d)-(\d\d)_(\d\d)/).to_a
item.attributes[:created_at] ||= Time.local(date[1],date[2],date[3],date[4]).to_s
item.attributes[:category] ||= "Others"
item.attributes[:tags] ||= [""]
if(related_posts[item.identifier]!=nil) then
item.attributes[:related_posts] ||= eval(related_posts[item.identifier])
end
end
#tagページの自動生成
all_tags = items.map { |p| p.attributes[:tags] }.flatten.compact.uniq
print ">tag and category"
all_tags.each { |tag|
item = Nanoc::Item.new("= render('_meta_page', :tag_meta => '#{tag}')",
{
:title => "Posts in #{get_tag_name(tag)}",
:tag_meta => "#{tag}",
:changefreq => 'weekly',
:priority => "0.0"
},
"/tag/#{get_tag_link(tag.downcase)}",
:binary => false
)
@items << item
print "."
}
print "\n"
#categoryページとそれ以下のtagページの自動生成
all_categories = items.map { |item| item.attributes[:category] }.flatten.compact.uniq
all_categories.each { |category|
item = Nanoc::Item.new("= render('_meta_page', :category_meta => '#{category}')",
{
:title => "Posts in #{category}",
:category_meta => "#{category}",
:changefreq => 'weekly',
:priority => "0.0"
},
"/category/#{category.downcase}",
:binary => false
)
@items << item
#puts " -category page #{category}(#{items_with(category: category).size()}) :#{item.attributes}"
tags = items_with(category: category).map { |p| p.attributes[:tags] }.flatten.compact.uniq
tags.each { |tag|
item = Nanoc::Item.new("= render('_meta_page', :tag_meta => '#{tag}', :category_meta => '#{category}')",
{
:title => "Posts in #{get_tag_name(tag)} of #{category}",
:category_meta => "#{category}",
:tag_meta => "#{tag}",
:changefreq => 'weekly',
:priority => "0.0"
},
"/category/#{category.downcase}/tag/#{get_tag_link(tag.downcase)}",
:binary => false
)
@items << item
print "."
}
print "\n"
}
print "done"
items.delete_if { |item| item[:publish] == false }
puts "preprocess end"
end
#compile -----------------------------------------------------------------------
passthrough '/stylesheets/fonts/*/'
compile '/stylesheets/' do
# don’t filter or layout
end
compile '/sitemap/' do
filter :haml
end
compile '/atom/' do
filter :haml
end
compile '*' do
if item.binary?
# don't filter binary items
else
case item[:extension]
when 'md'
#filter :jp_markdown_filter
filter :redcarpet, :options => {
:fenced_code_blocks => true,
:autolink => true,
:strikethrough => true
},
:renderer => ArticleRenderer
layout 'default'
when 'haml'
filter :haml
layout 'default'
when 'scss'
filter :sass, sass_options.merge(:syntax => item[:extension].to_sym)
else
filter :haml
layout 'default'
end
end
end
#route -------------------------------------------------------------------------
route '/stylesheets/' do
'/style.css'
end
=begin
route '/stylesheets/fonts/*/' do
# /fonts/foo-eot/ -> /fonts/foo.eot
item.identifier.sub(/-#{item[:extension]}\/$/, '.') + item[:extension]
end
=end
route '/sitemap/' do
'/sitemap.xml'
end
route '/atom/' do
'/atom.xml'
end
route '*' do
if item.binary?
# Write item with identifier /foo/ to /foo.ext
item.identifier.chop + '.' + item[:extension]
else
case item[:extension]
when 'scss'
item.identifier.chop + '.css'
else
item.identifier + 'index.html'
end
end
end
layout '*', :haml, :ugly => true