Class: Jekyll::CategoryPages::Pagination

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll/category_pages.rb

Overview

Custom generator for generating all index pages based on a supplied layout.

Note that this generator uses a layout instead of a regular page template, since it will generate a set of new pages, not merely variations of a single page like the blog index Paginator does.

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object

Generate paginated category pages if necessary.

site - The Site object.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jekyll/category_pages.rb', line 34

def generate(site)
  category_base_path = site.config['category_path'] || 'category'
  category_layout_path = File.join('_layouts/', site.config['category_layout'] || 'category_index.html')

  if Paginate::Pager.pagination_enabled?(site)
    # Generate paginated category pages
    generate_paginated_categories(site, category_base_path, category_layout_path)
  else
    # Generate category pages without pagination
    generate_categories(site, category_base_path, category_layout_path)
  end
end

#generate_categories(site, category_base_path, category_layout) ⇒ Object

Generate the non-paginated category pages.

site - The Site object. category_base_path - String with the base path to the category index pages. category_layout - The name of the basic category layout page.



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/jekyll/category_pages.rb', line 110

def generate_categories(site, category_base_path, category_layout)
  categories = sorted_categories site

  # Generate the pages
  for category in categories
    posts_in_category = site.categories[category]
    category_path = File.join(category_base_path, Utils.slugify(category))

    site.pages << CategoryIndexPage.new(site, category_path, INDEXFILE, category, category_layout, posts_in_category, false)
  end

  Jekyll.logger.debug("Categories", "Processed " + categories.size.to_s + " category index pages")
end

#generate_paginated_categories(site, category_base_path, category_layout) ⇒ Object

Generate the paginated category pages.

site - The Site object. category_base_path - String with the base path to the category index pages. category_layout - The name of the basic category layout page.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jekyll/category_pages.rb', line 66

def generate_paginated_categories(site, category_base_path, category_layout)
  categories = sorted_categories site

  # Generate the pages
  for category in categories
    posts_in_category = site.categories[category]
    category_path = File.join(category_base_path, Utils.slugify(category))
    per_page = site.config['paginate']

    page_number = CategoryPager.calculate_pages(posts_in_category, per_page)
    page_paths = []
    category_pages = []
    (1..page_number).each do |current_page|
      # Collect all paths in the first pass and generate the basic page templates.
      page_name = current_page == 1 ? INDEXFILE : "page#{current_page}.html"
      page_paths.push page_name
      new_page = CategoryIndexPage.new(site, category_path, page_name, category, category_layout, posts_in_category, true)
      category_pages.push new_page
    end

    (1..page_number).each do |current_page|
      # Generate the paginator content in the second pass.
      previous_link = current_page == 1 ? nil : page_paths[current_page - 2]
      next_link = current_page == page_number ? nil : page_paths[current_page]
      previous_page = current_page == 1 ? nil : (current_page - 1)
      next_page = current_page == page_number ? nil : (current_page + 1)
      category_pages[current_page - 1].add_paginator_relations(current_page, per_page, page_number,
                                                               previous_link, next_link, previous_page, next_page)
    end

    for page in category_pages
      # Finally, add the new pages to the site in the third pass.
      site.pages << page
    end
  end

  Jekyll.logger.debug("Paginated categories", "Processed " + categories.size.to_s + " paginated category index pages")
end

#sorted_categories(site) ⇒ Object

Sort the list of categories and remove duplicates.

site - The Site object.

Returns an array of strings containing the site’s categories.



52
53
54
55
56
57
58
59
# File 'lib/jekyll/category_pages.rb', line 52

def sorted_categories(site)
  categories = []
  site.categories.each_pair do |category, pages|
    categories.push(category)
  end
  categories.sort!.uniq!
  return categories
end