Skip to content

Commit

Permalink
Allow to exclude certain categories from paginated posts #6 - no test
Browse files Browse the repository at this point in the history
  • Loading branch information
djacquel committed Jan 20, 2015
1 parent 52262e7 commit c99bd44
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/jekyll-paginate/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ def generate(site)
# "next_page" => <Number> }}
def paginate(site, page)
all_posts = site.site_payload['site']['posts'].reject { |post| post['hidden'] }

# can be moved to Jekyll:Configuration:fix_common_issues
if !site.config['not_paginated_categories'].nil? && !site.config['not_paginated_categories'].is_a?(Array)
Jekyll.logger.warn "Config Warning:", "The `not_paginated_categories` key must be an array" +
" It's currently set to '#{config['not_paginated_categories'].inspect}'."
config['not_paginated_categories'] = nil
end

if !site.config['not_paginated_categories'].nil?
all_posts = filter(site, all_posts)
end

pages = Pager.calculate_pages(all_posts, site.config['paginate'].to_i)
(1..pages).each do |num_page|
pager = Pager.new(site, num_page, all_posts, pages)
Expand Down Expand Up @@ -80,6 +92,31 @@ def self.template_page(site)
end.first
end

# Public: Filter posts list depending on categories
#
# site - the Jekyll::Site object
# posts - array of site.posts
#
# Returns array of filtered posts that are not in a **not_paginated_categories**
def filter(site, posts)
posts.reject{ |post| excluded?(site, post) }
end

# Public: Vote if a post is from an excluded category
#
# site - the Jekyll::Site object
# post - a Jekyll::Post object
#
# Returns bolean true if excluded - false if not
def excluded?(site, post)
post.categories.each do |c|
if site.config['not_paginated_categories'].index(c)
return true
end
end
return false
end

end
end
end

1 comment on commit c99bd44

@djacquel
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This filtering only occurs when generator find both parameters :

paginate: n
not_paginated_categories:
  - 'toto'

Please sign in to comment.