bridgetown-paginate 0.21.4 → 1.0.0.alpha4

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: b71e882e5543a240bc6a6f628ad81c4d93fd09f4df967a2add259a9b18fc6fae
4
- data.tar.gz: e5ccdab51aa1c522d611f934f8cbd1d031424a1e553c11b2b943dcede0a8aa6a
3
+ metadata.gz: f3f90c0d855d1ed1740df8b21ea50f98d4ff2877fd764da4d6990411aa334753
4
+ data.tar.gz: aa273ae39a092c540a78632dd15dbcb13adee465b6762c6e83e5d142a34e9f70
5
5
  SHA512:
6
- metadata.gz: e7a73d861b11556dca0a1018759ae80034b70c64a3b4efcf3c76cbaf6ba64a6caed19c34d434eb8889b1f8fd0152c4012a0b2add70581e66204fa7d83c9f2609
7
- data.tar.gz: b7ea940094a639ca9ac7aa669e049aa625fc19d7c1e335d3f5bdb7fff5b0debb82687e8732fa33613007eb89975820bfc9c6a9bb473db6d217ac7433d03eb2d4
6
+ metadata.gz: 1e6897fc2511da348b6439038248603f76627620b78645dd55ba665ae3fcecfbb66fe50f7a8f5fdafa3e83ee55e98e667bb054e2b429d13fbc29010bd687b6fe
7
+ data.tar.gz: af6e375ed18f1924116f5ec2a59c55e33a2eb91c41d45a3e048d8bcbf2662c73361d6e1c51839b1682a832b4a942ec8a79d423741685e585346989cd043a6e6d
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
 
@@ -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")
@@ -352,8 +355,7 @@ module Bridgetown
352
355
 
353
356
  # 3. Create the paginator logic for this page, pass in the prev and next
354
357
  # page numbers, assign paginator to in-memory page
355
- # TODO: remove .pager by v1.0, deprecated
356
- newpage.paginator = newpage.pager = Paginator.new(
358
+ newpage.paginator = Paginator.new(
357
359
  config["per_page"],
358
360
  first_index_page_url,
359
361
  paginated_page_url,
@@ -379,11 +381,7 @@ module Bridgetown
379
381
  newpage.data["permalink"] = newpage.paginator.page_path if template.data["permalink"]
380
382
 
381
383
  # Transfer the title across to the new page
382
- tmp_title = if !template.data["title"]
383
- site_title
384
- else
385
- template.data["title"]
386
- end
384
+ tmp_title = template.data["title"] || site_title
387
385
 
388
386
  # If the user specified a title suffix to be added then let's add that
389
387
  # to all the pages except the first
@@ -420,40 +418,40 @@ module Bridgetown
420
418
  # simplest is to include all of the links to the pages preceeding the
421
419
  # current one (e.g for page 1 you get the list 2, 3, 4.... and for
422
420
  # page 2 you get the list 3,4,5...)
423
- if config["trail"] && !config["trail"].nil? && newpages.size.to_i.positive?
424
- trail_before = [config["trail"]["before"].to_i, 0].max
425
- trail_after = [config["trail"]["after"].to_i, 0].max
426
- trail_length = trail_before + trail_after + 1
427
-
428
- if trail_before.positive? || trail_after.positive?
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
- )
455
- end
456
- 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
+ )
457
455
  end
458
456
  end
459
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.4
4
+ version: 1.0.0.alpha4
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-09-10 00:00:00.000000000 Z
11
+ date: 2021-10-19 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.4
19
+ version: 1.0.0.alpha4
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.4
26
+ version: 1.0.0.alpha4
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: