dimples 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 75d2399050f11daf798efddb9f19ddf49ad1747a
4
- data.tar.gz: 4729eb1b780383dfc1803d1a1306d434e2ae7853
3
+ metadata.gz: c480fd2f7363a7a6072668b2380b2e6962456bf3
4
+ data.tar.gz: ab0db580748680bd12d31dc34b947ee7d2161c60
5
5
  SHA512:
6
- metadata.gz: 6e9bdc8ac3a8cdfe45e081530a1306acbcba76f6ec5c642f50b6aa870a4bb0cfc8be9c0daff5d3a6ae6d6f99bdcee7bffe4af8fe5bdc824557d52be2c6b36377
7
- data.tar.gz: b4559ed758eb51bcbe39dc6636ca9536b5f5fd4f46c763d555924a8a88431676fff2b66a3bfd68c52d3608c8cd13071b6a44095105f59d61b65f163cb6abd8c5
6
+ metadata.gz: d56249e0b2cfaa9d0e4cfe6472503cfaa30d6e17e83da57cdeee62568c643c126492e03a714118d6f6e5023dd3c51b9d870d33bb075336f4494073ed01ab34c4
7
+ data.tar.gz: 84b2d79bc6f0108196098b91fbb62ba8d533114ef4902164ebf525ae1ad6cd8fc50a5b260a00ccdc0e1327cb7a457a21ccf255b6b110cb8ddf95e64ffda87fbb
data/lib/dimples/site.rb CHANGED
@@ -17,7 +17,7 @@ module Dimples
17
17
  @output_paths = {}
18
18
  @templates = {}
19
19
  @categories = {}
20
- @archives = {}
20
+ @archives = {year: {}, month: {}, day: {}}
21
21
 
22
22
  @pages = []
23
23
  @posts = []
@@ -39,163 +39,117 @@ module Dimples
39
39
  @output_paths[:posts] = File.join(@output_paths[:site], @config['paths']['posts'])
40
40
  end
41
41
 
42
+ def generate
43
+ prepare_site
44
+ scan_files
45
+
46
+ generate_posts
47
+ generate_pages
48
+ generate_archives
49
+
50
+ generate_categories if @config['generation']['categories']
51
+ generate_posts_feed if @config['generation']['feed']
52
+ generate_category_feeds if @config['generation']['category_feeds']
53
+
54
+ copy_assets
55
+ end
56
+
57
+ def prepare_site
58
+ begin
59
+ FileUtils.remove_dir(@output_paths[:site]) if Dir.exist?(@output_paths[:site])
60
+ Dir.mkdir(@output_paths[:site])
61
+ rescue => e
62
+ raise "Failed to prepare the site directory (#{e})"
63
+ end
64
+ end
65
+
42
66
  def scan_files
67
+ scan_templates
68
+ scan_pages
69
+ scan_posts
70
+ end
71
+
72
+ def scan_templates
43
73
  Dir.glob(File.join(@source_paths[:templates], '**', '*.*')).each do |path|
44
74
  template = Dimples::Template.new(self, path)
45
75
  @templates[template.slug] = template
46
76
  end
77
+ end
47
78
 
79
+ def scan_pages
48
80
  Dir.glob(File.join(@source_paths[:pages], '**', '*.*')).each do |path|
49
81
  @pages << @page_class.new(self, path)
50
82
  end
83
+ end
51
84
 
52
- Dir.glob(File.join(@source_paths[:posts], '*.*')).each do |path|
53
- @posts << @post_class.new(self, path)
54
- end
55
-
56
- @posts.reverse!
57
- @latest_post = @posts.first
58
-
59
- @posts.each do |post|
60
-
61
- year = post.year.to_s
62
- month = post.month.to_s
63
- day = post.day.to_s
64
-
65
- @archives[year] ||= {posts: [], months: {}}
85
+ def scan_posts
66
86
 
