lib/jekyll/generators/pagination.rb in jekyll-1.0.0.beta3 vs lib/jekyll/generators/pagination.rb in jekyll-1.0.0.beta4
- old
+ new
@@ -35,28 +35,24 @@
(1..pages).each do |num_page|
pager = Pager.new(site.config, num_page, all_posts, pages)
if num_page > 1
newpage = Page.new(site, site.source, page.dir, page.name)
newpage.pager = pager
- newpage.dir = File.join(page.dir, paginate_path(site, num_page))
+ newpage.dir = File.join(page.dir, Pager.paginate_path(site.config, num_page))
site.pages << newpage
else
page.pager = pager
end
end
end
- private
- def paginate_path(site, num_page)
- format = site.config['paginate_path']
- format.sub(':num', num_page.to_s)
- end
end
end
class Pager
- attr_reader :page, :per_page, :posts, :total_posts, :total_pages, :previous_page, :next_page
+ attr_reader :page, :per_page, :posts, :total_posts, :total_pages,
+ :previous_page, :previous_page_path, :next_page, :next_page_path
# Calculate the number of pages.
#
# all_posts - The Array of all Posts.
# per_page - The Integer of entries per page.
@@ -74,10 +70,22 @@
# Returns true if pagination is enabled, false otherwise.
def self.pagination_enabled?(config, file)
file == 'index.html' && !config['paginate'].nil?
end
+ # Static: Return the pagination path of the page
+ #
+ # site_config - the site config
+ # num_page - the pagination page number
+ #
+ # Returns the pagination path as a string
+ def self.paginate_path(site_config, num_page)
+ return nil if num_page.nil? || num_page <= 1
+ format = site_config['paginate_path']
+ format.sub(':num', num_page.to_s)
+ end
+
# Initialize a new Pager.
#
# config - The Hash configuration of the site.
# page - The Integer page number.
# all_posts - The Array of all the site's Posts.
@@ -96,11 +104,13 @@
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(config, @previous_page)
@next_page = @page != @total_pages ? @page + 1 : nil
+ @next_page_path = Pager.paginate_path(config, @next_page)
end
# Convert this Pager's data to a Hash suitable for use by Liquid.
#
# Returns the Hash representation of this Pager.
@@ -110,10 +120,12 @@
'per_page' => per_page,
'posts' => posts,
'total_posts' => total_posts,
'total_pages' => total_pages,
'previous_page' => previous_page,
- 'next_page' => next_page
+ 'previous_page_path' => previous_page_path,
+ 'next_page' => next_page,
+ 'next_page_path' => next_page_path
}
end
end
end