dimples 2.7.2 → 3.0.0

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: 6b52d2144b5fa793210da87b05d71b556da99f92
4
- data.tar.gz: 25d265f537063f2cbf077a196b4c5f698f301948
3
+ metadata.gz: a4cee778dfaf4073d4f09fa374b884e684916696
4
+ data.tar.gz: 269d2ba199c54bc6c5cb66094ab38069896de042
5
5
  SHA512:
6
- metadata.gz: a2c53bac19908af864f60bc0c10d5e1dc95ddfd74aa6c224789519aa5101d89eef26ab501e4dddefdfe26a9e04e02574c7138fc3b68a4b2b68f7c1e04351f744
7
- data.tar.gz: d3010e778563ba0d9c4a0f0ab536e5e6c354a0da9bebe40914e1ec64e1c1a50b38d233f787d9dcaab5cffe649266986ca54bacf92cc31e8744677022f2ea870d
6
+ metadata.gz: 7d333b6cf6d9475d643e4e8ae47b5b1777ef598489afaaa5e9eb795e97a773fd84010c182176ba90cffc5c4d0cf7cacf676c9ac63a806d964509e41c25f9531b
7
+ data.tar.gz: 1bd4720fdfc2d8d506110aff782e1307580f4e90f7f9e6c0d8bcc3244a202c06f64d65bc748f7867a1a30814aa189bb8b78510f96700c7abd1a8532979e20483
@@ -8,14 +8,13 @@ require 'logger'
8
8
  require 'tilt'
9
9
  require 'yaml'
10
10
 
11
- require 'dimples/errors'
12
- require 'dimples/logger'
13
-
14
- require 'dimples/frontable'
15
-
16
11
  require 'dimples/category'
17
12
  require 'dimples/configuration'
13
+ require 'dimples/errors'
14
+ require 'dimples/frontable'
15
+ require 'dimples/logger'
18
16
  require 'dimples/page'
17
+ require 'dimples/pagination'
19
18
  require 'dimples/post'
20
19
  require 'dimples/renderer'
21
20
  require 'dimples/site'
@@ -29,7 +29,7 @@ module Dimples
29
29
  'source_path' => Dir.pwd,
30
30
  'destination_path' => File.join(Dir.pwd, 'site'),
31
31
  'verbose_logging' => false,
32
- 'class_overrides' => { site: nil, post: nil, page: nil },
32
+ 'class_overrides' => { site: nil, post: nil },
33
33
  'rendering' => {},
34
34
  'category_names' => {},
35
35
  'paths' => default_paths,
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dimples
4
+ # A module that supports pagination.
5
+ module Pagination
6
+ def paginate(site, items, path, layout, options = {})
7
+ context = options[:context] || {}
8
+ url = path.gsub(site.output_paths[:site], '') + '/'
9
+ per_page = options[:per_page] || site.config['pagination']['per_page']
10
+
11
+ pager = Pager.new(url, items, per_page)
12
+
13
+ pager.each do |index, page_items|
14
+ page = Dimples::Page.new(site)
15
+ page.layout = layout
16
+
17
+ page.title = options[:title] || site.templates[layout].title
18
+ page.extension = options[:extension] if options[:extension]
19
+
20
+ output_path = page.output_path(
21
+ index != 1 ? File.join(path, "page#{index}") : path
22
+ )
23
+
24
+ context[:items] = page_items
25
+ context[:pagination] = pager.to_h
26
+
27
+ page.write(output_path, context)
28
+ end
29
+ end
30
+
31
+ # A class that models the context of a single page during pagination.
32
+ class Pager
33
+ include Enumerable
34
+
35
+ attr_reader :current_page
36
+ attr_reader :previous_page
37
+ attr_reader :next_page
38
+ attr_reader :page_count
39
+ attr_reader :item_count
40
+
41
+ def initialize(url, items, per_page)
42
+ @url = url
43
+ @items = items
44
+ @per_page = per_page
45
+ @page_count = (items.length.to_f / per_page.to_i).ceil
46
+
47
+ step_to(1)
48
+ end
49
+
50
+ def each(&block)
51
+ (1..@page_count).each do |index|
52
+ block.yield step_to(index), items_at(index)
53
+ end
54
+ end
55
+
56
+ def step_to(page)
57
+ @current_page = (1..@page_count).cover?(page) ? page : 1
58
+ @previous_page = (@current_page - 1).positive? ? @current_page - 1 : nil
59
+ @next_page = (@current_page + 1) <= @page_count ? @current_page + 1 : nil
60
+
61
+ @current_page
62
+ end
63
+
64
+ def items_at(page)
65
+ @items.slice((page - 1) * @per_page, @per_page)
66
+ end
67
+
68
+ def previous_page_url
69
+ return unless @previous_page
70
+ @previous_page != 1 ? "#{@url}page#{@previous_page}" : @url
71
+ end
72
+
73
+ def next_page_url
74
+ "#{@url}page#{@next_page}" if @next_page
75
+ end
76
+
77
+ def to_h
78
+ output = {
79
+ page: @current_page,
80
+ page_count: @page_count,
81
+ item_count: @items.count,
82
+ url: @url
83
+ }
84
+
85
+ if @previous_page
86
+ output[:previous_page] = @previous_page
87
+ output[:previous_page_url] = previous_page_url
88
+ end
89
+
90
+ if @next_page
91
+ output[:next_page] = @next_page
92
+ output[:next_page_url] = next_page_url
93
+ end
94
+
95
+ output
96
+ end
97
+ end
98
+ end
99
+ end
@@ -10,7 +10,9 @@ module Dimples
10
10
  callback = proc { @source.contents }
