Module: Octopress::Paginate

Extended by:
Paginate
Included in:
Paginate
Defined in:
lib/octopress-paginate.rb,
lib/octopress-paginate/hooks.rb,
lib/octopress-paginate/version.rb

Defined Under Namespace

Classes: PageHook, PaginationPage, SiteHook

Constant Summary collapse

DEFAULT =
{
  'collection'   => 'posts',
  'per_page'     => 10,
  'limit'        => 5,
  'permalink'    => '/page:num/',
  'title_suffix' => ' - page :num',
  'page_num'     => 1
}
LOOP =
/(paginate.+\s+in)\s+(site\.(.+?))(.+)%}/
VERSION =
"1.1.2"

Instance Method Summary collapse

Instance Method Details

#add_pages(page) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
# File 'lib/octopress-paginate.rb', line 52

def add_pages(page)
  config = page.data['paginate']
  pages = (collection(page).size.to_f / config['per_page']).ceil - 1

  if config['limit']
    pages = [pages, config['limit'] - 1].min
  end

  page.data['paginate']['pages'] = pages + 1

  new_pages = []

  pages.times do |i|
    index = i+2

    # If page is generated by an Octopress Ink plugin, use the built in 
    # methods for cloning the page
    #
    if page.respond_to?(:asset) && page.asset.to_s.match('Octopress::Ink')
      new_page = page.asset.new_page(page_data(page, index))
    else
      new_page = PaginationPage.new(page.site, page.site.source, File.dirname(page.path), File.basename(page.path))
      new_page.data.merge!(page_data(page, index))
      new_page.process('index.html')
    end

    new_pages << new_page
  end

  all_pages = [page].concat(new_pages)

  all_pages.each_with_index do |p, index|

    if index > 0
      prev_page = all_pages[index - 1]
      p.data['paginate']['previous_page'] = index
      p.data['paginate']['previous_page_path'] = prev_page.url
    end

    if next_page = all_pages[index + 1]
      p.data['paginate']['next_page'] = index + 2
      p.data['paginate']['next_page_path'] = next_page.url
    end
  end

  page.site.pages.concat new_pages
end

#collection(page) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/octopress-paginate.rb', line 131

def collection(page)
  collection = if page['paginate']['collection'] == 'posts'
    if defined?(Octopress::Multilingual) && page.lang
      page.site.posts_by_language(page.lang)
    else
      page.site.posts.reverse
    end
  else
    page.site.collections[page['paginate']['collection']].docs
  end

  if categories = page.data['paginate']['categories']
    collection = collection.reject{|p| (p.categories & categories).empty?}
  end

  if tags = page.data['paginate']['tags']
    collection = collection.reject{|p| (p.tags & tags).empty?}
  end

  collection
end

#items(payload, collection) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/octopress-paginate.rb', line 170

def items(payload, collection)
  config = payload['page']['paginate']

  n = (config['page_num'] - 1) * config['per_page']
  max = n + (config['per_page'] - 1)

  collection[n..max]
end

#page_data(page, index) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/octopress-paginate.rb', line 100

def page_data(page, index)
  {
    'paginate'  => paginate_data(page, index),
    'permalink' => page_permalink(page, index),
    'title'     => page_title(page, index),
  }
end

#page_payload(payload, page) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/octopress-paginate.rb', line 153

def page_payload(payload, page)
  config = page.data['paginate']
  collection = collection(page)
  { 'paginator' => {
    "#{config['collection']}"       => items(payload, collection),
    "page"                          => config['page_num'],
    "per_page"                      => config['per_page'],
    "limit"                         => config['limit'],
    "total_#{config['collection']}" => collection.size,
    "total_pages"                   => config['pages'],
    'previous_page'                 => config['previous_page'],
    'previous_page_path'            => config['previous_page_path'],
    'next_page'                     => config['next_page'],
    'next_page_path'                => config['next_page_path']
  }}
end


108
109
110
111
# File 'lib/octopress-paginate.rb', line 108

def page_permalink(page, index)
  subdir = page.data['paginate']['permalink'].clone.sub(':num', index.to_s)
  File.join(page.dir, subdir)
end

#page_title(page, index) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/octopress-paginate.rb', line 119

def page_title(page, index)
  title = if page.data['title']
    page.data['title']
  else 
    page.data['paginate']['collection'].capitalize
  end

  title += page.data['paginate']['title_suffix'].sub(/:num/, index.to_s)

  title
end

#paginate(page) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/octopress-paginate.rb', line 31

def paginate(page)

  defaults = DEFAULT.merge(page.site.config['pagination'] || {})

  if page.data['paginate'].is_a? Hash
    page.data['paginate'] = defaults.merge(page.data['paginate'])
  else
    page.data['paginate'] = defaults
  end

  if tag = page.data['paginate']['tag']
    page.data['paginate']['tags'] = Array(tag)
  end

  if category = page.data['paginate']['category']
    page.data['paginate']['categories'] = Array(category)
  end

  add_pages(page)
end

#paginate_data(page, index) ⇒ Object



113
114
115
116
117
# File 'lib/octopress-paginate.rb', line 113

def paginate_data(page, index)
  paginate_data = page.data['paginate'].clone
  paginate_data['page_num'] = index
  paginate_data
end