bridgetown-paginate 0.21.3 → 1.0.0.alpha3

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: 04d94f97eb062654a42ae01347d17218c0415ad79ab66d057c9edf2443e4d689
4
- data.tar.gz: 4d558f3739429dc5d4b118d9b0e2cc5017bd4c33a4657a19f79e0fba0dceaeb1
3
+ metadata.gz: e066aadc6ed91f7b51a26c92e8375cc09dc5c18d356c63cc5a373bcba6345764
4
+ data.tar.gz: 6c6528cf2a1c315627f303254331fa9d8b02a76c50c6689d284ff90eb57e8405
5
5
  SHA512:
6
- metadata.gz: 1f43513cabe783023538e01a54c3dbb3cbd4ad80743b7e4ac62ada8e96cd77f9302b94f9d6c1961e55788f943247e91d4db536162db74ca6e61fc87976c703f7
7
- data.tar.gz: 29b7cf579f0af84d1632e7f57ad4194196242c6c76057ff4c12401a4294b8c3343fd60f91cee0212a42cec4c96c3bb2dc3c1daba286694c2092f019455987418
6
+ metadata.gz: 0672dc6e9d4a31cdb52e1d53cc1e5ac3c8cc00db4faf22a1d3a017ac659f456389633bd1a5bd0067a71828b597bbb30ef2a8d9b391e3c0483c278f43b49f584c
7
+ data.tar.gz: 9d4431a78b299b58c7f0a8b9c74f20cda4ab004ff20eef073f8a097090a7b56150198e57187bf0dc464f8b139f6211af5ab988387ad15efdae3745abdc824f5e
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:
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.author = "Bridgetown Team"
9
9
  spec.email = "maintainers@bridgetownrb.com"
10
10
  spec.summary = "A Bridgetown plugin to add pagination support for posts and collection indices."
11
- spec.homepage = "https://github.com/bridgetownrb/bridgetown/tree/master/bridgetown-paginate"
11
+ spec.homepage = "https://github.com/bridgetownrb/bridgetown/tree/main/bridgetown-paginate"
12
12
  spec.license = "MIT"
13
13
 
14
14
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r!^(test|script|spec|features)/!) }
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Handles Generated Pages
4
- Bridgetown::Hooks.register :pages, :post_init, reloadable: false do |page|
4
+ Bridgetown::Hooks.register :generated_pages, :post_init, reloadable: false do |page|
5
5
  if page.class != Bridgetown::Paginate::PaginationPage &&
6
6
  page.site.config.dig("pagination", "enabled")
7
7
  data = page.data.with_dot_access
@@ -32,19 +32,18 @@ module Bridgetown
32
32
  DEFAULT,
33
33
  site.config["pagination"] || {}
34
34
  )
35
- default_config["collection"] = "posts" unless site.uses_resource?
36
35
 
37
36
  # If disabled then simply quit
38
37
  unless default_config["enabled"]
39
38
  Bridgetown.logger.info "Pagination:", "disabled. Enable in site config" \
40
- " with pagination:\\n enabled: true"
39
+ " with pagination:\\n enabled: true"
41
40
  return
42
41
  end
43
42
 
44
43
  Bridgetown.logger.debug "Pagination:", "Starting"
45
44
 
46
45
  # Get all matching pages in the site found by the init hooks, and ensure they're
47
- # still in the site.pages array
46
+ # still in the site.generated_pages array
48
47
  templates = self.class.matching_templates.select do |page|
49
48
  site.generated_pages.include?(page) || site.resources.include?(page)
50
49
  end
@@ -96,18 +95,19 @@ module Bridgetown
96
95
  if page_to_remove.is_a?(Bridgetown::Resource::Base)
97
96
  page_to_remove.collection.resources.delete(page_to_remove)
98
97
  else
99
- site.pages.delete(page_to_remove)
98
+ site.generated_pages.delete(page_to_remove)
100
99
  end
101
100
  end
102
101
 
103
102
  # Create a proc that will delegate logging
104
103
  # Decoupling Bridgetown specific logging
105
104
  logging_lambda = ->(message, type = "info") do
106
- if type == "debug"
105
+ case type
106
+ when "debug"
107
107
  Bridgetown.logger.debug "Pagination:", message.to_s
108
- elsif type == "error"
108
+ when "error"
109
109
  Bridgetown.logger.error "Pagination:", message.to_s
110
- elsif type == "warn"
110
+ when "warn"
111
111
  Bridgetown.logger.warn "Pagination:", message.to_s
112
112
  else
113
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
 
@@ -12,7 +12,7 @@ module Bridgetown
12
12
  @logging_lambda = nil
