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.
- checksums.yaml +4 -4
- data/lib/bridgetown-paginate.rb +11 -12
- data/lib/bridgetown-paginate/defaults.rb +23 -25
- data/lib/bridgetown-paginate/hooks.rb +9 -0
- data/lib/bridgetown-paginate/pagination_generator.rb +112 -102
- data/lib/bridgetown-paginate/pagination_indexer.rb +104 -92
- data/lib/bridgetown-paginate/pagination_model.rb +376 -369
- data/lib/bridgetown-paginate/pagination_page.rb +43 -46
- data/lib/bridgetown-paginate/paginator.rb +114 -116
- data/lib/bridgetown-paginate/utils.rb +115 -117
- metadata +5 -4
@@ -2,59 +2,56 @@
|
|
2
2
|
|
3
3
|
module Bridgetown
|
4
4
|
module Paginate
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
23
|
-
|
21
|
+
# Creates the basename and ext member values
|
22
|
+
process(@name)
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
-
|
40
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
47
|
-
|
48
|
-
end
|
45
|
+
Bridgetown::Hooks.trigger :pages, :post_init, self
|
46
|
+
end
|
49
47
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
51
|
-
|
52
|
-
|
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
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
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
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
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
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
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
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
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
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
122
|
+
def initialize(num, path, title)
|
123
|
+
@num = num
|
124
|
+
@path = path
|
125
|
+
@title = title
|
126
|
+
end
|
128
127
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
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
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
27
|
-
|
25
|
+
s
|
26
|
+
end
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
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
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
68
|
-
|
69
|
-
|
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
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
#
|
97
|
-
|
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
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
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
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
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
|