bridgetown-paginate 1.0.0.alpha1 → 1.0.0.alpha2

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
  SHA256:
3
- metadata.gz: 1503605e813afc4d2b3fa331033a8a2f6a41066a88cf83432eb065e93c5fbdb0
4
- data.tar.gz: e8078cbed32a2d3b6af6044dae34944d4019935865edda9d222fb03779296c79
3
+ metadata.gz: 7f4ddd4a70ec92776ef3aa0ef66b3962795bed297e7a9dc82823254b9ed0192a
4
+ data.tar.gz: c8f3a2c8df7fc1fcc53cc5dff9a55241bbdba95f9779fc350ab11caaece8fce6
5
5
  SHA512:
6
- metadata.gz: 3e5fd22e0b2f291ea3be6921134fce48d384b39163b08dfc5f8b63ddc8ba0ca531e44db48b7919bce5c2db2c7799240f5dd19eea309e7962cb83d106be0d9282
7
- data.tar.gz: 5fefc5e0d4384942edaabac5b26fa97abd260e47b42abc61c0ec6686f92624dceb802e0833058a4208e4470d3383a8ab5914d6c3e5a9018fa0c8f77c238762ca
6
+ metadata.gz: fe5262a059bb676c9c16f9d81c50cf3b006319da0c6fe28aeba1c6b1faaec84b0b8e158d6e15b4719b20c39e249222a2a54cef12d1c88c342079572d5778ac42
7
+ data.tar.gz: cfda5839ca555de2c8bb99a1639656bbb26a6c0074b99491db9469e4f451b3cdeb9cfb48a98d0fdad035802199bb800799bafe963cbe39e767ed16d59ac8650c
data/.rubocop.yml CHANGED
@@ -2,9 +2,8 @@
2
2
  inherit_from: ../.rubocop.yml
3
3
 
4
4
  AllCops:
5
- Include:
6
- - lib/**/*.rb
7
- - spec/**/*.rb
5
+ Exclude:
6
+ - "*.gemspec"
8
7
 
9
8
  Bridgetown/NoPutsAllowed:
10
9
  Exclude:
@@ -36,7 +36,7 @@ module Bridgetown
36
36
  # If disabled then simply quit
37
37
  unless default_config["enabled"]
38
38
  Bridgetown.logger.info "Pagination:", "disabled. Enable in site config" \
39
- " with pagination:\\n enabled: true"
39
+ " with pagination:\\n enabled: true"
40
40
  return
41
41
  end
42
42
 
@@ -102,11 +102,12 @@ module Bridgetown
102
102
  # Create a proc that will delegate logging
103
103
  # Decoupling Bridgetown specific logging
104
104
  logging_lambda = ->(message, type = "info") do
105
- if type == "debug"
105
+ case type
106
+ when "debug"
106
107
  Bridgetown.logger.debug "Pagination:", message.to_s
107
- elsif type == "error"
108
+ when "error"
108
109
  Bridgetown.logger.error "Pagination:", message.to_s
109
- elsif type == "warn"
110
+ when "warn"
110
111
  Bridgetown.logger.warn "Pagination:", message.to_s
111
112
  else
112
113
  Bridgetown.logger.info "Pagination:", message.to_s
@@ -7,7 +7,7 @@ module Bridgetown
7
7
  # filtering said collections when requested by the defined filters.
8
8
  #
9
9
  class PaginationIndexer
10
- @cached_index = {}
10
+ @cached_index = {}.compare_by_identity
11
11
 
12
12
  class << self
13
13
  attr_accessor :cached_index
@@ -53,10 +53,8 @@ module Bridgetown
53
53
  end
54
54
  end
55
55
 
56
- unless cached_index[all_documents.object_id].is_a?(Hash)
57
- cached_index[all_documents.object_id] = {}
58
- end
59
- cached_index[all_documents.object_id][index_key] = index
56
+ cached_index[all_documents] = {} unless cached_index[all_documents].is_a?(Hash)
57
+ cached_index[all_documents][index_key] = index
60
58
  index
61
59
  end
62
60
 
@@ -32,8 +32,11 @@ module Bridgetown
32
32
  # rubocop:disable Metrics/BlockLength
