bridgetown-paginate 0.19.3 → 0.20.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
  SHA256:
3
- metadata.gz: 1ca47493176c8ebed605f0667252e0a2ff0c11a6237a1a1db0b518bbdee16456
4
- data.tar.gz: 453cda37d5394446d8537fe437e03a5d5d8c2c7159b00827a4b1792364772dc1
3
+ metadata.gz: 3065f58ab8318220ef8bd61ea6f96bb093ecdc1e3743f4d6a17f455d278f0cc0
4
+ data.tar.gz: 2f6319d1120d70afcfde19f18d34c6a7201eb75e0801d44640cb55f35dd46c08
5
5
  SHA512:
6
- metadata.gz: f03ce120a726ba5661a40f049b09f43e398bc472a90ad1cfa07b0f4398c3f1a0dbfedd4d4a5278626fbfcce71c1bca5db88fba9709767541999a5805d3dd784e
7
- data.tar.gz: ade124514e84227a32a83068b68ce67230b62079ef91c6e5e8d4847ab7f2117ed9410e19ca3315a059da2a827bbe0ca7fccb378760f783261f672d7c1bd128f0
6
+ metadata.gz: 73cbae538916130c049dd7c1f4044150e02ce7558314b835e8e0446874833b863775a9726b64519a3224ee3e3d7c7522984fd3b945b591257adbc3ffb6383b30
7
+ data.tar.gz: 573b35021fd706df9be029e6a8946a78860d15a4d2bd2d9bd02053f368e2ebab0217933bcea3a0a16286a55738cc4f828113d0f70683193c23cdb6e477d63d49
@@ -2,12 +2,10 @@
2
2
 
3
3
  module Bridgetown
4
4
  module Paginate
5
- # The default configuration for the Paginator
5
+ # The default configuration for the Paginator
6
6
  DEFAULT = {
7
- "enabled" => false,
8
- "collection" => "posts",
9
7
  "offset" => 0, # Supports skipping x number of posts from the
10
- # beginning of the post list
8
+ # beginning of the post list
11
9
  "per_page" => 10,
12
10
  "permalink" => "/page/:num/", # Supports :num as customizable elements
13
11
  "title" => ":title (Page :num)", # Supports :num as customizable elements
@@ -17,13 +15,13 @@ module Bridgetown
17
15
  "limit" => 0, # Limit how many content objects to paginate (default: 0, means all)
18
16
  "trail" => {
19
17
  "before" => 0, # Limits how many links to show before the current page
20
- # in the pagination trail (0, means off, default: 0)
18
+ # in the pagination trail (0, means off, default: 0)
21
19
  "after" => 0, # Limits how many links to show after the current page
22
- # in the pagination trail (0 means off, default: 0)
20
+ # in the pagination trail (0 means off, default: 0)
23
21
  },
24
22
  "indexpage" => nil, # The default name of the index pages
25
23
  "extension" => "html", # The default extension for the output pages
26
- # (ignored if indexpage is nil)
24
+ # (ignored if indexpage is nil)
27
25
  "debug" => false, # Turns on debug output for the gem
28
26
  }.freeze
29
27
  end
@@ -1,9 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Handles Legacy Pages
3
4
  Bridgetown::Hooks.register :pages, :post_init, reloadable: false do |page|
4
5
  if page.class != Bridgetown::Paginate::PaginationPage &&
5
- page.site.config.dig("pagination", "enabled") &&
6
- page.data.dig("pagination", "enabled")
6
+ page.site.config.dig("pagination", "enabled")
7
+ data = page.data.with_dot_access
8
+ if (data.pagination.present? && data.pagination.enabled != false) ||
9
+ (data.paginate.present? && data.paginate.enabled != false)
10
+ Bridgetown::Paginate::PaginationGenerator.add_matching_template(page)
11
+ end
12
+ end
13
+ end
14
+
15
+ # Handles Resources
16
+ Bridgetown::Hooks.register :resources, :post_read, reloadable: false do |page|
17
+ if page.site.config.dig("pagination", "enabled") && (
18
+ (page.data.pagination.present? && page.data.pagination.enabled != false) ||
19
+ (page.data.paginate.present? && page.data.paginate.enabled != false)
20
+ )
7
21
  Bridgetown::Paginate::PaginationGenerator.add_matching_template(page)