13
13
  # The lambda used to create pages and add them to the site
14
14
  @page_add_lambda = nil
15
- # Lambda to remove a page from the site.pages collection
15
+ # Lambda to remove a page
16
16
  @page_remove_lambda = nil
17
17
  # Lambda to get all documents/posts in a particular collection (by name)
18
18
  @collection_by_name_lambda = nil
@@ -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
 
@@ -50,15 +53,20 @@ module Bridgetown
50
53
  template_config = Bridgetown::Utils.deep_merge_hashes(
51
54
  default_config,
52
55
  template.data["pagination"] || template.data["paginate"] || {}
53
- )
56
+ ).tap do |config|
57
+ config["collection"] = config["collection"].to_s if config["collection"].is_a?(Symbol)
58
+ config["category"] = config["category"].to_s if config["category"].is_a?(Symbol)
59
+ config["tag"] = config["tag"].to_s if config["tag"].is_a?(Symbol)
60
+ config["locale"] = config["locale"].to_s if config["locale"].is_a?(Symbol)
61
+ end
54
62
 
55
63
  # Is debugging enabled on the page level
56
64
  @debug = template_config["debug"]
57
65
  _debug_print_config_info(template_config, template.path)
58
66
 
59
- next if template_config["enabled"] == false
67
+ next unless template_config["enabled"]
60
68
 
61
- @logging_lambda.call "found page: " + template.path, "debug" unless @debug
69
+ @logging_lambda.call "found page: #{template.path}", "debug" unless @debug
62
70
 
63
71
  # Request all documents in all collections that the user has requested
64
72
  all_posts = get_docs_in_collections(template_config["collection"], template)
@@ -149,34 +157,34 @@ module Bridgetown
149
157
  r = 20
150
158
  f = "Pagination: ".rjust(20)
151
159
  # Debug print the config
152
- if @debug
153
- puts f + "----------------------------"
154
- puts f + "Page: " + page_path.to_s
155
- puts f + " Active configuration"
156
- puts f + " Enabled: ".ljust(r) + config["enabled"].to_s
157
- puts f + " Items per page: ".ljust(r) + config["per_page"].to_s
158
- puts f + " Permalink: ".ljust(r) + config["permalink"].to_s
159
- puts f + " Title: ".ljust(r) + config["title"].to_s
160
- puts f + " Limit: ".ljust(r) + config["limit"].to_s
161
- puts f + " Sort by: ".ljust(r) + config["sort_field"].to_s
162
- puts f + " Sort reverse: ".ljust(r) + config["sort_reverse"].to_s
163
-
164
- puts f + " Active Filters"
165
- puts f + " Collection: ".ljust(r) + config["collection"].to_s
166
- puts f + " Offset: ".ljust(r) + config["offset"].to_s
167
- puts f + " Category: ".ljust(r) + (config["category"].nil? || config["category"] == "posts" ? "[Not set]" : config["category"].to_s)
168
- puts f + " Tag: ".ljust(r) + (config["tag"].nil? ? "[Not set]" : config["tag"].to_s)
169
- puts f + " Locale: ".ljust(r) + (config["locale"].nil? ? "[Not set]" : config["locale"].to_s)
170
- 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)
171
179
  end
172
180
  # rubocop:enable Layout/LineLength
173
181
 
174
182
  # rubocop:disable Layout/LineLength
175
183
  def _debug_print_filtering_info(filter_name, before_count, after_count)
176
184
  # Debug print the config
177
- if @debug
178
- puts "Pagination: ".rjust(20) + " Filtering by: " + filter_name.to_s.ljust(9) + " " + before_count.to_s.rjust(3) + " => " + after_count.to_s
179
- 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}"
180
188
  end
181
189
  # rubocop:enable Layout/LineLength
182
190
 
@@ -258,7 +266,7 @@ module Bridgetown
258
266
  # So to unblock this common issue for the date field I simply iterate
259
267
  # once over every document and initialize the .date field explicitly
260
268
  if @debug
261
- 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"
262
270
  end
263
271
  using_posts.each do |u_post|
264
272
  next unless u_post.respond_to?("date")
@@ -347,8 +355,7 @@ module Bridgetown
347
355
 
348
356
  # 3. Create the paginator logic for this page, pass in the prev and next
349
357
  # page numbers, assign paginator to in-memory page
