Class: Paginate::Pager
- Inherits:
-
Object
- Object
- Paginate::Pager
- Defined in:
- lib/fenton-jekyll-plugin/jb_jekyll_paging.rb
Instance Attribute Summary collapse
-
#next_page ⇒ Object
readonly
Returns the value of attribute next_page.
-
#next_page_path ⇒ Object
readonly
Returns the value of attribute next_page_path.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#posts ⇒ Object
readonly
Returns the value of attribute posts.
-
#previous_page ⇒ Object
readonly
Returns the value of attribute previous_page.
-
#previous_page_path ⇒ Object
readonly
Returns the value of attribute previous_page_path.
-
#total_pages ⇒ Object
readonly
Returns the value of attribute total_pages.
-
#total_posts ⇒ Object
readonly
Returns the value of attribute total_posts.
Class Method Summary collapse
-
.calculate_pages(all_posts, per_page) ⇒ Object
Calculate the number of pages.
-
.ensure_leading_slash(path) ⇒ Object
Static: Return a String version of the input which has a leading slash.
-
.in_hierarchy(source, page_dir, paginate_path) ⇒ Object
Determine if the subdirectories of the two paths are the same relative to source.
-
.paginate_path(site, num_page) ⇒ Object
Static: Return the pagination path of the page.
-
.pagination_candidate?(config, page) ⇒ Boolean
Static: Determine if a page is a possible candidate to be a template page.
-
.pagination_enabled?(site) ⇒ Boolean
Determine if pagination is enabled the site.
-
.remove_leading_slash(path) ⇒ Object
Static: Return a String version of the input without a leading slash.
Instance Method Summary collapse
-
#initialize(site, page, all_posts, num_pages = nil) ⇒ Pager
constructor
Initialize a new Pager.
-
#to_liquid ⇒ Object
Convert this Pager’s data to a Hash suitable for use by Liquid.
Constructor Details
#initialize(site, page, all_posts, num_pages = nil) ⇒ Pager
Initialize a new Pager.
site - the Jekyll::Site object page - The Integer page number. all_posts - The Array of all the site’s Posts. num_pages - The Integer number of pages or nil if you’d like the number
of pages calculated.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 178 def initialize(site, page, all_posts, num_pages = nil) @page = page @per_page = site.config['page_size'].to_i @total_pages = num_pages || Pager.calculate_pages(all_posts, @per_page) if @page > @total_pages raise RuntimeError, "page number can't be greater than total pages: #{@page} > #{@total_pages}" end init = (@page - 1) * @per_page offset = (init + @per_page - 1) >= all_posts.size ? all_posts.size : (init + @per_page - 1) @total_posts = all_posts.size @posts = all_posts[init..offset] @previous_page = @page != 1 ? @page - 1 : nil @previous_page_path = Pager.paginate_path(site, @previous_page) @next_page = @page != @total_pages ? @page + 1 : nil @next_page_path = Pager.paginate_path(site, @next_page) end |
Instance Attribute Details
#next_page ⇒ Object (readonly)
Returns the value of attribute next_page.
81 82 83 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 81 def next_page @next_page end |
#next_page_path ⇒ Object (readonly)
Returns the value of attribute next_page_path.
81 82 83 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 81 def next_page_path @next_page_path end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
81 82 83 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 81 def page @page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
81 82 83 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 81 def per_page @per_page end |
#posts ⇒ Object (readonly)
Returns the value of attribute posts.
81 82 83 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 81 def posts @posts end |
#previous_page ⇒ Object (readonly)
Returns the value of attribute previous_page.
81 82 83 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 81 def previous_page @previous_page end |
#previous_page_path ⇒ Object (readonly)
Returns the value of attribute previous_page_path.
81 82 83 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 81 def previous_page_path @previous_page_path end |
#total_pages ⇒ Object (readonly)
Returns the value of attribute total_pages.
81 82 83 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 81 def total_pages @total_pages end |
#total_posts ⇒ Object (readonly)
Returns the value of attribute total_posts.
81 82 83 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 81 def total_posts @total_posts end |
Class Method Details
.calculate_pages(all_posts, per_page) ⇒ Object
Calculate the number of pages.
all_posts - The Array of all Posts. per_page - The Integer of entries per page.
Returns the Integer number of pages.
90 91 92 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 90 def self.calculate_pages(all_posts, per_page) (all_posts.size.to_f / per_page.to_i).ceil end |
.ensure_leading_slash(path) ⇒ Object
Static: Return a String version of the input which has a leading slash.
If the input already has a forward slash in position zero, it will be
returned unchanged.
path - a String path
Returns the path with a leading slash
158 159 160 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 158 def self.ensure_leading_slash(path) path[0..0] == '/' ? path : "/#{path}" end |
.in_hierarchy(source, page_dir, paginate_path) ⇒ Object
Determine if the subdirectories of the two paths are the same relative to source
source - the site source page_dir - the directory of the Jekyll::Page paginate_path - the absolute paginate path (from root of FS)
Returns whether the subdirectories are the same relative to source
126 127 128 129 130 131 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 126 def self.in_hierarchy(source, page_dir, paginate_path) return false if paginate_path == File.dirname(paginate_path) return false if paginate_path == Pathname.new(source).parent page_dir == paginate_path || in_hierarchy(source, page_dir, File.dirname(paginate_path)) end |
.paginate_path(site, num_page) ⇒ Object
Static: Return the pagination path of the page
site - the Jekyll::Site object num_page - the pagination page number
Returns the pagination path as a string
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 139 def self.paginate_path(site, num_page) return nil if num_page.nil? return Pagination.first_page_url(site) if num_page <= 1 format = site.config['paginate_path'] if format.include?(":num") format = format.sub(':num', num_page.to_s) else raise ArgumentError.new("Invalid pagination path: '#{format}'. It must include ':num'.") end ensure_leading_slash(format) end |
.pagination_candidate?(config, page) ⇒ Boolean
Static: Determine if a page is a possible candidate to be a template page.
Page's name must be `index.html` and exist in any of the directories
between the site source and `paginate_path`.
config - the site configuration hash page - the Jekyll::Page about which we’re inquiring
Returns true if the
111 112 113 114 115 116 117 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 111 def self.pagination_candidate?(config, page) page_dir = File.dirname(File.(remove_leading_slash(page.path), config['source'])) paginate_path = remove_leading_slash(config['paginate_path']) paginate_path = File.(paginate_path, config['source']) page.name == 'index.html' && in_hierarchy(config['source'], page_dir, File.dirname(paginate_path)) end |
.pagination_enabled?(site) ⇒ Boolean
Determine if pagination is enabled the site.
site - the Jekyll::Site object
Returns true if pagination is enabled, false otherwise.
99 100 101 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 99 def self.pagination_enabled?(site) !site.config['page_size'].nil? && site.pages.size > 0 end |
.remove_leading_slash(path) ⇒ Object
Static: Return a String version of the input without a leading slash.
path - a String path
Returns the input without the leading slash
167 168 169 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 167 def self.remove_leading_slash(path) ensure_leading_slash(path)[1..-1] end |
Instance Method Details
#to_liquid ⇒ Object
Convert this Pager’s data to a Hash suitable for use by Liquid.
Returns the Hash representation of this Pager.
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/fenton-jekyll-plugin/jb_jekyll_paging.rb', line 201 def to_liquid { 'page' => page, 'per_page' => per_page, 'posts' => posts, 'total_posts' => total_posts, 'total_pages' => total_pages, 'previous_page' => previous_page, 'previous_page_path' => previous_page_path, 'next_page' => next_page, 'next_page_path' => next_page_path } end |