67
- @archives[year][:posts] << post
68
- @archives[year][:months][month] ||= {posts: [], days: {}, name: post.date.strftime('%B')}
69
- @archives[year][:months][month][:posts] << post
70
- @archives[year][:months][month][:days][day] ||= []
71
- @archives[year][:months][month][:days][day] << post
87
+ Dir.glob(File.join(@source_paths[:posts], '*.*')).reverse.each do |path|
88
+ post = @post_class.new(self, path)
72
89
 
73
90
  post.categories.each do |category|
74
91
  (@categories[category] ||= []) << post
75
92
  end
76
- end
77
- end
78
93
 
79
- def generate
80
- scan_files
81
-
82
- begin
83
- FileUtils.remove_dir(@output_paths[:site]) if Dir.exist?(@output_paths[:site])
84
- Dir.mkdir(@output_paths[:site])
85
- rescue => e
86
- raise "Failed to prepare the site directory (#{e})"
87
- end
94
+ %w[year month day].each do |date_type|
95
+ if @config['generation']["#{date_type}_archives"]
88
96
 
89
- @posts.each do |post|
90
- begin
91
- post.write(@output_paths[:posts])
92
- rescue => e
93
- raise "Failed to render post #{File.basename(post.path)} (#{e})"
97
+ date_key = post.date.strftime(@config['date_formats'][date_type])
98
+ (@archives[date_type.to_sym][date_key] ||= []) << post
99
+ end
94
100
  end
95
- end
96
101
 
97
- @pages.each do |page|
98
- begin
99
- page.write(@output_paths[:site])
100
- rescue => e
101
- raise "Failed to render page from #{page.path.gsub(@source_paths[:root], '')} (#{e})"
102
- end
102
+ @posts << post
103
103
  end
104
104
 
105
- if @config['generation']['paginated_posts'] && @posts.length > 0
106
- begin
107
- paginate(@posts, false, @config['pagination']['per_page'], [@output_paths[:posts]], @config['layouts']['posts'])
108
- rescue => e
109
- raise "Failed to paginate main posts (#{e})"
110
- end
111
- end
105
+ @latest_post = @posts.first
106
+ end
112
107
 
113
- if @config['generation']['year_archives'] && @archives.length > 0
108
+ def generate_posts
109
+ @posts.each do |post|
114
110
  begin
115
- generate_archives
111
+ post.write(@output_paths[:posts])
116
112
  rescue => e
117
- raise "Failed to generate archives (#{e})"
113
+ raise "Failed to render post #{post.path} (#{e})"
118
114
  end
119
115
  end
120
116
 
121
- if @config['generation']['categories'] && @categories.length > 0
122
- @categories.each_pair do |slug, posts|
123
- if @config['generation']['category_feeds']
124
- begin
125
- generate_feed(File.join(@output_paths[:posts], slug), {posts: posts[0..@config['pagination']['per_page'] - 1], category: slug})
126
- rescue => e
127
- raise "Failed to generate category feed for '#{slug}' (#{e})"
128
- end
129
- end
130
-
131
- begin
132
- paginate(posts, slug.capitalize, @config['pagination']['per_page'], [@output_paths[:posts], slug], @config['layouts']['category'])
133
- rescue => e
134
- raise "Failed to generate category pages for '#{slug}' (#{e})"
135
- end
136
- end
117
+ if @config['generation']['paginated_posts']
118
+ paginate(posts: @posts, paths: [@output_paths[:posts]], layout: @config['layouts']['posts'])
137
119
  end
120
+ end
138
121
 
139
- if @config['generation']['feed'] && @posts.length > 0
122
+ def generate_pages
123
+ @pages.each do |page|
140
124
  begin
141
- generate_feed(@output_paths[:site], {posts: @posts[0..@config['pagination']['per_page'] - 1]})
125
+ page.write(@output_paths[:site])
142
126
  rescue => e