11
11
 
12
12
  @engine = if @source.path
13
- Tilt.new(@source.path, {}, &callback)
13
+ ext = File.extname(@source.path)[1..-1]
14
+ options = @site.config['rendering'][ext] || {}
15
+ Tilt.new(@source.path, options, &callback)
14
16
  else
15
17
  Tilt::StringTemplate.new(&callback)
16
18
  end
@@ -3,6 +3,8 @@
3
3
  module Dimples
4
4
  # A class that models a single site.
5
5
  class Site
6
+ include Pagination
7
+
6
8
  attr_accessor :source_paths
7
9
  attr_accessor :output_paths
8
10
  attr_accessor :config
@@ -12,7 +14,6 @@ module Dimples
12
14
  attr_accessor :pages
13
15
  attr_accessor :posts
14
16
  attr_accessor :latest_post
15
- attr_accessor :page_class
16
17
  attr_accessor :post_class
17
18
  attr_accessor :errors
18
19
 
@@ -28,31 +29,8 @@ module Dimples
28
29
  @archives = { year: {}, month: {}, day: {} }
29
30
  @latest_post = false
30
31
 
31
- @page_class = @config.class_override(:page) || Dimples::Page
32
32
  @post_class = @config.class_override(:post) || Dimples::Post
33
33
 
34
- set_source_paths
35
- set_output_paths
36
- end
37
-
38
- def generate
39
- prepare_output_directory
40
- scan_files
41
- generate_files
42
- copy_assets
43
- rescue Errors::RenderingError,
44
- Errors::PublishingError,
45
- Errors::GenerationError => e
46
- @errors << e.message
47
- end
48
-
49
- def generated?
50
- @errors.count.zero?
51
- end
52
-
53
- private
54
-
55
- def set_source_paths
56
34
  @source_paths = {
57
35
  root: File.expand_path(@config['source_path'])
58
36
  }
@@ -60,9 +38,7 @@ module Dimples
60
38
  %w[pages posts public templates].each do |path|
61
39
  @source_paths[path.to_sym] = File.join(@source_paths[:root], path)
62
40
  end
63
- end
64
41
 
65
- def set_output_paths
66
42
  @output_paths = {
67
43
  site: File.expand_path(@config['destination_path'])
68
44
  }
@@ -73,15 +49,18 @@ module Dimples
73
49
  end
74
50
  end
75
51
 
76
- def prepare_output_directory
77
- if Dir.exist?(@output_paths[:site])
78
- FileUtils.remove_dir(@output_paths[:site])
79
- end
52
+ def generate
53
+ scan_files
54
+ generate_files
55
+ copy_assets
56
+ rescue Errors::RenderingError,
57
+ Errors::PublishingError,
58
+ Errors::GenerationError => e
59
+ @errors << e.message
60
+ end
80
61
 
