posts' pagination
This commit is contained in:
parent
556131793f
commit
808d6c6a62
|
@ -173,6 +173,14 @@ date parts and post name will be made and an index.html will be placed in the
|
|||
leaf directory resulting in URLs like 2008/11/17/blogging-like-a-hacker/.
|
||||
|
||||
$ jekyll --permalink [date|none|pretty]
|
||||
|
||||
h3. Posts' pagination
|
||||
|
||||
There is also an option to paginate a blog's post. With this option enabled,
|
||||
Jekyll takes the index.html file in your base directory and compiles it within
|
||||
several directories called "page2", "page3" .. "pageN".
|
||||
|
||||
$ jekyll --paginate [POSTS_PER_PAGE]
|
||||
|
||||
h2. Configuration File
|
||||
|
||||
|
@ -279,6 +287,29 @@ h3. Post
|
|||
|
||||
post.content
|
||||
The content of the Post.
|
||||
|
||||
h3. Paginator
|
||||
|
||||
paginator.page
|
||||
The number of the current page.
|
||||
|
||||
paginator.per_page
|
||||
Number of posts per page.
|
||||
|
||||
paginator.posts
|
||||
Posts available for that page.
|
||||
|
||||
paginator.total_posts
|
||||
Total number of posts.
|
||||
|
||||
paginator.total_pages
|
||||
Total number of pages.
|
||||
|
||||
paginator.previous_page
|
||||
The number of the previous page.
|
||||
|
||||
paginator.next_page
|
||||
The number of the next page.
|
||||
|
||||
h2. YAML Front Matter
|
||||
|
||||
|
|
|
@ -100,6 +100,10 @@ Then /^the (.*) directory should exist$/ do |dir|
|
|||
assert File.directory?(dir)
|
||||
end
|
||||
|
||||
Then /^the (.*) file should exist$/ do |file|
|
||||
assert File.file?(file)
|
||||
end
|
||||
|
||||
Then /^I should see "(.*)" in "(.*)"$/ do |text, file|
|
||||
assert_match Regexp.new(text), File.open(file).readlines.join
|
||||
end
|
||||
|
@ -114,5 +118,4 @@ end
|
|||
|
||||
Then /^I should see today's date in "(.*)"$/ do |file|
|
||||
assert_match Regexp.new(Date.today.to_s), File.open(file).readlines.join
|
||||
end
|
||||
|
||||
end
|
|
@ -16,6 +16,7 @@ require 'redcloth'
|
|||
|
||||
# internal requires
|
||||
require 'jekyll/core_ext'
|
||||
require 'jekyll/pager'
|
||||
require 'jekyll/site'
|
||||
require 'jekyll/convertible'
|
||||
require 'jekyll/layout'
|
||||
|
|
|
@ -46,18 +46,21 @@ module Jekyll
|
|||
end
|
||||
|
||||
# Write the generated page file to the destination directory.
|
||||
# +dest+ is the String path to the destination dir
|
||||
# +dest_prefix+ is the String path to the destination dir
|
||||
# +dest_suffix+ is a suffix path to the destination dir
|
||||
#
|
||||
# Returns nothing
|
||||
def write(dest)
|
||||
FileUtils.mkdir_p(File.join(dest, @dir))
|
||||
def write(dest_prefix, dest_suffix = nil)
|
||||
dest = File.join(dest_prefix, @dir)
|
||||
dest = File.join(dest, dest_suffix) if dest_suffix
|
||||
FileUtils.mkdir_p(dest)
|
||||
|
||||
name = @name
|
||||
if self.ext != ""
|
||||
name = @name.split(".")[0..-2].join('.') + self.ext
|
||||
end
|
||||
|
||||
path = File.join(dest, @dir, name)
|
||||
path = File.join(dest, name)
|
||||
File.open(path, 'w') do |f|
|
||||
f.write(self.output)
|
||||
end
|
||||
|
|
|
@ -167,11 +167,14 @@ module Jekyll
|
|||
directories.delete('_posts')
|
||||
read_posts(dir)
|
||||
end
|
||||
|
||||
[directories, files].each do |entries|
|
||||
entries.each do |f|
|
||||
if File.directory?(File.join(base, f))
|
||||
next if self.dest.sub(/\/$/, '') == File.join(base, f)
|
||||
transform_pages(File.join(dir, f))
|
||||
elsif Pager.pagination_enabled?(self.config, f)
|
||||
paginate_posts(f, dir)
|
||||
else
|
||||
first3 = File.open(File.join(self.source, dir, f)) { |fd| fd.read(3) }
|
||||
|
||||
|
@ -229,6 +232,31 @@ module Jekyll
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Paginates the blog's posts. Renders the index.html file into paginated directories, ie: page2, page3...
|
||||
# and adds more wite-wide data
|
||||
#
|
||||
# {"paginator" => { "page" => <Number>,
|
||||
# "per_page" => <Number>,
|
||||
# "posts" => [<Post>],
|
||||
# "total_posts" => <Number>,
|
||||
# "total_pages" => <Number>,
|
||||
# "previous_page" => <Number>,
|
||||
# "next_page" => <Number> }}
|
||||
def paginate_posts(file, dir)
|
||||
all_posts = self.posts.sort { |a,b| b <=> a }
|
||||
page = Page.new(self, self.source, dir, file)
|
||||
|
||||
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)
|
||||
|
||||
page.render(self.layouts, site_payload.merge({'paginator' => pager.to_hash}))
|
||||
suffix = "page#{num_page}" if num_page > 1
|
||||
page.write(self.dest, suffix)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue