Skip to content

Commit

Permalink
posts' pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
calavera committed Apr 4, 2009
1 parent 5561317 commit 808d6c6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
31 changes: 31 additions & 0 deletions README.textile
Expand Up @@ -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/.

$ 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

Expand Down Expand Up @@ -279,6 +287,29 @@ h3. Post

post.content
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

Expand Down
7 changes: 5 additions & 2 deletions features/step_definitions/jekyll_steps.rb
Expand Up @@ -100,6 +100,10 @@
assert File.directory?(dir)
end

Then /^the (.*) file should exist$/ do |file|
assert File.file?(file)
end

Then /^I should see "(.*)" in "(.*)"$/ do |text, file|
assert_match Regexp.new(text), File.open(file).readlines.join
end
Expand All @@ -114,5 +118,4 @@

Then /^I should see today's date in "(.*)"$/ do |file|
assert_match Regexp.new(Date.today.to_s), File.open(file).readlines.join
end

end
1 change: 1 addition & 0 deletions lib/jekyll.rb
Expand Up @@ -16,6 +16,7 @@

# internal requires
require 'jekyll/core_ext'
require 'jekyll/pager'
require 'jekyll/site'
require 'jekyll/convertible'
require 'jekyll/layout'
Expand Down
11 changes: 7 additions & 4 deletions lib/jekyll/page.rb
Expand Up @@ -46,18 +46,21 @@ def render(layouts, site_payload)
end

# 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
def write(dest)
FileUtils.mkdir_p(File.join(dest, @dir))
def write(dest_prefix, dest_suffix = nil)
dest = File.join(dest_prefix, @dir)
dest = File.join(dest, dest_suffix) if dest_suffix
FileUtils.mkdir_p(dest)

name = @name
if self.ext != ""
name = @name.split(".")[0..-2].join('.') + self.ext
end

path = File.join(dest, @dir, name)
path = File.join(dest, name)
File.open(path, 'w') do |f|
f.write(self.output)
end
Expand Down
28 changes: 28 additions & 0 deletions lib/jekyll/site.rb
Expand Up @@ -167,11 +167,14 @@ def transform_pages(dir = '')
directories.delete('_posts')
read_posts(dir)
end

[directories, files].each do |entries|
entries.each do |f|
if File.directory?(File.join(base, f))
next if self.dest.sub(/\/$/, '') == File.join(base, f)
transform_pages(File.join(dir, f))
elsif Pager.pagination_enabled?(self.config, f)
paginate_posts(f, dir)
else
first3 = File.open(File.join(self.source, dir, f)) { |fd| fd.read(3) }

Expand Down Expand Up @@ -229,6 +232,31 @@ def filter_entries(entries)
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

0 comments on commit 808d6c6

Please sign in to comment.