bridgetown-paginate 0.11.0 → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,59 +2,56 @@
2
2
 
3
3
  module Bridgetown
4
4
  module Paginate
5
- module Generator
6
- #
7
- # This page handles the creation of the fake pagination pages based on the
8
- # original page configuration.
9
- # The code does the same things as the default Bridgetown page.rb code but
10
- # just forces the code to look into the template instead of the (currently
11
- # non-existing) pagination page. This page exists purely in memory and is
12
- # not read from disk
13
- #
14
- class PaginationPage < Bridgetown::Page
15
- def initialize(page_to_copy, cur_page_nr, total_pages, index_pageandext, template_ext)
16
- @site = page_to_copy.site
17
- @base = ""
18
- @url = ""
19
- @name = index_pageandext.nil? ? "index#{template_ext}" : index_pageandext
20
- @path = page_to_copy.path
5
+ #
6
+ # This page handles the creation of the fake pagination pages based on the
7
+ # original page configuration.
8
+ # The code does the same things as the default Bridgetown page.rb code but
9
+ # just forces the code to look into the template instead of the (currently
10
+ # non-existing) pagination page. This page exists purely in memory and is
11
+ # not read from disk
12
+ #
13
+ class PaginationPage < Bridgetown::Page
14
+ def initialize(page_to_copy, cur_page_nr, total_pages, index_pageandext, template_ext)
15
+ @site = page_to_copy.site
16
+ @base = ""
17
+ @url = ""
18
+ @name = index_pageandext.nil? ? "index#{template_ext}" : index_pageandext
19
+ @path = page_to_copy.path
21
20
 
22
- # Creates the basename and ext member values
23
- process(@name)
21
+ # Creates the basename and ext member values
22
+ process(@name)
24
23
 
25
- # Only need to copy the data part of the page as it already contains the
26
- # layout information
27
- 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
24
+ # Only need to copy the data part of the page as it already contains the
25
+ # layout information
26
+ self.data = Bridgetown::Utils.deep_merge_hashes page_to_copy.data, {}
27
+ if !page_to_copy.data["autopage"]
28
+ self.content = page_to_copy.content
29
+ elsif page_to_copy.data["autopage"].key?("display_name")
30
+ # If the page is an auto page then migrate the necessary autopage info
31
+ # across into the new pagination page (so that users can get the
32
+ # correct keys etc)
33
+ data["autopages"] = Bridgetown::Utils.deep_merge_hashes(
34
+ page_to_copy.data["autopage"], {}
35
+ )
36
+ end
38
37
 
39
- # Store the current page and total page numbers in the pagination_info construct
40
- data["pagination_info"] = { "curr_page" => cur_page_nr, "total_pages" => total_pages }
38
+ # Store the current page and total page numbers in the pagination_info construct
39
+ data["pagination_info"] = { "curr_page" => cur_page_nr, "total_pages" => total_pages }
41
40
 
42
- # Perform some validation that is also performed in Bridgetown::Page
43
- validate_data! page_to_copy.path
44
- validate_permalink! page_to_copy.path
41
+ # Perform some validation that is also performed in Bridgetown::Page
42
+ validate_data! page_to_copy.path
43
+ validate_permalink! page_to_copy.path
45
44
 
46
- # TODO: Trigger a page event
47
- # Bridgetown::Hooks.trigger :pages, :post_init, self
48
- end
45
+ Bridgetown::Hooks.trigger :pages, :post_init, self
46
+ end
49
47
 
50
- # rubocop:disable Naming/AccessorMethodName
51
- def set_url(url_value)
52
- @path = url_value.delete_prefix "/"
53
- @dir = File.dirname(@path)
54
- @url = url_value
55
- end
56
- # rubocop:enable Naming/AccessorMethodName
48
+ # rubocop:disable Naming/AccessorMethodName
49
+ def set_url(url_value)
50
+ @path = url_value.delete_prefix "/"
51
+ @dir = File.dirname(@path)
52
+ @url = url_value
57
53
  end
54
+ # rubocop:enable Naming/AccessorMethodName
58
55
  end
59
56
  end
60
57
  end
@@ -2,137 +2,135 @@
2
2
 
3
3
  module Bridgetown
4
4
  module Paginate
5
- module Generator
6
- #
7
- # Handles the preparation of all the documents based on the current page index
5
+ #
6
+ # Handles the preparation of all the documents based on the current page index
7
+ #
8
+ class Paginator
9
+ attr_reader :page, :per_page, :documents, :total_documents, :total_pages,
10
+ :previous_page, :previous_page_path, :next_page, :next_page_path, :page_path,
11
+ :first_page, :first_page_path, :last_page, :last_page_path
12
+ attr_accessor :page_trail
13
+
14
+ # Initialize a new Paginator.
8
15
  #
