Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

pagination is now a generator extension

  • Loading branch information...
commit 18545ddf712fc4515d763d29c11da712f51d5563 1 parent fc86c9d
@krisb krisb authored
View
12 lib/jekyll.rb
@@ -16,28 +16,22 @@
# internal requires
require 'jekyll/core_ext'
-require 'jekyll/pager'
require 'jekyll/site'
require 'jekyll/convertible'
require 'jekyll/layout'
require 'jekyll/page'
require 'jekyll/post'
require 'jekyll/filters'
-require 'jekyll/tags/highlight'
-require 'jekyll/tags/include'
require 'jekyll/albino'
require 'jekyll/static_file'
+Dir[File.dirname(__FILE__) + "/jekyll/tags/*.rb"].each { |f| require f }
#extensions
require 'jekyll/extension'
require 'jekyll/converter'
-Dir["#{File.dirname(__FILE__)}/jekyll/converters/*.rb"].each do |file|
- require file
-end
require 'jekyll/generator'
-Dir["#{File.dirname(__FILE__)}/jekyll/generators/*.rb"].each do |file|
- require file
-end
+Dir[File.dirname(__FILE__) + "/jekyll/converters/*.rb"].each { |f| require f }
+Dir[File.dirname(__FILE__) + "/jekyll/generators/*.rb"].each { |f| require f }
module Jekyll
# Default options. Overriden by values in _config.yml or command-line opts.
View
4 lib/jekyll/extension.rb
@@ -18,6 +18,10 @@ def inherited(base)
subclasses.sort!
end
+ def all
+ subclasses
+ end
+
# priority order of this converter
def priority(priority = nil)
if priority && PRIORITIES.has_key?(priority)
View
79 lib/jekyll/generators/pagination.rb
@@ -2,6 +2,85 @@ module Jekyll
class Pagination < Generator
+ def generate(site)
+ site.pages.dup.each do |page|
+ paginate(site, page) if Pager.pagination_enabled?(site.config, page.name)
+ end
+ end
+
+ # Paginates the blog's posts. Renders the index.html file into paginated
+ # directories, ie: page2/index.html, page3/index.html, etc and adds more
+ # site-wide data.
+ # +page+ is the index.html Page that requires pagination
+ #
+ # {"paginator" => { "page" => <Number>,
+ # "per_page" => <Number>,
+ # "posts" => [<Post>],
+ # "total_posts" => <Number>,
+ # "total_pages" => <Number>,
+ # "previous_page" => <Number>,
+ # "next_page" => <Number> }}
+ def paginate(site, page)
+ all_posts = site.site_payload['site']['posts']
+ pages = Pager.calculate_pages(all_posts, site.config['paginate'].to_i)
+ (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, "page#{num_page}")
+ site.pages << newpage
+ else
+ page.pager = pager
+ end
+ end
+ end
+
+ end
+
+ 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 = num_pages + 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 greater 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_liquid
+ {
+ '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
45 lib/jekyll/pager.rb
@@ -1,45 +0,0 @@
-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 = num_pages + 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 greater 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_liquid
- {
- '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
43 lib/jekyll/site.rb
@@ -39,8 +39,8 @@ def setup
# Check to see if LSI is enabled.
require 'classifier' if self.lsi
- self.converters = Jekyll::Converter.subclasses.collect { |c| c.new(self.config) }
- self.generators = Jekyll::Generator.subclasses.collect { |c| c.new(self.config) }
+ self.converters = Jekyll::Converter.all.collect { |c| c.new(self.config) }
+ self.generators = Jekyll::Generator.all.collect { |c| c.new(self.config) }
end
# Do the actual work of processing the site and generating the
@@ -102,6 +102,12 @@ def read_posts(dir)
self.posts.sort!
end
+ def generate
+ self.generators.each do |generator|
+ generator.generate(self)
+ end
+ end
+
def render
self.posts.each do |post|
post.render(self.layouts, site_payload)
@@ -203,38 +209,5 @@ def filter_entries(entries)
end
end
- def generate
- self.pages.dup.each do |page|
- paginate(page) if Pager.pagination_enabled?(self.config, page.name)
- end
- end
-
- # Paginates the blog's posts. Renders the index.html file into paginated
- # directories, ie: page2/index.html, page3/index.html, etc and adds more
- # site-wide data.
- # +page+ is the index.html Page that requires pagination
- #
- # {"paginator" => { "page" => <Number>,
- # "per_page" => <Number>,
- # "posts" => [<Post>],
- # "total_posts" => <Number>,
- # "total_pages" => <Number>,
- # "previous_page" => <Number>,
- # "next_page" => <Number> }}
- def paginate(page)
- all_posts = site_payload['site']['posts']
- 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)
- if num_page > 1
- newpage = Page.new(self, self.source, page.dir, page.name)
- newpage.pager = pager
- newpage.dir = File.join(page.dir, "page#{num_page}")
- self.pages << newpage
- else
- page.pager = pager
- end
- end
- end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.