143
- raise "Failed to generate main feed (#{e})"
127
+ raise "Failed to render page #{page.path.gsub(@source_paths[:root], '')} (#{e})"
144
128
  end
145
129
  end
130
+ end
146
131
 
147
- begin
148
- FileUtils.cp_r(File.join(@source_paths[:public], '.'), @output_paths[:site]) if Dir.exists?(@source_paths[:public])
149
- rescue => e
150
- raise "Failed to copy site assets (#{e})"
132
+ def generate_categories
133
+ @categories.each_pair do |slug, posts|
134
+ paginate(posts: posts, title: slug.capitalize, paths: [@output_paths[:posts], slug], layout: @config['layouts']['category'])
151
135
  end
152
136
  end
153
137
 
154
138
  def generate_archives
155
- if @config['layouts'].has_key?('year')
156
- year_template = @config['layouts']['year']
157
- elsif @config['layouts'].has_key?('archives')
158
- year_template = @config['layouts']['archives']
159
- else
160
- year_template = @config['layouts']['posts']
161
- end
139
+ %w[year month day].each do |date_type|
162
140
 
163
- @archives.each do |year, year_archive|
164
-
165
- if @config['generation']['month_archives']
166
- if @config['layouts'].has_key?('month')
167
- month_template = @config['layouts']['month']
168
- elsif @config['layouts'].has_key?('archives')
169
- month_template = @config['layouts']['archives']
170
- else
171
- month_template = @config['layouts']['posts']
172
- end
141
+ if @config['generation']["#{date_type}_archives"]
173
142
 
174
- year_archive[:months].each do |month, month_archive|
143
+ template = @config['layouts'][date_type] || @config['layouts']['archives'] || @config['layouts']['posts']
144
+ archives[date_type.to_sym].each_pair do |date_title, posts|
175
145
 
176
- if @config['generation']['day_archives']
146
+ paths = [@output_paths[:posts], posts[0].year]
147
+ paths << posts[0].month if date_type =~ /month|day/
148
+ paths << posts[0].day if date_type == 'day'
177
149
 
178
- if @config['layouts'].has_key?('day')
179
- day_template = @config['layouts']['day']
180
- elsif @config['layouts'].has_key?('archives')
181
- day_template = @config['layouts']['archives']
182
- else
183
- day_template = @config['layouts']['posts']
184
- end
185
-
186
- month_archive[:days].each do |day, posts|
187
- day_title = posts[0].date.strftime(@config['date_formats']['day'])
188
- paginate(posts, day_title, @config['pagination']['per_page'], [@output_paths[:posts], year.to_s, month.to_s, day.to_s], day_template)
189
- end
190
- end
191
-
192
- month_title = month_archive[:posts][0].date.strftime(@config['date_formats']['month'])
193
- paginate(month_archive[:posts], month_title, @config['pagination']['per_page'], [@output_paths[:posts], year.to_s, month.to_s], month_template)
150
+ paginate(posts: posts, title: date_title, paths: paths, layout: template)
194
151
  end
195
152
  end
196
-
197
- year_title = year_archive[:posts][0].date.strftime(@config['date_formats']['year'])
198
- paginate(year_archive[:posts], year_title, @config['pagination']['per_page'], [@output_paths[:posts], year.to_s], year_template)
199
153
  end
200
154
  end
201
155
 
@@ -206,53 +160,75 @@ module Dimples
206
160
  feed.extension = 'atom'
207
161
  feed.layout = 'feed'
208
162
 
209
- feed.write(path, options)
163
+ begin
164
+ feed.write(path, options)
165
+ rescue => e
166
+ raise "Failed to generate feed #{path} (#{e})"
167
+ end
210
168
  end
211
169
 
212
- def paginate(posts, title, per_page, paths, layout, params = {})
213
- fail "'#{layout}' template not found" unless @templates.has_key?(layout)
170
+ def generate_posts_feed
171
+ generate_feed(@output_paths[:site], {posts: @posts[0..@config['pagination']['per_page'] - 1]})
172
+ end
214
173
 