81
- Dir.mkdir(@output_paths[:site])
82
- rescue => e
83
- error_message = "Couldn't prepare the output directory (#{e.message})"
84
- raise Errors::GenerationError, error_message
62
+ def generated?
63
+ @errors.count.zero?
85
64
  end
86
65
 
87
66
  def scan_files
@@ -113,7 +92,7 @@ module Dimples
113
92
  end
114
93
 
115
94
  def scan_page(path)
116
- @page_class.new(self, path)
95
+ Dimples::Page.new(self, path)
117
96
  end
118
97
 
119
98
  def scan_posts
@@ -141,29 +120,26 @@ module Dimples
141
120
  @categories[slug].posts << post
142
121
  end
143
122
 
144
- add_post_to_archives(post)
123
+ archive_year(post.year) << post
124
+ archive_month(post.year, post.month) << post
125
+ archive_day(post.year, post.month, post.day) << post
145
126
  end
146
127
  end
147
128
 
148
- def add_post_to_archives(post)
149
- archive_year(post.year) << post
150
- archive_month(post.year, post.month) << post
151
- archive_day(post.year, post.month, post.day) << post
152
- end
153
-
154
- def archive_year(year)
155
- @archives[:year][year] ||= []
156
- end
157
-
158
- def archive_month(year, month)
159
- @archives[:month]["#{year}/#{month}"] ||= []
160
- end
129
+ def prepare_output_directory
130
+ if Dir.exist?(@output_paths[:site])
131
+ FileUtils.remove_dir(@output_paths[:site])
132
+ end
161
133
 
162
- def archive_day(year, month, day)
163
- @archives[:day]["#{year}/#{month}/#{day}"] ||= []
134
+ Dir.mkdir(@output_paths[:site])
135
+ rescue => e
136
+ error_message = "Couldn't prepare the output directory (#{e.message})"
137
+ raise Errors::GenerationError, error_message
164
138
  end
165
139
 
166
140
  def generate_files
141
+ prepare_output_directory
142
+
167
143
  generate_pages unless @pages.count.zero?
168
144
 
169
145
  return if @posts.count.zero?
@@ -182,9 +158,7 @@ module Dimples
182
158
  generate_post(post)
183
159
  end
184
160
 
185
- layout = @config['layouts']['posts']
186
-
187
- paginate(posts: @posts, path: @output_paths[:archives], layout: layout)
161
+ paginate(self, @posts, @output_paths[:archives], @config['layouts']['posts'])
188
162
 
189
163
  generate_posts_feeds if @config['generation']['feeds']
190
164
  end
@@ -220,15 +194,14 @@ module Dimples
220
194
  end
221
195
 
222
196
  def generate_category(category)