8
22
  end
9
23
  end
@@ -33,6 +33,7 @@ module Bridgetown
33
33
  DEFAULT,
34
34
  site.config["pagination"] || {}
35
35
  )
36
+ default_config["collection"] = "posts" unless site.uses_resource?
36
37
 
37
38
  # If disabled then simply quit
38
39
  unless default_config["enabled"]
@@ -43,37 +44,33 @@ module Bridgetown
43
44
 
44
45
  Bridgetown.logger.debug "Pagination:", "Starting"
45
46
 
46
- ################ 0 ####################
47
47
  # Get all matching pages in the site found by the init hooks, and ensure they're
48
48
  # still in the site.pages array
49
49
  templates = self.class.matching_templates.select do |page|
50
- site.pages.include? page
50
+ site.pages.include?(page) || site.resources.include?(page)
51
51
  end
52
52
 
53
53
  # Get the default title of the site (used as backup when there is no
54
54
  # title available for pagination)
55
55
  site_title = site.data.dig("metadata", "title") || site.config["title"]
56
56
 
57
- ################ 1 ####################
58
57
  # Specify the callback function that returns the correct docs/posts
59
58
  # based on the collection name
60
- # "posts" are just another collection in Bridgetown but a specialized
61
- # version that require timestamps
62
- # This collection is the default and if the user doesn't specify a
59
+ # Posts collection is the default and if the user doesn't specify a
63
60
  # collection in their front-matter then that is the one we load
64
61
  # If the collection is not found then empty array is returned
65
- collection_by_name_lambda = lambda do |collection_name|
62
+ collection_by_name_lambda = ->(collection_name) do
66
63
  coll = []
67
64
  if collection_name == "all"
68
65
  # the 'all' collection_name is a special case and includes all
69
66
  # collections in the site (except posts!!)
70
67
  # this is useful when you want to list items across multiple collections
71
- site.collections.each do |coll_name, coll_data|
72
- next unless !coll_data.nil? && coll_name != "posts"
68
+ site.collections.each do |coll_name, collection|
69
+ next unless !collection.nil? && coll_name != "posts"
73
70
 
74
71
  # Exclude all pagination pages
75
- coll += coll_data.docs.reject do |doc|
76
- doc.data.key?("pagination")
72
+ coll += collection.each.reject do |doc|
73
+ doc.data.key?("pagination") || doc.data.key?("paginate")
77
74
  end
78
75
  end
79
76
  else
@@ -81,31 +78,32 @@ module Bridgetown
81
78
  return [] unless site.collections.key?(collection_name)
82
79
 
83
80
  # Exclude all pagination pages
84
- coll = site.collections[collection_name].docs.reject do |doc|
85
- doc.data.key?("pagination")
81
+ coll = site.collections[collection_name].each.reject do |doc|
82
+ doc.data.key?("pagination") || doc.data.key?("paginate")
86
83
  end
87
84
  end
88
85
  return coll
89
86
  end
90
87
 
91
- ################ 2 ####################
92
88
  # Create the proc that constructs the real-life site page
93
89
  # This is necessary to decouple the code from the Bridgetown site object
94
- page_add_lambda = lambda do |newpage|
95
- site.pages << newpage # Add the page to the site so that it is generated correctly
90
+ page_add_lambda = ->(newpage) do
91
+ site.add_generated_page newpage
96
92
  return newpage # Return the site to the calling code
97
93
  end
98
94
 
99
- ################ 2.5 ####################
100
95
  # lambda that removes a page from the site pages list
101
- page_remove_lambda = lambda do |page_to_remove|
102
- site.pages.delete_if { |page| page == page_to_remove }
96
+ page_remove_lambda = ->(page_to_remove) do
97
+ if page_to_remove.is_a?(Bridgetown::Resource::Base)
98
+ page_to_remove.collection.resources.delete(page_to_remove)
99
+ else
100
+ site.pages.delete(page_to_remove)
101
+ end
103
102
  end
104
103
 
105
- ################ 3 ####################
106
104
  # Create a proc that will delegate logging