350
- # TODO: remove .pager by v1.0, deprecated
351
- newpage.paginator = newpage.pager = Paginator.new(
358
+ newpage.paginator = Paginator.new(
352
359
  config["per_page"],
353
360
  first_index_page_url,
354
361
  paginated_page_url,
@@ -374,11 +381,7 @@ module Bridgetown
374
381
  newpage.data["permalink"] = newpage.paginator.page_path if template.data["permalink"]
375
382
 
376
383
  # Transfer the title across to the new page
377
- tmp_title = if !template.data["title"]
378
- site_title
379
- else
380
- template.data["title"]
381
- end
384
+ tmp_title = template.data["title"] || site_title
382
385
 
383
386
  # If the user specified a title suffix to be added then let's add that
384
387
  # to all the pages except the first
@@ -415,40 +418,40 @@ module Bridgetown
415
418
  # simplest is to include all of the links to the pages preceeding the
416
419
  # current one (e.g for page 1 you get the list 2, 3, 4.... and for
417
420
  # page 2 you get the list 3,4,5...)
418
- if config["trail"] && !config["trail"].nil? && newpages.size.to_i.positive?
419
- trail_before = [config["trail"]["before"].to_i, 0].max
420
- trail_after = [config["trail"]["after"].to_i, 0].max
421
- trail_length = trail_before + trail_after + 1
422
-
423
- if trail_before.positive? || trail_after.positive?
424
- newpages.select do |npage|
425
- # Selecting the beginning of the trail
426
- idx_start = [npage.paginator.page - trail_before - 1, 0].max
427
- # Selecting the end of the trail
428
- idx_end = [idx_start + trail_length, newpages.size.to_i].min
429
-
430
- # Always attempt to maintain the max total of <trail_length> pages
431
- # in the trail (it will look better if the trail doesn't shrink)
432
- if idx_end - idx_start < trail_length
433
- # Attempt to pad the beginning if we have enough pages
434
- # Never go beyond the zero index
435
- idx_start = [
436
- idx_start - (trail_length - (idx_end - idx_start)),
437
- 0,
438
- ].max
439
- end
440
-
441
- # Convert the newpages array into a two dimensional array that has
442
- # [index, page_url] as items
443
- npage.paginator.page_trail = newpages[idx_start...idx_end] \
444
- .each_with_index.map do |ipage, idx|
445
- PageTrail.new(
446
- idx_start + idx + 1,
447
- ipage.paginator.page_path,
448
- ipage.data["title"]
449
- )
450
- end
451
- 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
+ )
452
455
  end
453
456
  end
454
457
  end
@@ -11,15 +11,14 @@ 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 = ""
18
18
  @name = index_pageandext.nil? ? "index#{template_ext}" : index_pageandext
19
19
  @path = page_to_copy.path
20
-
21
- # Creates the basename and ext member values
22
- process(@name)
20
+ @basename = File.basename(@path, ".*")
21
+ @ext = File.extname(@name)
23
22
 
24
23
  # Only need to copy the data part of the page as it already contains the
25
24
  # layout information
@@ -29,11 +28,7 @@ module Bridgetown
29
28
  # Store the current page and total page numbers in the pagination_info construct
30
29
  data["pagination_info"] = { "curr_page" => cur_page_nr, "total_pages" => total_pages }
31
30
 
32
- # Perform some validation that is also performed in Bridgetown::Page
33
- validate_data! page_to_copy.path
34
- validate_permalink! page_to_copy.path
35
-
36
- Bridgetown::Hooks.trigger :pages, :post_init, self
31
+ Bridgetown::Hooks.trigger :generated_pages, :post_init, self
37
32
  end
38
33
 
39
34
  # rubocop:disable Naming/AccessorMethodName
@@ -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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bridgetown-paginate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.3
4
+ version: 1.0.0.alpha3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bridgetown Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-07 00:00:00.000000000 Z
11
+ date: 2021-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bridgetown-core
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.21.3
19
+ version: 1.0.0.alpha3
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: 0.21.3
26
+ version: 1.0.0.alpha3
27
27
  description:
28
28
  email: maintainers@bridgetownrb.com
29
29
  executables: []
@@ -42,7 +42,7 @@ files:
42
42
  - lib/bridgetown-paginate/pagination_page.rb
43
43
  - lib/bridgetown-paginate/paginator.rb
44
44
  - lib/bridgetown-paginate/utils.rb
45
- homepage: https://github.com/bridgetownrb/bridgetown/tree/master/bridgetown-paginate
45
+ homepage: https://github.com/bridgetownrb/bridgetown/tree/main/bridgetown-paginate
46
46
  licenses:
47
47
  - MIT
48
48
  metadata: {}
@@ -57,9 +57,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
57
57
  version: '0'
58
58
  required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ">="
60
+ - - ">"
61
61
  - !ruby/object:Gem::Version
62
- version: '0'
62
+ version: 1.3.1
63
63
  requirements: []
64
64
  rubygems_version: 3.1.4
65
65
  signing_key: