posts' pagination

This commit is contained in:
David Calavera 2009-04-04 12:43:46 +02:00
parent 556131793f
commit 808d6c6a62
5 changed files with 72 additions and 6 deletions

View File

@ -173,6 +173,14 @@ date parts and post name will be made and an index.html will be placed in the
leaf directory resulting in URLs like 2008/11/17/blogging-like-a-hacker/. leaf directory resulting in URLs like 2008/11/17/blogging-like-a-hacker/.
$ jekyll --permalink [date|none|pretty] $ jekyll --permalink [date|none|pretty]
h3. Posts' pagination
There is also an option to paginate a blog's post. With this option enabled,
Jekyll takes the index.html file in your base directory and compiles it within
several directories called "page2", "page3" .. "pageN".
$ jekyll --paginate [POSTS_PER_PAGE]
h2. Configuration File h2. Configuration File
@ -279,6 +287,29 @@ h3. Post
post.content post.content
The content of the Post. The content of the Post.
h3. Paginator
paginator.page
The number of the current page.
paginator.per_page
Number of posts per page.
paginator.posts
Posts available for that page.
paginator.total_posts
Total number of posts.
paginator.total_pages
Total number of pages.
paginator.previous_page
The number of the previous page.
paginator.next_page
The number of the next page.
h2. YAML Front Matter h2. YAML Front Matter

View File

@ -100,6 +100,10 @@ Then /^the (.*) directory should exist$/ do |dir|
assert File.directory?(dir) assert File.directory?(dir)
end end
Then /^the (.*) file should exist$/ do |file|
assert File.file?(file)
end
Then /^I should see "(.*)" in "(.*)"$/ do |text, file| Then /^I should see "(.*)" in "(.*)"$/ do |text, file|
assert_match Regexp.new(text), File.open(file).readlines.join assert_match Regexp.new(text), File.open(file).readlines.join
end end
@ -114,5 +118,4 @@ end
Then /^I should see today's date in "(.*)"$/ do |file| Then /^I should see today's date in "(.*)"$/ do |file|
assert_match Regexp.new(Date.today.to_s), File.open(file).readlines.join assert_match Regexp.new(Date.today.to_s), File.open(file).readlines.join
end end

View File

@ -16,6 +16,7 @@ require 'redcloth'
# internal requires # internal requires
require 'jekyll/core_ext' require 'jekyll/core_ext'
require 'jekyll/pager'
require 'jekyll/site' require 'jekyll/site'
require 'jekyll/convertible' require 'jekyll/convertible'
require 'jekyll/layout' require 'jekyll/layout'

View File

@ -46,18 +46,21 @@ module Jekyll
end end
# Write the generated page file to the destination directory. # Write the generated page file to the destination directory.
# +dest+ is the String path to the destination dir # +dest_prefix+ is the String path to the destination dir
# +dest_suffix+ is a suffix path to the destination dir
# #
# Returns nothing # Returns nothing
def write(dest) def write(dest_prefix, dest_suffix = nil)
FileUtils.mkdir_p(File.join(dest, @dir)) dest = File.join(dest_prefix, @dir)
dest = File.join(dest, dest_suffix) if dest_suffix
FileUtils.mkdir_p(dest)
name = @name name = @name
if self.ext != "" if self.ext != ""
name = @name.split(".")[0..-2].join('.') + self.ext name = @name.split(".")[0..-2].join('.') + self.ext
end end
path = File.join(dest, @dir, name) path = File.join(dest, name)
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f.write(self.output) f.write(self.output)
end end

View File

@ -167,11 +167,14 @@ module Jekyll
directories.delete('_posts') directories.delete('_posts')
read_posts(dir) read_posts(dir)
end end
[directories, files].each do |entries| [directories, files].each do |entries|
entries.each do |f| entries.each do |f|
if File.directory?(File.join(base, f)) if File.directory?(File.join(base, f))
next if self.dest.sub(/\/$/, '') == File.join(base, f) next if self.dest.sub(/\/$/, '') == File.join(base, f)
transform_pages(File.join(dir, f)) transform_pages(File.join(dir, f))
elsif Pager.pagination_enabled?(self.config, f)
paginate_posts(f, dir)
else else
first3 = File.open(File.join(self.source, dir, f)) { |fd| fd.read(3) } first3 = File.open(File.join(self.source, dir, f)) { |fd| fd.read(3) }
@ -229,6 +232,31 @@ module Jekyll
end end
end end
end end
# Paginates the blog's posts. Renders the index.html file into paginated directories, ie: page2, page3...
# and adds more wite-wide data
#
# {"paginator" => { "page" => <Number>,
# "per_page" => <Number>,
# "posts" => [<Post>],
# "total_posts" => <Number>,
# "total_pages" => <Number>,
# "previous_page" => <Number>,
# "next_page" => <Number> }}
def paginate_posts(file, dir)
all_posts = self.posts.sort { |a,b| b <=> a }
page = Page.new(self, self.source, dir, file)
pages = Pager.calculate_pages(all_posts, self.config['paginate'].to_i)
(1..pages).each do |num_page|
pager = Pager.new(self.config, num_page, all_posts, pages)
page.render(self.layouts, site_payload.merge({'paginator' => pager.to_hash}))
suffix = "page#{num_page}" if num_page > 1
page.write(self.dest, suffix)
end
end
end end
end end