107
105
  # Decoupling Bridgetown specific logging
108
- logging_lambda = lambda do |message, type = "info"|
106
+ logging_lambda = ->(message, type = "info") do
109
107
  if type == "debug"
110
108
  Bridgetown.logger.debug "Pagination:", message.to_s
111
109
  elsif type == "error"
@@ -117,7 +115,6 @@ module Bridgetown
117
115
  end
118
116
  end
119
117
 
120
- ################ 4 ####################
121
118
  # Now create and call the model with the real-life page creation proc and site data
122
119
  model = PaginationModel.new(
123
120
  logging_lambda,
@@ -126,6 +123,7 @@ module Bridgetown
126
123
  collection_by_name_lambda
127
124
  )
128
125
  count = model.run(default_config, templates, site_title)
126
+ self.class.matching_templates.clear
129
127
  Bridgetown.logger.info "Pagination:", "Complete, processed #{count} pagination page(s)"
130
128
  end
131
129
  end
@@ -33,7 +33,7 @@ module Bridgetown
33
33
  def run(default_config, templates, site_title) # rubocop:todo Metrics/AbcSize
34
34
  if templates.size.to_i <= 0
35
35
  @logging_lambda.call "is enabled in the config, but no paginated pages found." \
36
- " Add 'pagination:\\n enabled: true' to the front-matter of a page.", "warn"
36
+ " Add 'pagination:\\n collection: <label>' to the front-matter of a page.", "warn"
37
37
  return
38
38
  end
39
39
 
@@ -43,66 +43,63 @@ module Bridgetown
43
43
  templates.each do |template|
44
44
  # All pages that should be paginated need to include the pagination
45
45
  # config element
46
- if template.data["pagination"].is_a?(Hash)
47
- template_config = Bridgetown::Utils.deep_merge_hashes(
48
- default_config,
49
- template.data["pagination"] || {}
50
- )
46
+ unless template.data["pagination"].is_a?(Hash) || template.data["paginate"].is_a?(Hash)
47
+ next
48
+ end
51
49
 
52
- # Is debugging enabled on the page level
53
- @debug = template_config["debug"]
50
+ template_config = Bridgetown::Utils.deep_merge_hashes(
51
+ default_config,
52
+ template.data["pagination"] || template.data["paginate"] || {}
53
+ )
54
54
 
55
- _debug_print_config_info(template_config, template.path)
55
+ # Is debugging enabled on the page level
56
+ @debug = template_config["debug"]
57
+ _debug_print_config_info(template_config, template.path)
56
58
 
57
- # Only paginate the template if it is explicitly enabled
58
- # requiring this makes the logic simpler as I don't need to
59
- # determine which index pages were generated automatically and
60
- # which weren't
61
- if template_config["enabled"]
62
- @logging_lambda.call "found page: " + template.path, "debug" unless @debug
59
+ next if template_config["enabled"] == false
63
60
 
64
- # Request all documents in all collections that the user has requested
65
- all_posts = get_docs_in_collections(template_config["collection"])
61
+ @logging_lambda.call "found page: " + template.path, "debug" unless @debug
66
62
 
