Permalink
Browse files

Merging in calavera's pagination branch

  • Loading branch information...
2 parents 8a11c0e + 6eb29a4 commit e1dbda47edb00ae815bc8d942ece0d5684ef8088 @qrush qrush committed May 9, 2009
View
@@ -51,6 +51,16 @@ opts = OptionParser.new do |opts|
opts.on("--permalink [TYPE]", "Use 'date' (default) for YYYY/MM/DD") do |style|
options['permalink'] = style unless style.nil?
end
+
+ opts.on("--paginate [POSTS_PER_PAGE]", "Paginate a blog's posts") do |per_page|
+ begin
+ options['paginate'] = per_page.to_i
+ raise ArgumentError if options['paginate'] == 0
+ rescue
+ puts 'you must specify a number of posts by page bigger than 0'
+ exit 0
+ end
+ end
opts.on("--version", "Display current version") do
puts "Jekyll " + Jekyll.version
@@ -0,0 +1,22 @@
+Feature: Site pagination
+ In order to paginate my blog
+ As a blog's user
+ I want divide the posts in several pages
+
+ Scenario: Create pages
+ Given I have a configuration file with "paginate" set to "1"
+ And I have a _layouts directory
+ And I have an "index.html" file that contains "Basic Site"
+ And I have a _posts directory
+ And I have the following post:
+ | title | date | layout | content |
+ | Wargames | 3/27/2009 | default | The only winning move is not to play. |
+ | Wargames2 | 4/27/2009 | default | The only winning move is not to play2. |
+ When I run jekyll
+ Then the _site/page2 directory should exist
+ And the _site/page2/index.html file should exist
+
+
+
+
+
@@ -10,7 +10,7 @@ Feature: Site data
And I should see "Contact: email@me.com" in "_site/contact.html"
Scenario: Use site.time variable
- Given I have an "index.html" page that contains "Generated on: {{ site.time }}"
+ Given I have an "index.html" page that contains "{{ site.time }}"
When I run jekyll
Then the _site directory should exist
And I should see today's time in "_site/index.html"
@@ -115,6 +115,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
@@ -130,4 +134,3 @@
Then /^I should see today's date in "(.*)"$/ do |file|
assert_match Regexp.new(Date.today.to_s), File.open(file).readlines.join
end
-
View
@@ -16,6 +16,7 @@
# internal requires
require 'jekyll/core_ext'
+require 'jekyll/pager'
require 'jekyll/site'
require 'jekyll/convertible'
require 'jekyll/layout'
View
@@ -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
View
@@ -0,0 +1,45 @@
+module Jekyll
+ class Pager
+ attr_reader :page, :per_page, :posts, :total_posts, :total_pages, :previous_page, :next_page
+
+ def self.calculate_pages(all_posts, per_page)
+ num_pages = all_posts.size / per_page.to_i
+ num_pages.abs + 1 if all_posts.size % per_page.to_i != 0
+ num_pages
+ end
+
+ def self.pagination_enabled?(config, file)
+ file == 'index.html' && !config['paginate'].nil?
+ end
+
+ def initialize(config, page, all_posts, num_pages = nil)
+ @page = page
+ @per_page = config['paginate'].to_i
+ @total_pages = num_pages || Pager.calculate_pages(all_posts, @per_page)
+
+ if @page > @total_pages
+ raise RuntimeError, "page number can't be grater 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
+ @next_page = @page != @total_pages ? @page + 1 : nil
+ end
+
+ def to_hash
+ {
+ 'page' => page,
+ 'per_page' => per_page,
+ 'posts' => posts,
+ 'total_posts' => total_posts,
+ 'total_pages' => total_pages,
+ 'previous_page' => previous_page,
+ 'next_page' => next_page
+ }
+ end
+ end
+end
View
@@ -172,11 +172,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) }
@@ -233,5 +236,30 @@ 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
View
@@ -0,0 +1,47 @@
+require File.dirname(__FILE__) + '/helper'
+
+class TestPager < Test::Unit::TestCase
+
+ def setup
+ stub(Jekyll).configuration do
+ Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir,
+ 'paginate' => 2})
+ end
+
+ @config = Jekyll.configuration
+ @site = Site.new(@config)
+ @posts = @site.read_posts('')
+ end
+
+ def teardown
+ @config = Jekyll.configuration('paginate' => nil)
+ end
+
+ def test_calculate_pages
+ assert_equal(2, Pager.calculate_pages(@posts, @config['paginate']))
+ end
+
+ def test_create_first_pager
+ pager = Pager.new(@config, 1, @posts)
+ assert_equal(@config['paginate'].to_i, pager.posts.size)
+ assert_equal(2, pager.total_pages)
+ assert_nil(pager.previous_page)
+ assert_equal(2, pager.next_page)
+ end
+
+ def test_create_second_pager
+ pager = Pager.new(@config, 2, @posts)
+ assert_equal(@posts.size - @config['paginate'].to_i, pager.posts.size)
+ assert_equal(2, pager.total_pages)
+ assert_equal(1, pager.previous_page)
+ assert_nil(pager.next_page)
+ end
+
+ def test_create_third_pager
+ assert_raise(RuntimeError) { Pager.new(@config, 3, @posts) }
+ end
+
+ def test_pagination_enabled_with_command_option
+ assert_equal(true, Pager.pagination_enabled?(@config, 'index.html'))
+ end
+end

0 comments on commit e1dbda4

Please sign in to comment.