33
33
  def run(default_config, templates, site_title) # rubocop:todo Metrics/AbcSize
34
34
  if templates.size.to_i <= 0
35
- @logging_lambda.call "is enabled in the config, but no paginated pages found." \
36
- " Add 'pagination:\\n collection: <label>' to the front-matter of a page.", "warn"
35
+ @logging_lambda.call(
36
+ "is enabled in the config, but no paginated pages found." \
37
+ " Add 'pagination:\\n collection: <label>' to the front-matter of a page.",
38
+ "warn"
39
+ )
37
40
  return
38
41
  end
39
42
 
@@ -63,7 +66,7 @@ module Bridgetown
63
66
 
64
67
  next unless template_config["enabled"]
65
68
 
66
- @logging_lambda.call "found page: " + template.path, "debug" unless @debug
69
+ @logging_lambda.call "found page: #{template.path}", "debug" unless @debug
67
70
 
68
71
  # Request all documents in all collections that the user has requested
69
72
  all_posts = get_docs_in_collections(template_config["collection"], template)
@@ -154,34 +157,34 @@ module Bridgetown
154
157
  r = 20
155
158
  f = "Pagination: ".rjust(20)
156
159
  # Debug print the config
157
- if @debug
158
- puts f + "----------------------------"
159
- puts f + "Page: " + page_path.to_s
160
- puts f + " Active configuration"
161
- puts f + " Enabled: ".ljust(r) + config["enabled"].to_s
162
- puts f + " Items per page: ".ljust(r) + config["per_page"].to_s
163
- puts f + " Permalink: ".ljust(r) + config["permalink"].to_s
164
- puts f + " Title: ".ljust(r) + config["title"].to_s
165
- puts f + " Limit: ".ljust(r) + config["limit"].to_s
166
- puts f + " Sort by: ".ljust(r) + config["sort_field"].to_s
167
- puts f + " Sort reverse: ".ljust(r) + config["sort_reverse"].to_s
168
-
169
- puts f + " Active Filters"
170
- puts f + " Collection: ".ljust(r) + config["collection"].to_s
171
- puts f + " Offset: ".ljust(r) + config["offset"].to_s
172
- puts f + " Category: ".ljust(r) + (config["category"].nil? || config["category"] == "posts" ? "[Not set]" : config["category"].to_s)
173
- puts f + " Tag: ".ljust(r) + (config["tag"].nil? ? "[Not set]" : config["tag"].to_s)
174
- puts f + " Locale: ".ljust(r) + (config["locale"].nil? ? "[Not set]" : config["locale"].to_s)
175
- end
160
+ return unless @debug
161
+
162
+ puts "#{f}----------------------------"
163
+ puts "#{f}Page: #{page_path}"
164
+ puts "#{f} Active configuration"
165
+ puts f + " Enabled: ".ljust(r) + config["enabled"].to_s
166
+ puts f + " Items per page: ".ljust(r) + config["per_page"].to_s
167
+ puts f + " Permalink: ".ljust(r) + config["permalink"].to_s
168
+ puts f + " Title: ".ljust(r) + config["title"].to_s
169
+ puts f + " Limit: ".ljust(r) + config["limit"].to_s
170
+ puts f + " Sort by: ".ljust(r) + config["sort_field"].to_s
171
+ puts f + " Sort reverse: ".ljust(r) + config["sort_reverse"].to_s
172
+
173
+ puts "#{f} Active Filters"
174
+ puts f + " Collection: ".ljust(r) + config["collection"].to_s
175
+ puts f + " Offset: ".ljust(r) + config["offset"].to_s
176
+ puts f + " Category: ".ljust(r) + (config["category"].nil? || config["category"] == "posts" ? "[Not set]" : config["category"].to_s)
177
+ puts f + " Tag: ".ljust(r) + (config["tag"].nil? ? "[Not set]" : config["tag"].to_s)
178
+ puts f + " Locale: ".ljust(r) + (config["locale"].nil? ? "[Not set]" : config["locale"].to_s)
176
179
  end
177
180
  # rubocop:enable Layout/LineLength
178
181
 
179
182
  # rubocop:disable Layout/LineLength
180
183
  def _debug_print_filtering_info(filter_name, before_count, after_count)