67
- # Create the necessary indexes for the posts
68
- all_categories = if template_config["category"]
69
- PaginationIndexer.index_documents_by(all_posts, "categories")
70
- end
71
- all_tags = if template_config["tag"]
72
- PaginationIndexer.index_documents_by(all_posts, "tags")
73
- end
74
- all_locales = if template_config["locale"]
75
- PaginationIndexer.index_documents_by(all_posts, "locale")
76
- end
77
-
78
- # Load in custom query index, if specified
79
- all_where_matches = if template_config["where_query"]
80
- PaginationIndexer.index_documents_by(
81
- all_posts, template_config["where_query"]
82
- )
83
- end
84
-
85
- documents_payload = {
86
- posts: all_posts,
87
- tags: all_tags,
88
- categories: all_categories,
89
- locales: all_locales,
90
- where_matches: all_where_matches,
91
- }
92
-
93
- # TODO: NOTE!!! This whole request for posts and indexing results
94
- # could be cached to improve performance, leaving like this for
95
- # now during testing
96
-
97
- # Now construct the pagination data for this template page
98
- paginate(
99
- template,
100
- template_config,
101
- site_title,
102
- documents_payload
103
- )
104
- end
105
- end
63
+ # Request all documents in all collections that the user has requested
64
+ all_posts = get_docs_in_collections(template_config["collection"], template)
65
+
66
+ # Create the necessary indexes for the posts
67
+ all_categories = if template_config["category"]
68
+ PaginationIndexer.index_documents_by(all_posts, "categories")
69
+ end
70
+ all_tags = if template_config["tag"]
71
+ PaginationIndexer.index_documents_by(all_posts, "tags")
72
+ end
73
+ all_locales = if template_config["locale"]
74
+ PaginationIndexer.index_documents_by(all_posts, "locale")
75
+ end
76
+
77
+ # Load in custom query index, if specified
78
+ all_where_matches = if template_config["where_query"]
79
+ PaginationIndexer.index_documents_by(
80
+ all_posts, template_config["where_query"]
81
+ )
82
+ end
83
+
84
+ documents_payload = {
85
+ posts: all_posts,
86
+ tags: all_tags,
87
+ categories: all_categories,
88
+ locales: all_locales,
89
+ where_matches: all_where_matches,
90
+ }
91
+
92
+ # TODO: NOTE!!! This whole request for posts and indexing results
93
+ # could be cached to improve performance, leaving like this for
94
+ # now during testing
95
+
96
+ # Now construct the pagination data for this template page
97
+ paginate(
98
+ template,
99
+ template_config,
100
+ site_title,
101
+ documents_payload
102
+ )
106
103
  end
107
104
 
108
105
  # Return the total number of templates found
@@ -114,7 +111,13 @@ module Bridgetown
114
111
  # specified
115
112
  # raw_collection_names can either be a list of collections separated by a
116
113
  # ',' or ' ' or a single string
117
- def get_docs_in_collections(raw_collection_names)
114
+ def get_docs_in_collections(raw_collection_names, template)
115
+ if raw_collection_names.blank?
116
+ @logging_lambda.call "Missing collection name for paginated page: " \
117
+ "#{template.relative_path}"
118
+ return []
119
+ end
120
+
118
121
  if @docs_by_collection_cache[raw_collection_names]
119
122
  return @docs_by_collection_cache[raw_collection_names]
120
123
  end
@@ -134,7 +137,7 @@ module Bridgetown
134
137
  end
135
138
 
136
139
  # Hidden documents should not not be processed anywhere.
137
- docs = docs.reject { |doc| doc["hidden"] }
140
+ docs = docs.reject { |doc| doc.data.exclude_from_pagination }
138
141
 
139
142
  @docs_by_collection_cache[raw_collection_names] = docs
140
143
 
@@ -261,13 +264,13 @@ module Bridgetown
261
264
  next unless u_post.respond_to?("date")
262
265
 
263
266
  tmp_date = u_post.date
264
- if !tmp_date || tmp_date.nil?
265
- if @debug
266
- puts "Pagination: ".rjust(20) +
267
- "Explicitly assigning date for doc: #{u_post.data["title"]} | #{u_post.path}"
268
- end
269
- u_post.date = File.mtime(u_post.path)
267
+ next unless !tmp_date || tmp_date.nil?
268
+
269
+ if @debug
270
+ puts "Pagination: ".rjust(20) +
271
+ "Explicitly assigning date for doc: #{u_post.data["title"]} | #{u_post.path}"
270
272
  end
273
+ u_post.date = File.mtime(u_post.path)
271
274
  end
272
275
 
273
276
  using_posts.sort! do |a, b|
@@ -324,19 +327,20 @@ module Bridgetown
324
327
  cur_page_nr,
325
328
  total_pages,
326
329
  index_page_with_ext,
327
- template.ext
330
+ template.extname
328
331
  )
329
332
 
330
333
  # 2. Create the url for the in-memory page (calc permalink etc),
331
334
  # construct the title, set all page.data values needed
332
335
  paginated_page_url = config["permalink"]
333
336
  first_index_page_url = if template.data["permalink"]