223
- params = {
224
- posts: category.posts,
225
- title: category.name,
226
- path: File.join(@output_paths[:categories], category.slug),
227
- layout: @config['layouts']['category'],
228
- context: { category: category.slug }
197
+ path = File.join(@output_paths[:categories], category.slug)
198
+
199
+ options = {
200
+ context: { category: category.slug },
201
+ title: category.name
229
202
  }
230
203
 
231
- paginate(params)
204
+ paginate(self, category.posts, path, @config['layouts']['category'], options)
232
205
  end
233
206
 
234
207
  def generate_archives
@@ -240,27 +213,21 @@ module Dimples
240
213
  end
241
214
 
242
215
  def generate_archive_posts(date_type)
243
- @archives[date_type.to_sym].each_value do |posts|
244
- post = posts[0]
245
-
246
- dates = case date_type
247
- when 'year'
248
- { year: post.year }
249
- when 'month'
250
- { year: post.year, month: post.month }
251
- when 'day'
252
- { year: post.year, month: post.month, day: post.day }
253
- end
254
-
255
- params = {
256
- posts: posts,
257
- title: post.date.strftime(@config['date_formats'][date_type]),
258
- path: File.join(@output_paths[:archives], dates.values),
259
- layout: @config['layouts']["#{date_type}_archives"],
260
- context: dates
216
+ @archives[date_type.to_sym].each do |date, posts|
217
+ year, month, day = date.split('/')
218
+
219
+ dates = { year: year }
220
+ dates[:month] = month if month
221
+ dates[:day] = day if day
222
+
223
+ path = File.join(@output_paths[:archives], dates.values)
224
+
225
+ options = {
226
+ context: dates,
227
+ title: posts[0].date.strftime(@config['date_formats'][date_type])
261
228
  }
262
229
 
263
- paginate(params)
230
+ paginate(self, posts, path, @config['layouts']["#{date_type}_archives"], options)
264
231
  end
265
232
  end
266
233
 
@@ -268,7 +235,7 @@ module Dimples
268
235
  feed_templates.each do |format|
269
236
  next unless @templates[format]
270
237
 
271
- feed = @page_class.new(self)
238
+ feed = Dimples::Page.new(self)
272
239
 
273
240
  feed.filename = 'feed'
274
241
  feed.extension = @templates[format].slug
@@ -292,10 +259,6 @@ module Dimples
292
259
  end
293
260
  end
294
261
 
295
- def feed_templates
296
- @feed_templates ||= @templates.keys.select { |k| k =~ /^feeds\./ }
297
- end
298
-
299
262
  def copy_assets
300
263
  if Dir.exist?(@source_paths[:public])
301
264
  Dimples.logger.debug('Copying assets...') if @config['verbose_logging']
@@ -307,51 +270,22 @@ module Dimples
307
270
  raise Errors::GenerationError, "Site assets failed to copy (#{e.message})"
308
271
  end
309
272
 
310
- def paginate(posts:, title: nil, path:, layout: false, context: {})
311
- per_page = @config['pagination']['per_page']
312
- pages = (posts.length.to_f / per_page.to_i).ceil
313
- url = path.gsub(@output_paths[:site], '') + '/'
314
-
315
- (1..pages).each do |index|
316
- page = @page_class.new(self)
317
-
318
- page.layout = layout
319
- page.title = title || @templates[layout].title
320
-
321
- output_path = File.join(path, index != 1 ? "page#{index}" : '')
322
-
323
- context[:posts] = posts.slice((index - 1) * per_page, per_page)
324
- context[:pagination] = build_pagination(index, pages, posts.count, url)
273
+ private
325
274
 
326
- page.write(page.output_path(output_path), context)
327
- end
275
+ def archive_year(year)
276
+ @archives[:year][year] ||= []
328
277
  end
329
278
 
330
- def build_pagination(index, pages, item_count, url)
331
- pagination = {
332
- page: index,
333
- pages: pages,
334
- post_count: item_count,
335
- url: url
336
- }
337
-
338
- if (index - 1).positive?
339
- pagination[:previous_page] = index - 1
340
- pagination[:previous_page_url] = url
341
-
342
- if pagination[:previous_page] != 1
343
- page_string = "page#{pagination[:previous_page]}"
344
- pagination[:previous_page_url] += page_string
345
- end
346
- end
279
+ def archive_month(year, month)
280
+ @archives[:month]["#{year}/#{month}"] ||= []
281
+ end
347
282
 
348
- if (index + 1) <= pages
349
- pagination[:next_page] = index + 1
350
- page_string = "#{url}page#{pagination[:next_page]}"
351
- pagination[:next_page_url] = page_string
352
- end
283
+ def archive_day(year, month, day)
284
+ @archives[:day]["#{year}/#{month}/#{day}"] ||= []
285
+ end
353
286
 
354
- pagination
287
+ def feed_templates
288
+ @feed_templates ||= @templates.keys.select { |k| k =~ /^feeds\./ }
355
289
  end
356
290
  end
357
291
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dimples
4
- VERSION = '2.7.2'
4
+ VERSION = '3.0.0'
5
5
  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: 2.7.2
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Bogan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-09 00:00:00.000000000 Z
11
+ date: 2017-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tilt
@@ -144,6 +144,7 @@ files:
144
144
  - lib/dimples/frontable.rb
145
145
  - lib/dimples/logger.rb
146
146
  - lib/dimples/page.rb
147
+ - lib/dimples/pagination.rb
147
148
  - lib/dimples/post.rb
148
149
  - lib/dimples/renderer.rb
149
150
  - lib/dimples/site.rb