181
184
  # Debug print the config
182
- if @debug
183
- puts "Pagination: ".rjust(20) + " Filtering by: " + filter_name.to_s.ljust(9) + " " + before_count.to_s.rjust(3) + " => " + after_count.to_s
184
- end
185
+ return unless @debug
186
+
187
+ puts "#{"Pagination: ".rjust(20)} Filtering by: #{filter_name.to_s.ljust(9)} #{before_count.to_s.rjust(3)} => #{after_count}"
185
188
  end
186
189
  # rubocop:enable Layout/LineLength
187
190
 
@@ -263,7 +266,7 @@ module Bridgetown
263
266
  # So to unblock this common issue for the date field I simply iterate
264
267
  # once over every document and initialize the .date field explicitly
265
268
  if @debug
266
- puts "Pagination: ".rjust(20) + "Rolling through the date fields for all documents"
269
+ puts "#{"Pagination: ".rjust(20)}Rolling through the date fields for all documents"
267
270
  end
268
271
  using_posts.each do |u_post|
269
272
  next unless u_post.respond_to?("date")
@@ -378,11 +381,7 @@ module Bridgetown
378
381
  newpage.data["permalink"] = newpage.paginator.page_path if template.data["permalink"]
379
382
 
380
383
  # Transfer the title across to the new page
381
- tmp_title = if !template.data["title"]
382
- site_title
383
- else
384
- template.data["title"]
385
- end
384
+ tmp_title = template.data["title"] || site_title
386
385
 
387
386
  # If the user specified a title suffix to be added then let's add that
388
387
  # to all the pages except the first
@@ -419,40 +418,40 @@ module Bridgetown
419
418
  # simplest is to include all of the links to the pages preceeding the
420
419
  # current one (e.g for page 1 you get the list 2, 3, 4.... and for
421
420
  # page 2 you get the list 3,4,5...)
422
- if config["trail"] && !config["trail"].nil? && newpages.size.to_i.positive?
423
- trail_before = [config["trail"]["before"].to_i, 0].max
424
- trail_after = [config["trail"]["after"].to_i, 0].max
425
- trail_length = trail_before + trail_after + 1
426
-
427
- if trail_before.positive? || trail_after.positive?
428
- newpages.select do |npage|
429
- # Selecting the beginning of the trail
430
- idx_start = [npage.paginator.page - trail_before - 1, 0].max
431
- # Selecting the end of the trail
432
- idx_end = [idx_start + trail_length, newpages.size.to_i].min
433
-
434
- # Always attempt to maintain the max total of <trail_length> pages
435
- # in the trail (it will look better if the trail doesn't shrink)
436
- if idx_end - idx_start < trail_length
437
- # Attempt to pad the beginning if we have enough pages
438
- # Never go beyond the zero index
439
- idx_start = [
440
- idx_start - (trail_length - (idx_end - idx_start)),
441
- 0,
442
- ].max
443
- end
444
-
445
- # Convert the newpages array into a two dimensional array that has
446
- # [index, page_url] as items
447
- npage.paginator.page_trail = newpages[idx_start...idx_end] \
448
- .each_with_index.map do |ipage, idx|
449
- PageTrail.new(
450
- idx_start + idx + 1,
451
- ipage.paginator.page_path,
452
- ipage.data["title"]
453
- )
454
- end
455
- end
421
+ return unless config["trail"] && !config["trail"].nil? && newpages.size.to_i.positive?
422
+
423
+ trail_before = [config["trail"]["before"].to_i, 0].max
424
+ trail_after = [config["trail"]["after"].to_i, 0].max
425
+ trail_length = trail_before + trail_after + 1
426
+
427
+ return unless trail_before.positive? || trail_after.positive?
428
+
429
+ newpages.select do |npage|
430
+ # Selecting the beginning of the trail
431
+ idx_start = [npage.paginator.page - trail_before - 1, 0].max
432
+ # Selecting the end of the trail
433
+ idx_end = [idx_start + trail_length, newpages.size.to_i].min
434
+
435
+ # Always attempt to maintain the max total of <trail_length> pages
436
+ # in the trail (it will look better if the trail doesn't shrink)
437
+ if idx_end - idx_start < trail_length
438
+ # Attempt to pad the beginning if we have enough pages
439
+ # Never go beyond the zero index
440
+ idx_start = [
441
+ idx_start - (trail_length - (idx_end - idx_start)),
442
+ 0,
443
+ ].max
444
+ end
445
+
446
+ # Convert the newpages array into a two dimensional array that has
447
+ # [index, page_url] as items
448
+ npage.paginator.page_trail = newpages[idx_start...idx_end] \
449
+ .each_with_index.map do |ipage, idx|
450
+ PageTrail.new(
451
+ idx_start + idx + 1,
452
+ ipage.paginator.page_path,
453
+ ipage.data["title"]
454
+ )
456
455
  end