334
- Utils.ensure_trailing_slash(
335
- template.data["permalink"]
336
- )
337
+ template.data["permalink"]
338
+ elsif template.respond_to?(:relative_url)
339
+ template.relative_url
337
340
  else
338
- Utils.ensure_trailing_slash(template.dir)
341
+ template.dir
339
342
  end
343
+ first_index_page_url = Utils.ensure_trailing_slash(first_index_page_url)
340
344
  paginated_page_url = File.join(first_index_page_url, paginated_page_url)
341
345
 
342
346
  # 3. Create the paginator logic for this page, pass in the prev and next
@@ -389,8 +393,7 @@ module Bridgetown
389
393
  end
390
394
 
391
395
  # Signals that this page is automatically generated by the pagination logic
392
- # (we don't do this for the first page as it is there to mask the one we removed)
393
- newpage.data["autogen"] = "bridgetown-paginate" if cur_page_nr > 1
396
+ newpage.data["autogen"] = "bridgetown-paginate"
394
397
 
395
398
  # If there's only one post (like on a per_page: 1 scenario), let's be
396
399
  # helpful and supply a document variable
@@ -10,8 +10,7 @@ module Bridgetown
10
10
  # non-existing) pagination page. This page exists purely in memory and is
11
11
  # not read from disk
12
12
  #
13
- class PaginationPage < Bridgetown::Page
14
- # rubocop:todo Metrics/AbcSize
13
+ class PaginationPage < Bridgetown::GeneratedPage
15
14
  def initialize(page_to_copy, cur_page_nr, total_pages, index_pageandext, template_ext)
16
15
  @site = page_to_copy.site
17
16
  @base = ""
@@ -25,16 +24,7 @@ module Bridgetown
25
24
  # Only need to copy the data part of the page as it already contains the
26
25
  # layout information
27
26
  self.data = Bridgetown::Utils.deep_merge_hashes page_to_copy.data, {}
28
- if !page_to_copy.data["autopage"]
29
- self.content = page_to_copy.content
30
- elsif page_to_copy.data["autopage"].key?("display_name")
31
- # If the page is an auto page then migrate the necessary autopage info
32
- # across into the new pagination page (so that users can get the
33
- # correct keys etc)
34
- data["autopages"] = Bridgetown::Utils.deep_merge_hashes(
35
- page_to_copy.data["autopage"], {}
36
- )
37
- end
27
+ self.content = page_to_copy.content
38
28
 
39
29
  # Store the current page and total page numbers in the pagination_info construct
40
30
  data["pagination_info"] = { "curr_page" => cur_page_nr, "total_pages" => total_pages }
@@ -45,7 +35,6 @@ module Bridgetown
45
35
 
46
36
  Bridgetown::Hooks.trigger :pages, :post_init, self
47
37
  end
48
- # rubocop:enable Metrics/AbcSize
49
38
 
50
39
  # rubocop:disable Naming/AccessorMethodName
51
40
  def set_url(url_value)
@@ -90,6 +90,15 @@ module Bridgetown
90
90
  @last_page_path = Utils.format_page_number(paginated_page_url, @total_pages, @total_pages)
91
91
  end
92
92
 
93
+ # TODO: eventually deprecate documents and only have resources
94
+ def resources
95
+ documents
96
+ end
97
+
98
+ def total_resources
99
+ total_documents
100
+ end
101
+
93
102
  # Convert this Paginator's data to a Hash suitable for use by Liquid.
94
103
  #
95
104
  # Returns the Hash representation of this Paginator.
@@ -97,7 +106,9 @@ module Bridgetown
97
106
  {
98
107
  "per_page" => per_page,
99
108
  "documents" => documents,
109
+ "resources" => documents,
100
110
  "total_documents" => total_documents,
111
+ "total_resources" => total_resources,
101
112
  "total_pages" => total_pages,
102
113
  "page" => page,
103
114
  "page_path" => page_path,
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.19.3
4
+ version: 0.20.0
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-02-11 00:00:00.000000000 Z
11
+ date: 2021-03-16 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.19.3
19
+ version: 0.20.0
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.19.3
26
+ version: 0.20.0
27
27
  description:
28
28
  email: maintainers@bridgetownrb.com
29
29
  executables: []