215
- pages = (posts.length.to_f / per_page.to_i).ceil
174
+ def generate_category_feeds
175
+ @categories.each_pair do |slug, posts|
176
+ generate_feed(File.join(@output_paths[:posts], slug), {posts: posts[0..@config['pagination']['per_page'] - 1], category: slug})
177
+ end
178
+ end
216
179
 
217
- for index in 0...pages
218
- range = posts.slice(index * per_page, per_page)
180
+ def copy_assets
181
+ begin
182
+ FileUtils.cp_r(File.join(@source_paths[:public], '.'), @output_paths[:site]) if Dir.exists?(@source_paths[:public])
183
+ rescue => e
184
+ raise "Failed to copy site assets (#{e})"
185
+ end
186
+ end
219
187
 
220
- page = @page_class.new(self)
188
+ def pagination_url(page, paths)
189
+ path = '/'
221
190
 
222
- page_paths = paths.clone
223
- page_title = title ? title : @templates[layout].title
191
+ path += File.split(paths[0])[-1] + "/" if paths[0] != @output_paths[:site]
192
+ path += paths[1..-1].join('/') + "/" if paths.length > 1
193
+
194
+ path
195
+ end
224
196
 
225
- if page_paths[0] == @output_paths[:site]
226
- url_path = '/'
227
- else
228
- url_path = "/#{File.split(page_paths[0])[-1]}/"
229
- end
197
+ def paginate(posts:, title: nil, paths:, layout: false)
198
+ fail "'#{layout}' template not found" unless @templates.has_key?(layout)
230
199
 
231
- url_path += "#{page_paths[1..-1].join('/')}/" if page_paths.length > 1
200
+ per_page = @config['pagination']['per_page']
201
+ page_count = (posts.length.to_f / per_page.to_i).ceil
232
202
 
233
- if index > 0
234
- page_paths.push("page#{index + 1}")
235
- end
203
+ for index in 1..page_count
204
+ range = posts.slice((index - 1) * per_page, per_page)
205
+
206
+ page = @page_class.new(self)
207
+
208
+ page.layout = layout
209
+ page.title = title || @templates[layout].title
210
+
211
+ page_paths = paths.clone
236
212
 
237
213
  pagination = {
238
- page: index + 1,
239
- pages: pages,
240
- total: posts.length,
241
- path: url_path
214
+ page: index,
215
+ pages: page_count,
216
+ post_count: posts.length,
217
+ path: pagination_url(index, page_paths)
242
218
  }
243
219
 
244
- if (pagination[:page] - 1) > 0
245
- pagination[:previous_page] = pagination[:page] - 1
246
- end
220
+ page_paths << "page#{index}" if index != 1
247
221
 
248
- if (pagination[:page] + 1) <= pagination[:pages]
249
- pagination[:next_page] = pagination[:page] + 1
250
- end
222
+ pagination[:previous_page] = pagination[:page] - 1 if (pagination[:page] - 1) > 0
223
+ pagination[:next_page] = pagination[:page] + 1 if (pagination[:page] + 1) <= pagination[:pages]
251
224
 
252
- page.layout = layout
253
- page.title = page_title
225
+ path = File.join(page_paths)
254
226
 
255
- page.write(File.join(page_paths), {posts: range, pagination: pagination}.merge(params))
227
+ begin
228
+ page.write(path, {posts: range, pagination: pagination})
229
+ rescue => e
230
+ raise "Failed to generate paginated page #{path} (#{e})"
231
+ end
256
232
  end
257
233
  end
258
234
  end
@@ -1,3 +1,3 @@
1
1
  module Dimples
2
- VERSION = "1.1.1"
2
+ VERSION = "1.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dimples
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Bogan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-21 00:00:00.000000000 Z
11
+ date: 2015-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tilt