Merge pull request #942 from mojombo/pagination-paths

Add paginator.previous_page_path and paginator.next_page_path
This commit is contained in:
Parker Moore 2013-04-09 12:24:30 -07:00
commit b7646cc9ca
2 changed files with 94 additions and 10 deletions

View File

@ -37,7 +37,7 @@ module Jekyll
if num_page > 1 if num_page > 1
newpage = Page.new(site, site.source, page.dir, page.name) newpage = Page.new(site, site.source, page.dir, page.name)
newpage.pager = pager 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 site.pages << newpage
else else
page.pager = pager page.pager = pager
@ -45,16 +45,12 @@ module Jekyll
end 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
end end
class Pager 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. # Calculate the number of pages.
# #
@ -76,6 +72,18 @@ module Jekyll
file == 'index.html' && !config['paginate'].nil? file == 'index.html' && !config['paginate'].nil?
end 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. # Initialize a new Pager.
# #
# config - The Hash configuration of the site. # config - The Hash configuration of the site.
@ -98,7 +106,9 @@ module Jekyll
@total_posts = all_posts.size @total_posts = all_posts.size
@posts = all_posts[init..offset] @posts = all_posts[init..offset]
@previous_page = @page != 1 ? @page - 1 : nil @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 = @page != @total_pages ? @page + 1 : nil
@next_page_path = Pager.paginate_path(config, @next_page)
end end
# Convert this Pager's data to a Hash suitable for use by Liquid. # Convert this Pager's data to a Hash suitable for use by Liquid.
@ -112,7 +122,9 @@ module Jekyll
'total_posts' => total_posts, 'total_posts' => total_posts,
'total_pages' => total_pages, 'total_pages' => total_pages,
'previous_page' => previous_page, '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 end

View File

@ -31,6 +31,78 @@ paginate: 5
The number should be the maximum number of Posts youd like to be displayed per- The number should be the maximum number of Posts youd like to be displayed per-
page in the generated site. page in the generated site.
## Liquid Attributes Available
The pagination plugin exposes the `paginator` liquid object with the following
attributes:
<table>
<thead>
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><p><code>page</code></p></td>
<td><p>current page number</p></td>
</tr>
<tr>
<td><p><code>per_page</code></p></td>
<td><p>number of posts per page</p></td>
</tr>
<tr>
<td><p><code>posts</code></p></td>
<td><p>a list of posts for the current page</p></td>
</tr>
<tr>
<td><p><code>total_posts</code></p></td>
<td><p>total number of posts in the site</p></td>
</tr>
<tr>
<td><p><code>total_pages</code></p></td>
<td><p>number of pagination pages</p></td>
</tr>
<tr>
<td><p><code>previous_page</code></p></td>
<td>
<p>
page number of the previous pagination page,
or <code>nil</code> if no previous page exists
</p>
</td>
</tr>
<tr>
<td><p><code>previous_page_path</code></p></td>
<td>
<p>
path of previous pagination page,
or <code>nil</code> if no previous page exists
</p>
</td>
</tr>
<tr>
<td><p><code>next_page</code></p></td>
<td>
<p>
page number of the next pagination page,
or <code>nil</code> if no subsequent page exists
</p>
</td>
</tr>
<tr>
<td><p><code>next_page_path</code></p></td>
<td>
<p>
path of next pagination page,
or <code>nil</code> if no subsequent page exists
</p>
</td>
</tr>
</tbody>
</table>
<div class="note info"> <div class="note info">
<h5>Pagination does not support tags or categories</h5> <h5>Pagination does not support tags or categories</h5>
<p>Pagination pages through every post in the <code>posts</code> variable regardless of variables defined in the YAML Front Matter of each. It does not currently allow paging over groups of posts linked by a common tag or category.</p> <p>Pagination pages through every post in the <code>posts</code> variable regardless of variables defined in the YAML Front Matter of each. It does not currently allow paging over groups of posts linked by a common tag or category.</p>
@ -98,7 +170,7 @@ page with links to all but the current page.
{% if paginator.previous_page == 1 %} {% if paginator.previous_page == 1 %}
<a href="/">Previous</a> <a href="/">Previous</a>
{% else %} {% else %}
<a href="/page{{ paginator.previous_page }}">Previous</a> <a href="{{ paginator.previous_page_path }}">Previous</a>
{% endif %} {% endif %}
</p> </p>
{% else %} {% else %}
@ -129,7 +201,7 @@ page with links to all but the current page.
{% if paginator.next_page %} {% if paginator.next_page %}
<p class="next"> <p class="next">
<a href="/page{{ paginator.next_page }}">Next</a> <a href="{{ paginator.next_page_path }}">Next</a>
</p> </p>
{% else %} {% else %}
<p class="next disabled"> <p class="next disabled">