457
456
  end
458
457
  end
@@ -11,7 +11,7 @@ module Bridgetown
11
11
  # not read from disk
12
12
  #
13
13
  class PaginationPage < Bridgetown::GeneratedPage
14
- def initialize(page_to_copy, cur_page_nr, total_pages, index_pageandext, template_ext)
14
+ def initialize(page_to_copy, cur_page_nr, total_pages, index_pageandext, template_ext) # rubocop:disable Lint/MissingSuper
15
15
  @site = page_to_copy.site
16
16
  @base = ""
17
17
  @url = ""
@@ -29,7 +29,7 @@ module Bridgetown
29
29
 
30
30
  if @page > @total_pages
31
31
  raise "page number can't be greater than total pages:" \
32
- " #{@page} > #{@total_pages}"
32
+ " #{@page} > #{@total_pages}"
33
33
  end
34
34
 
35
35
  init = (@page - 1) * @per_page
@@ -63,7 +63,7 @@ module Bridgetown
63
63
  @documents = documents[init..offset]
64
64
  @page_path = Utils.format_page_number(this_page_url, cur_page_nr, @total_pages)
65
65
 
66
- @previous_page = @page != 1 ? @page - 1 : nil
66
+ @previous_page = @page == 1 ? nil : @page - 1
67
67
  @previous_page_path = unless @page == 1
68
68
  if @page == 2
69
69
  Utils.format_page_number(
@@ -77,7 +77,7 @@ module Bridgetown
77
77
  )
78
78
  end
79
79
  end
80
- @next_page = @page != @total_pages ? @page + 1 : nil
80
+ @next_page = @page == @total_pages ? nil : @page + 1
81
81
  @next_page_path = if @page != @total_pages
82
82
  Utils.format_page_number(
83
83
  paginated_page_url, @next_page, @total_pages
@@ -74,14 +74,10 @@ module Bridgetown
74
74
  # Sorting routine used for ordering posts by custom fields.
75
75
  # Handles Strings separately as we want a case-insenstive sorting
76
76
  #
77
- # rubocop:disable Naming/MethodParameterName, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
77
+ # rubocop:disable Naming/MethodParameterName, Metrics/CyclomaticComplexity
78
78
  def self.sort_values(a, b)
79
- if a.nil? && !b.nil?
80
- return -1
81
- elsif !a.nil? && b.nil?
82
- return 1
83
- end
84
-
79
+ return -1 if a.nil? && !b.nil?
80
+ return 1 if !a.nil? && b.nil?
85
81
  return a.downcase <=> b.downcase if a.is_a?(String)
86
82
 
87
83
  if a.respond_to?("to_datetime") && b.respond_to?("to_datetime")
@@ -91,7 +87,7 @@ module Bridgetown
91
87
  # By default use the built in sorting for the data type
92
88
  a <=> b
93
89
  end
94
- # rubocop:enable Naming/MethodParameterName, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
90
+ # rubocop:enable Naming/MethodParameterName, Metrics/CyclomaticComplexity
95
91
 
96
92
  # Retrieves the given sort field from the given post
97
93
  # the sort_field variable can be a hierarchical value on the form
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bridgetown-paginate
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha1
4
+ version: 1.0.0.alpha2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bridgetown Team
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 1.0.0.alpha1
19
+ version: 1.0.0.alpha2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 1.0.0.alpha1
26
+ version: 1.0.0.alpha2
27
27
  description:
28
28
  email: maintainers@bridgetownrb.com
29
29
  executables: []