9
- class Paginator
10
- attr_reader :page, :per_page, :documents, :total_documents, :total_pages,
11
- :previous_page, :previous_page_path, :next_page, :next_page_path, :page_path,
12
- :first_page, :first_page_path, :last_page, :last_page_path
13
- attr_accessor :page_trail
16
+ def initialize(
17
+ config_per_page,
18
+ first_index_page_url,
19
+ paginated_page_url,
20
+ documents,
21
+ cur_page_nr,
22
+ num_pages,
23
+ default_indexpage,
24
+ default_ext
25
+ )
26
+ @page = cur_page_nr
27
+ @per_page = config_per_page.to_i
28
+ @total_pages = num_pages
14
29
 
15
- # Initialize a new Paginator.
16
- #
17
- def initialize(
18
- config_per_page,
19
- first_index_page_url,
20
- paginated_page_url,
21
- documents,
22
- cur_page_nr,
23
- num_pages,
24
- default_indexpage,
25
- default_ext
26
- )
27
- @page = cur_page_nr
28
- @per_page = config_per_page.to_i
29
- @total_pages = num_pages
30
+ if @page > @total_pages
31
+ raise "page number can't be greater than total pages:" \
32
+ " #{@page} > #{@total_pages}"
33
+ end
30
34
 
31
- if @page > @total_pages
32
- raise "page number can't be greater than total pages:" \
33
- " #{@page} > #{@total_pages}"
34
- end
35
+ init = (@page - 1) * @per_page
36
+ offset = if init + @per_page - 1 >= documents.size
37
+ documents.size
38
+ else
39
+ init + @per_page - 1
40
+ end
35
41
 
36
- init = (@page - 1) * @per_page
37
- offset = if init + @per_page - 1 >= documents.size
38
- documents.size
39
- else
40
- init + @per_page - 1
41
- end
42
+ # Ensure that the current page has correct extensions if needed
43
+ this_page_url = Utils.ensure_full_path(
44
+ @page == 1 ? first_index_page_url : paginated_page_url,
45
+ !default_indexpage || default_indexpage.empty? ? "index" : default_indexpage,
46
+ !default_ext || default_ext.empty? ? ".html" : default_ext
47
+ )
42
48
 
