Class: Middleman::Blog::Paginator

Inherits:
Object
  • Object
show all
Includes:
UriTemplates
Defined in:
lib/middleman-blog/paginator.rb

Overview

A sitemap plugin that splits indexes (including tag and calendar indexes) over multiple pages

Instance Method Summary collapse

Methods included from UriTemplates

apply_uri_template, date_to_params, extract_directory_path, extract_params, safe_parameterize, uri_template

Constructor Details

#initialize(app, blog_controller) ⇒ Paginator

Returns a new instance of Paginator



10
11
12
13
14
15
# File 'lib/middleman-blog/paginator.rb', line 10

def initialize( app, blog_controller )
  @app             = app
  @blog_controller = blog_controller
  @per_page        = blog_controller.options.per_page
  @page_link       = blog_controller.options.page_link
end

Instance Method Details

#manipulate_resource_list(resources)

This method returns an undefined value.

Update the main sitemap resource list



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/middleman-blog/paginator.rb', line 19

def manipulate_resource_list(resources)
  new_resources = []

  resources.each do |res|
    next if res.ignored?

    # Avoid recomputing metadata over and over
    md = res.

    next unless md[:page][:pageable]

    # Skip other blogs' resources
    next unless match_blog(res, md)

    # "articles" local variable is populated by Calendar and Tag page generators
    # If it's not set then use the complete list of articles
    # TODO: Some way to allow the frontmatter to specify the article filter?
    articles = md[:locals]["articles"] || @blog_controller.data.articles
    articles.select!{|article| article.lang == md[:options][:locale]} if md.fetch(:options, false) and md[:options].fetch(:locale, false)

    # Allow blog.per_page and blog.page_link to be overridden in the frontmatter
    per_page  = md[:page][:per_page] || @per_page
    page_link = uri_template(md[:page][:page_link] || @page_link)

    num_pages = (articles.length / per_page.to_f).ceil

    # Add the pagination metadata to the base page (page 1)
    res. locals: page_locals(1, num_pages, per_page, nil, articles)

    prev_page_res = res

    # Create additional resources for the 2nd and subsequent pages.
    2.upto(num_pages) do |page_num|
      p = page_resource(res, page_num, page_link)

      # Copy the metadata from the base page
      p. md
      p. locals: page_locals(page_num, num_pages, per_page, prev_page_res, articles)

      # Add a reference in the previous page to this page
      prev_page_res. locals: { 'next_page' => p }

      prev_page_res = p

      new_resources << p
    end
  end

  resources + new_resources
end