43
- # Ensure that the current page has correct extensions if needed
44
- this_page_url = Utils.ensure_full_path(
45
- @page == 1 ? first_index_page_url : paginated_page_url,
46
- !default_indexpage || default_indexpage.empty? ? "index" : default_indexpage,
47
- !default_ext || default_ext.empty? ? ".html" : default_ext
49
+ # To support customizable pagination pages we attempt to explicitly
50
+ # append the page name to the url incase the user is using extensionless permalinks.
51
+ if default_indexpage&.length&.positive?
52
+ # Adjust first page url
53
+ first_index_page_url = Utils.ensure_full_path(
54
+ first_index_page_url, default_indexpage, default_ext
48
55
  )
56
+ # Adjust the paginated pages as well
57
+ paginated_page_url = Utils.ensure_full_path(
58
+ paginated_page_url, default_indexpage, default_ext
59
+ )
60
+ end
49
61
 
50
- # To support customizable pagination pages we attempt to explicitly
51
- # append the page name to the url incase the user is using extensionless permalinks.
52
- if default_indexpage&.length&.positive?
53
- # Adjust first page url
54
- first_index_page_url = Utils.ensure_full_path(
55
- first_index_page_url, default_indexpage, default_ext
56
- )
57
- # Adjust the paginated pages as well
58
- paginated_page_url = Utils.ensure_full_path(
59
- paginated_page_url, default_indexpage, default_ext
60
- )
61
- end
62
-
63
- @total_documents = documents.size
64
- @documents = documents[init..offset]
65
- @page_path = Utils.format_page_number(this_page_url, cur_page_nr, @total_pages)
62
+ @total_documents = documents.size
63
+ @documents = documents[init..offset]
64
+ @page_path = Utils.format_page_number(this_page_url, cur_page_nr, @total_pages)
66
65
 
67
- @previous_page = @page != 1 ? @page - 1 : nil
68
- @previous_page_path = unless @page == 1
69
- if @page == 2
70
- Utils.format_page_number(
71
- first_index_page_url, 1, @total_pages
72
- )
73
- else
74
- Utils.format_page_number(
75
- paginated_page_url,
76
- @previous_page,
77
- @total_pages
78
- )
79
- end
66
+ @previous_page = @page != 1 ? @page - 1 : nil
67
+ @previous_page_path = unless @page == 1
68
+ if @page == 2
69
+ Utils.format_page_number(
70
+ first_index_page_url, 1, @total_pages
71
+ )
72
+ else
73
+ Utils.format_page_number(
74
+ paginated_page_url,
75
+ @previous_page,
76
+ @total_pages
77
+ )
80
78
  end
81
- @next_page = @page != @total_pages ? @page + 1 : nil
82
- @next_page_path = if @page != @total_pages
83
- Utils.format_page_number(
84
- paginated_page_url, @next_page, @total_pages
85
- )
86
- end
79
+ end
80
+ @next_page = @page != @total_pages ? @page + 1 : nil
81
+ @next_page_path = if @page != @total_pages
82
+ Utils.format_page_number(
83
+ paginated_page_url, @next_page, @total_pages
84
+ )
85
+ end
87
86
 
88
- @first_page = 1
89
- @first_page_path = Utils.format_page_number(first_index_page_url, 1, @total_pages)
90
- @last_page = @total_pages
91
- @last_page_path = Utils.format_page_number(paginated_page_url, @total_pages, @total_pages)
92
- end
87
+ @first_page = 1
88
+ @first_page_path = Utils.format_page_number(first_index_page_url, 1, @total_pages)
89
+ @last_page = @total_pages
90
+ @last_page_path = Utils.format_page_number(paginated_page_url, @total_pages, @total_pages)
91
+ end
93
92
 
94
- # Convert this Paginator's data to a Hash suitable for use by Liquid.
95
- #
96
- # Returns the Hash representation of this Paginator.
97
- def to_liquid
98
- {
99
- "per_page" => per_page,
100
- "documents" => documents,
101
- "total_documents" => total_documents,
102
- "total_pages" => total_pages,
103
- "page" => page,
104
- "page_path" => page_path,
105
- "previous_page" => previous_page,
106
- "previous_page_path" => previous_page_path,
107
- "next_page" => next_page,
108
- "next_page_path" => next_page_path,
109
- "first_page" => first_page,
110
- "first_page_path" => first_page_path,
111
- "last_page" => last_page,
112
- "last_page_path" => last_page_path,
113
- "page_trail" => page_trail,
114
- }
115
- end
93
+ # Convert this Paginator's data to a Hash suitable for use by Liquid.
94
+ #
95
+ # Returns the Hash representation of this Paginator.
96
+ def to_liquid
97
+ {
98
+ "per_page" => per_page,
99
+ "documents" => documents,
100
+ "total_documents" => total_documents,
101
+ "total_pages" => total_pages,
102
+ "page" => page,
103
+ "page_path" => page_path,
104
+ "previous_page" => previous_page,
105
+ "previous_page_path" => previous_page_path,
106
+ "next_page" => next_page,
107
+ "next_page_path" => next_page_path,
108
+ "first_page" => first_page,
109
+ "first_page_path" => first_page_path,
110
+ "last_page" => last_page,
111
+ "last_page_path" => last_page_path,
112
+ "page_trail" => page_trail,
113
+ }
116
114
  end
115
+ end
117
116
 
118
- # Small utility class that handles individual pagination trails
119
- # and makes them easier to work with in Liquid
120
- class PageTrail
121
- attr_reader :num, :path, :title
117
+ # Small utility class that handles individual pagination trails
118
+ # and makes them easier to work with in Liquid
119
+ class PageTrail
120
+ attr_reader :num, :path, :title
122
121
 
123
- def initialize(num, path, title)
124
- @num = num
125
- @path = path
126
- @title = title
127
- end
122
+ def initialize(num, path, title)
123
+ @num = num
124
+ @path = path
125
+ @title = title
126
+ end
128
127
 
129
- def to_liquid
130
- {
131
- "num" => num,
132
- "path" => path,
133
- "title" => title,
134
- }
135
- end
128
+ def to_liquid
129
+ {
130
+ "num" => num,
131
+ "path" => path,
132
+ "title" => title,
133
+ }
136
134
  end
137
135
  end
138
136
  end
@@ -2,136 +2,134 @@
2
2
 
3
3
  module Bridgetown
4
4
  module Paginate
5
- module Generator
6
- # Static utility functions that are used in the code and
7
- # don't belong in once place in particular
8
- class Utils
9
- # Static: Calculate the number of pages.
10
- #
11
- # all_posts - The Array of all Posts.
12
- # per_page - The Integer of entries per page.
13
- #
14
- # Returns the Integer number of pages.
15
- def self.calculate_number_of_pages(all_posts, per_page)
16
- (all_posts.size.to_f / per_page.to_i).ceil
17
- end
5
+ # Static utility functions that are used in the code and
6
+ # don't belong in once place in particular
7
+ class Utils
8
+ # Static: Calculate the number of pages.
9
+ #
10
+ # all_posts - The Array of all Posts.
11
+ # per_page - The Integer of entries per page.
12
+ #
13
+ # Returns the Integer number of pages.
14
+ def self.calculate_number_of_pages(all_posts, per_page)
15
+ (all_posts.size.to_f / per_page.to_i).ceil
16
+ end
18
17
 
19
- # Static: returns a fully formatted string with the current (:num) page
20
- # number and maximum (:max) page count replaced if configured
21
- #
22
- def self.format_page_number(to_format, cur_page_nr, total_page_count = nil)
23
- s = to_format.sub(":num", cur_page_nr.to_s)
24
- s = s.sub(":max", total_page_count.to_s) unless total_page_count.nil?
18
+ # Static: returns a fully formatted string with the current (:num) page
19
+ # number and maximum (:max) page count replaced if configured
20
+ #
21
+ def self.format_page_number(to_format, cur_page_nr, total_page_count = nil)
22
+ s = to_format.sub(":num", cur_page_nr.to_s)
23
+ s = s.sub(":max", total_page_count.to_s) unless total_page_count.nil?
25
24
 
26
- s
27
- end
25
+ s
26
+ end
28
27
 
29
- # Static: returns a fully formatted string with the :title variable and
30
- # the current (:num) page number and maximum (:max) page count replaced
31
- #
32
- def self.format_page_title(to_format, title, cur_page_nr = nil, total_page_count = nil)
33
- format_page_number(to_format.sub(":title", title.to_s), cur_page_nr, total_page_count)
34
- end
28
+ # Static: returns a fully formatted string with the :title variable and
29
+ # the current (:num) page number and maximum (:max) page count replaced
30
+ #
31
+ def self.format_page_title(to_format, title, cur_page_nr = nil, total_page_count = nil)
32
+ format_page_number(to_format.sub(":title", title.to_s), cur_page_nr, total_page_count)
33
+ end
35
34
 
36
- # Static: Return a String version of the input which has a leading dot.
37
- # If the input already has a dot in position zero, it will be
38
- # returned unchanged.
39
- #
40
- # path - a String path
41
- #
42
- # Returns the path with a leading slash
43
- def self.ensure_leading_dot(path)
44
- path[0..0] == "." ? path : ".#{path}"
45
- end
35
+ # Static: Return a String version of the input which has a leading dot.
36
+ # If the input already has a dot in position zero, it will be
37
+ # returned unchanged.
38
+ #
39
+ # path - a String path
40
+ #
41
+ # Returns the path with a leading slash
42
+ def self.ensure_leading_dot(path)
43
+ path[0..0] == "." ? path : ".#{path}"
44
+ end
46
45
 
47
- # Static: Return a String version of the input which has a leading slash.
48
- # If the input already has a forward slash in position zero, it will be
49
- # returned unchanged.
50
- #
51
- # path - a String path
52
- #
53
- # Returns the path with a leading slash
54
- def self.ensure_leading_slash(path)
55
- path[0..0] == "/" ? path : "/#{path}"
56
- end
46
+ # Static: Return a String version of the input which has a leading slash.
47
+ # If the input already has a forward slash in position zero, it will be
48
+ # returned unchanged.
49
+ #
50
+ # path - a String path
51
+ #
52
+ # Returns the path with a leading slash
53
+ def self.ensure_leading_slash(path)
54
+ path[0..0] == "/" ? path : "/#{path}"
55
+ end
56
+
57
+ # Static: Return a String version of the input without a leading slash.
58
+ #
59
+ # path - a String path
60
+ #
61
+ # Returns the input without the leading slash
62
+ def self.remove_leading_slash(path)
63
+ path[0..0] == "/" ? path[1..-1] : path
64
+ end
65
+
66
+ # Static: Return a String version of the input which has a trailing slash.
67
+ # If the input already has a forward slash at the end, it will be
68
+ # returned unchanged.
69
+ #
70
+ # path - a String path
71
+ #
72
+ # Returns the path with a trailing slash
73
+ def self.ensure_trailing_slash(path)
74
+ path[-1] == "/" ? path : "#{path}/"
75
+ end
57
76
 
58
- # Static: Return a String version of the input without a leading slash.
59
- #
60
- # path - a String path
61
- #
62
- # Returns the input without the leading slash
63
- def self.remove_leading_slash(path)
64
- path[0..0] == "/" ? path[1..-1] : path
77
+ #
78
+ # Sorting routine used for ordering posts by custom fields.
79
+ # Handles Strings separately as we want a case-insenstive sorting
80
+ #
81
+ # rubocop:disable Naming/MethodParameterName, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
82
+ def self.sort_values(a, b)
83
+ if a.nil? && !b.nil?
84
+ return -1
85
+ elsif !a.nil? && b.nil?
86
+ return 1
65
87
  end
66
88
 
67
- # Static: Return a String version of the input which has a trailing slash.
68
- # If the input already has a forward slash at the end, it will be
69
- # returned unchanged.
70
- #
71
- # path - a String path
72
- #
73
- # Returns the path with a trailing slash
74
- def self.ensure_trailing_slash(path)
75
- path[-1] == "/" ? path : "#{path}/"
89
+ return a.downcase <=> b.downcase if a.is_a?(String)
90
+
91
+ if a.respond_to?("to_datetime") && b.respond_to?("to_datetime")
92
+ return a.to_datetime <=> b.to_datetime
76
93
  end
77
94
 
78
- #
79
- # Sorting routine used for ordering posts by custom fields.
80
- # Handles Strings separately as we want a case-insenstive sorting
81
- #
82
- # rubocop:disable Naming/MethodParameterName, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
83
- def self.sort_values(a, b)
84
- if a.nil? && !b.nil?
85
- return -1
86
- elsif !a.nil? && b.nil?
87
- return 1
88
- end
89
-
90
- return a.downcase <=> b.downcase if a.is_a?(String)
91
-
92
- if a.respond_to?("to_datetime") && b.respond_to?("to_datetime")
93
- return a.to_datetime <=> b.to_datetime
94
- end
95
-
96
- # By default use the built in sorting for the data type
97
- a <=> b
95
+ # By default use the built in sorting for the data type
96
+ a <=> b
97
+ end
98
+ # rubocop:enable Naming/MethodParameterName, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
99
+
100
+ # Retrieves the given sort field from the given post
101
+ # the sort_field variable can be a hierarchical value on the form
102
+ # "parent_field:child_field" repeated as many times as needed
103
+ # only the leaf child_field will be retrieved
104
+ def self.sort_get_post_data(post_data, sort_field)
105
+ # Begin by splitting up the sort_field by (;,:.)
106
+ sort_split = sort_field.split(":")
107
+ sort_value = post_data
108
+
109
+ sort_split.each do |r_key|
110
+ key = r_key.downcase.strip # Remove any erronious whitespace and convert to lower case
111
+ return nil unless sort_value.key?(key)
112
+
113
+ # Work my way through the hash
114
+ sort_value = sort_value[key]
98
115
  end
99
- # rubocop:enable Naming/MethodParameterName, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
100
-
101
- # Retrieves the given sort field from the given post
102
- # the sort_field variable can be a hierarchical value on the form
103
- # "parent_field:child_field" repeated as many times as needed
104
- # only the leaf child_field will be retrieved
105
- def self.sort_get_post_data(post_data, sort_field)
106
- # Begin by splitting up the sort_field by (;,:.)
107
- sort_split = sort_field.split(":")
108
- sort_value = post_data
109
-
110
- sort_split.each do |r_key|
111
- key = r_key.downcase.strip # Remove any erronious whitespace and convert to lower case
112
- return nil unless sort_value.key?(key)
113
-
114
- # Work my way through the hash
115
- sort_value = sort_value[key]
116
- end
117
-
118
- # If the sort value is a hash then return nil else return the value
119
- if sort_value.is_a?(Hash)
120
- nil
121
- else
122
- sort_value
123
- end
116
+
117
+ # If the sort value is a hash then return nil else return the value
118
+ if sort_value.is_a?(Hash)
119
+ nil
120
+ else
121
+ sort_value
124
122
  end
123
+ end
125
124
 
126
- # Ensures that the passed in url has a index and extension applied
127
- def self.ensure_full_path(url, default_index, default_ext)
128
- if url.end_with?("/")
129
- url + default_index + default_ext
130
- elsif !url.include?(".")
131
- url + default_index
132
- else
133
- url
134
- end
125
+ # Ensures that the passed in url has a index and extension applied
126
+ def self.ensure_full_path(url, default_index, default_ext)
127
+ if url.end_with?("/")
128
+ url + default_index + default_ext
129
+ elsif !url.include?(".")
130
+ url + default_index
131
+ else
132
+ url
135
133
  end
136
134
  end
137
135
  end