├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .rspec ├── .travis.yml ├── Gemfile ├── History.markdown ├── LICENSE.txt ├── README.md ├── Rakefile ├── jekyll-paginate.gemspec ├── lib ├── jekyll-paginate.rb └── jekyll-paginate │ ├── pager.rb │ ├── pagination.rb │ └── version.rb ├── script ├── bootstrap └── cibuild └── spec ├── pager_spec.rb ├── pagination_spec.rb ├── source ├── _posts │ ├── 2014-05-20-blah.html │ ├── 2014-05-21-bleh.html │ ├── 2014-05-22-humor.html │ ├── 2014-05-23-hey-there.html │ ├── 2014-05-24-whateva.html │ └── 2014-05-25-oh-yes.html ├── contacts │ └── index.html └── index.html └── spec_helper.rb /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Continuous Integration 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | 11 | jobs: 12 | j4: 13 | if: "!contains(github.event.commits[0].message, '[ci skip]')" 14 | name: "Jekyll ${{ matrix.jekyll_version }} (Ruby ${{ matrix.ruby_version }})" 15 | runs-on: 'ubuntu-latest' 16 | env: 17 | JEKYLL_VERSION: ${{ matrix.jekyll_version }} 18 | strategy: 19 | fail-fast: false 20 | matrix: 21 | ruby_version: 22 | - 2.5 23 | - 2.7 24 | - 3.0 25 | jekyll_version: 26 | - "~> 3.0" 27 | - "~> 4.0" 28 | steps: 29 | - uses: actions/checkout@v2 30 | with: 31 | fetch-depth: 5 32 | - name: "Set up Ruby ${{ matrix.ruby_version }}" 33 | uses: ruby/setup-ruby@v1 34 | with: 35 | ruby-version: ${{ matrix.ruby_version }} 36 | bundler-cache: true 37 | - name: Execute tests 38 | run: bundle exec rspec 39 | j3: 40 | if: "!contains(github.event.commits[0].message, '[ci skip]')" 41 | name: "Jekyll ${{ matrix.jekyll_version }} (Ruby ${{ matrix.ruby_version }})" 42 | runs-on: 'ubuntu-latest' 43 | env: 44 | JEKYLL_VERSION: ${{ matrix.jekyll_version }} 45 | strategy: 46 | fail-fast: false 47 | matrix: 48 | ruby_version: 49 | - 2.5 50 | jekyll_version: 51 | - "~> 3.9" 52 | steps: 53 | - uses: actions/checkout@v2 54 | with: 55 | fetch-depth: 5 56 | - name: "Set up Ruby ${{ matrix.ruby_version }}" 57 | uses: ruby/setup-ruby@v1 58 | with: 59 | ruby-version: ${{ matrix.ruby_version }} 60 | bundler-cache: true 61 | - name: Execute tests 62 | run: 'bundle exec rspec && true' 63 | 64 | style_check: 65 | if: "!contains(github.event.commits[0].message, '[ci skip]')" 66 | name: "Code Style Check (Ruby ${{ matrix.ruby_version }})" 67 | runs-on: 'ubuntu-latest' 68 | strategy: 69 | fail-fast: false 70 | matrix: 71 | ruby_version: 72 | - 2.5 73 | steps: 74 | - uses: actions/checkout@v2 75 | with: 76 | fetch-depth: 5 77 | - name: "Set up Ruby ${{ matrix.ruby_version }}" 78 | uses: ruby/setup-ruby@v1 79 | with: 80 | ruby-version: ${{ matrix.ruby_version }} 81 | bundler-cache: true 82 | - name: Check Style Offenses 83 | run: bundle exec rubocop -S -D 84 | 85 | gem_build: 86 | if: "!contains(github.event.commits[0].message, '[ci skip]')" 87 | name: "Test Gem build (Ruby ${{ matrix.ruby_version }})" 88 | runs-on: 'ubuntu-latest' 89 | strategy: 90 | fail-fast: false 91 | matrix: 92 | ruby_version: 93 | - 2.5 94 | steps: 95 | - uses: actions/checkout@v2 96 | with: 97 | fetch-depth: 5 98 | - name: "Set up Ruby ${{ matrix.ruby_version }}" 99 | uses: ruby/setup-ruby@v1 100 | with: 101 | ruby-version: ${{ matrix.ruby_version }} 102 | bundler-cache: true 103 | - name: Test Gem build 104 | run: bundle exec gem build jekyll-paginate.gemspec 105 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | spec/dest 16 | tmp 17 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --format progress 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | cache: bundler 3 | sudo: false 4 | rvm: 5 | - 2.2 6 | - 2.1 7 | - 2.0 8 | - 1.9.3 9 | before_script: bundle update 10 | script: script/cibuild 11 | notifications: 12 | irc: 13 | on_success: change 14 | on_failure: change 15 | channels: 16 | - irc.freenode.org#jekyll 17 | template: 18 | - '%{repository}#%{build_number} (%{branch}) %{message} %{build_url}' 19 | email: 20 | on_success: never 21 | on_failure: never 22 | 23 | matrix: 24 | exclude: 25 | - rvm: 1.9.3 26 | env: JEKYLL_VERSION=3.0.0 27 | - env: JEKYLL_VERSION=2.4 28 | rvm: 2.1 29 | - rvm: 2.2 30 | env: JEKYLL_VERSION=2.4 31 | 32 | env: 33 | matrix: 34 | - "" 35 | - JEKYLL_VERSION=2.4 36 | - JEKYLL_VERSION=3.0.0 37 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in jekyll-paginate.gemspec 4 | gemspec 5 | 6 | if ENV["JEKYLL_VERSION"] 7 | gem "jekyll", ENV["JEKYLL_VERSION"] 8 | end 9 | -------------------------------------------------------------------------------- /History.markdown: -------------------------------------------------------------------------------- 1 | ## HEAD 2 | 3 | ### Minor Enhancements 4 | 5 | * Throw an error if the pagination path doesn't contain `:num`. (#19) 6 | 7 | ### Development Fixes 8 | 9 | * Upgrade to the new Travis build environment and use their built-in caching (#20) 10 | * Add GitHub Actions CI (#46) 11 | 12 | ## 1.1.0 / 2014-10-14 13 | 14 | ### Minor Enhancements 15 | 16 | * Filter out posts that have `hidden: true` in front matter (#13) 17 | 18 | ### Development Fixes 19 | 20 | * Fix tests for rspec 3. (#9) 21 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Parker Moore 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jekyll::Paginate 2 | 3 | Default pagination generator for Jekyll. 4 | 5 | [![Build Status](https://secure.travis-ci.org/jekyll/jekyll-paginate.svg?branch=master)](https://travis-ci.org/jekyll/jekyll-paginate) 6 | 7 | ## Installation 8 | 9 | Add this line to your application's Gemfile: 10 | 11 | gem 'jekyll-paginate' 12 | 13 | And then execute: 14 | 15 | $ bundle 16 | 17 | Or install it yourself as: 18 | 19 | $ gem install jekyll-paginate 20 | 21 | ## Usage 22 | 23 | Once the gem is installed on your system, Jekyll will auto-require it. Just set the following configuration 24 | 25 | ## Contributing 26 | 27 | 1. Fork it ( http://github.com/jekyll/jekyll-paginate/fork ) 28 | 2. Create your feature branch (`git checkout -b my-new-feature`) 29 | 3. Commit your changes (`git commit -am 'Add some feature'`) 30 | 4. Push to the branch (`git push origin my-new-feature`) 31 | 5. Create new Pull Request 32 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | -------------------------------------------------------------------------------- /jekyll-paginate.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'jekyll-paginate/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "jekyll-paginate" 8 | spec.version = Jekyll::Paginate::VERSION 9 | spec.authors = ["Parker Moore"] 10 | spec.email = ["parkrmoore@gmail.com"] 11 | spec.summary = %q{Built-in Pagination Generator for Jekyll} 12 | spec.homepage = "https://github.com/jekyll/jekyll-paginate" 13 | spec.license = "MIT" 14 | 15 | spec.files = `git ls-files -z`.split("\x0") 16 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 17 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 18 | spec.require_paths = ["lib"] 19 | 20 | spec.add_development_dependency "jekyll", ">= 2.0", "< 5.0" 21 | spec.add_development_dependency "bundler" 22 | spec.add_development_dependency "rake" 23 | spec.add_development_dependency "rspec", "~> 3.0" 24 | spec.add_development_dependency "rubocop-jekyll" 25 | end 26 | -------------------------------------------------------------------------------- /lib/jekyll-paginate.rb: -------------------------------------------------------------------------------- 1 | require "jekyll-paginate/version" 2 | require "jekyll-paginate/pager" 3 | require "jekyll-paginate/pagination" 4 | 5 | module Jekyll 6 | module Paginate 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /lib/jekyll-paginate/pager.rb: -------------------------------------------------------------------------------- 1 | module Jekyll 2 | module Paginate 3 | class Pager 4 | attr_reader :page, :per_page, :posts, :total_posts, :total_pages, 5 | :previous_page, :previous_page_path, :next_page, :next_page_path 6 | 7 | # Calculate the number of pages. 8 | # 9 | # all_posts - The Array of all Posts. 10 | # per_page - The Integer of entries per page. 11 | # 12 | # Returns the Integer number of pages. 13 | def self.calculate_pages(all_posts, per_page) 14 | (all_posts.size.to_f / per_page.to_i).ceil 15 | end 16 | 17 | # Determine if pagination is enabled the site. 18 | # 19 | # site - the Jekyll::Site object 20 | # 21 | # Returns true if pagination is enabled, false otherwise. 22 | def self.pagination_enabled?(site) 23 | !site.config['paginate'].nil? && 24 | site.pages.size > 0 25 | end 26 | 27 | # Static: Determine if a page is a possible candidate to be a template page. 28 | # Page's name must be `index.html` and exist in any of the directories 29 | # between the site source and `paginate_path`. 30 | # 31 | # config - the site configuration hash 32 | # page - the Jekyll::Page about which we're inquiring 33 | # 34 | # Returns true if the 35 | def self.pagination_candidate?(config, page) 36 | page_dir = File.dirname(File.expand_path(remove_leading_slash(page.path), config['source'])) 37 | paginate_path = remove_leading_slash(config['paginate_path']) 38 | paginate_path = File.expand_path(paginate_path, config['source']) 39 | page.name == 'index.html' && 40 | in_hierarchy(config['source'], page_dir, File.dirname(paginate_path)) 41 | end 42 | 43 | # Determine if the subdirectories of the two paths are the same relative to source 44 | # 45 | # source - the site source 46 | # page_dir - the directory of the Jekyll::Page 47 | # paginate_path - the absolute paginate path (from root of FS) 48 | # 49 | # Returns whether the subdirectories are the same relative to source 50 | def self.in_hierarchy(source, page_dir, paginate_path) 51 | return false if paginate_path == File.dirname(paginate_path) 52 | return false if paginate_path == Pathname.new(source).parent 53 | page_dir == paginate_path || 54 | in_hierarchy(source, page_dir, File.dirname(paginate_path)) 55 | end 56 | 57 | # Static: Return the pagination path of the page 58 | # 59 | # site - the Jekyll::Site object 60 | # num_page - the pagination page number 61 | # 62 | # Returns the pagination path as a string 63 | def self.paginate_path(site, num_page) 64 | return nil if num_page.nil? 65 | return Pagination.first_page_url(site) if num_page <= 1 66 | format = site.config['paginate_path'] 67 | if format.include?(":num") 68 | format = format.sub(':num', num_page.to_s) 69 | else 70 | raise ArgumentError.new("Invalid pagination path: '#{format}'. It must include ':num'.") 71 | end 72 | ensure_leading_slash(format) 73 | end 74 | 75 | # Static: Return a String version of the input which has a leading slash. 76 | # If the input already has a forward slash in position zero, it will be 77 | # returned unchanged. 78 | # 79 | # path - a String path 80 | # 81 | # Returns the path with a leading slash 82 | def self.ensure_leading_slash(path) 83 | path[0..0] == "/" ? path : "/#{path}" 84 | end 85 | 86 | # Static: Return a String version of the input without a leading slash. 87 | # 88 | # path - a String path 89 | # 90 | # Returns the input without the leading slash 91 | def self.remove_leading_slash(path) 92 | ensure_leading_slash(path)[1..-1] 93 | end 94 | 95 | # Initialize a new Pager. 96 | # 97 | # site - the Jekyll::Site object 98 | # page - The Integer page number. 99 | # all_posts - The Array of all the site's Posts. 100 | # num_pages - The Integer number of pages or nil if you'd like the number 101 | # of pages calculated. 102 | def initialize(site, page, all_posts, num_pages = nil) 103 | @page = page 104 | @per_page = site.config['paginate'].to_i 105 | @total_pages = num_pages || Pager.calculate_pages(all_posts, @per_page) 106 | 107 | if @page > @total_pages 108 | raise RuntimeError, "page number can't be greater than total pages: #{@page} > #{@total_pages}" 109 | end 110 | 111 | init = (@page - 1) * @per_page 112 | offset = (init + @per_page - 1) >= all_posts.size ? all_posts.size : (init + @per_page - 1) 113 | 114 | @total_posts = all_posts.size 115 | @posts = all_posts[init..offset] 116 | @previous_page = @page != 1 ? @page - 1 : nil 117 | @previous_page_path = Pager.paginate_path(site, @previous_page) 118 | @next_page = @page != @total_pages ? @page + 1 : nil 119 | @next_page_path = Pager.paginate_path(site, @next_page) 120 | end 121 | 122 | # Convert this Pager's data to a Hash suitable for use by Liquid. 123 | # 124 | # Returns the Hash representation of this Pager. 125 | def to_liquid 126 | { 127 | 'page' => page, 128 | 'per_page' => per_page, 129 | 'posts' => posts, 130 | 'total_posts' => total_posts, 131 | 'total_pages' => total_pages, 132 | 'previous_page' => previous_page, 133 | 'previous_page_path' => previous_page_path, 134 | 'next_page' => next_page, 135 | 'next_page_path' => next_page_path 136 | } 137 | end 138 | 139 | end 140 | end 141 | end 142 | -------------------------------------------------------------------------------- /lib/jekyll-paginate/pagination.rb: -------------------------------------------------------------------------------- 1 | module Jekyll 2 | module Paginate 3 | class Pagination < Generator 4 | # This generator is safe from arbitrary code execution. 5 | safe true 6 | 7 | # This generator should be passive with regard to its execution 8 | priority :lowest 9 | 10 | # Generate paginated pages if necessary. 11 | # 12 | # site - The Site. 13 | # 14 | # Returns nothing. 15 | def generate(site) 16 | if Pager.pagination_enabled?(site) 17 | if template = self.class.template_page(site) 18 | paginate(site, template) 19 | else 20 | Jekyll.logger.warn "Pagination:", "Pagination is enabled, but I couldn't find " + 21 | "an index.html page to use as the pagination template. Skipping pagination." 22 | end 23 | end 24 | end 25 | 26 | # Paginates the blog's posts. Renders the index.html file into paginated 27 | # directories, e.g.: page2/index.html, page3/index.html, etc and adds more 28 | # site-wide data. 29 | # 30 | # site - The Site. 31 | # page - The index.html Page that requires pagination. 32 | # 33 | # {"paginator" => { "page" => , 34 | # "per_page" => , 35 | # "posts" => [], 36 | # "total_posts" => , 37 | # "total_pages" => , 38 | # "previous_page" => , 39 | # "next_page" => }} 40 | def paginate(site, page) 41 | all_posts = site.site_payload['site']['posts'].reject { |post| post['hidden'] } 42 | pages = Pager.calculate_pages(all_posts, site.config['paginate'].to_i) 43 | (1..pages).each do |num_page| 44 | pager = Pager.new(site, num_page, all_posts, pages) 45 | if num_page > 1 46 | newpage = Page.new(site, site.source, page.dir, page.name) 47 | newpage.pager = pager 48 | newpage.dir = Pager.paginate_path(site, num_page) 49 | site.pages << newpage 50 | else 51 | page.pager = pager 52 | end 53 | end 54 | end 55 | 56 | # Static: Fetch the URL of the template page. Used to determine the 57 | # path to the first pager in the series. 58 | # 59 | # site - the Jekyll::Site object 60 | # 61 | # Returns the url of the template page 62 | def self.first_page_url(site) 63 | if page = Pagination.template_page(site) 64 | page.url 65 | else 66 | nil 67 | end 68 | end 69 | 70 | # Public: Find the Jekyll::Page which will act as the pager template 71 | # 72 | # site - the Jekyll::Site object 73 | # 74 | # Returns the Jekyll::Page which will act as the pager template 75 | def self.template_page(site) 76 | site.pages.select do |page| 77 | Pager.pagination_candidate?(site.config, page) 78 | end.sort do |one, two| 79 | two.path.size <=> one.path.size 80 | end.first 81 | end 82 | 83 | end 84 | end 85 | end 86 | -------------------------------------------------------------------------------- /lib/jekyll-paginate/version.rb: -------------------------------------------------------------------------------- 1 | module Jekyll 2 | module Paginate 3 | VERSION = "1.1.0" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /script/bootstrap: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | bundle install 4 | -------------------------------------------------------------------------------- /script/cibuild: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | bundle exec rspec 4 | -------------------------------------------------------------------------------- /spec/pager_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | RSpec.describe(Jekyll::Paginate::Pager) do 4 | 5 | it "calculate number of pages" do 6 | expect(described_class.calculate_pages([], '2')).to eql(0) 7 | expect(described_class.calculate_pages([1], '2')).to eql(1) 8 | expect(described_class.calculate_pages([1,2], '2')).to eql(1) 9 | expect(described_class.calculate_pages([1,2,3], '2')).to eql(2) 10 | expect(described_class.calculate_pages([1,2,3,4], '2')).to eql(2) 11 | expect(described_class.calculate_pages([1,2,3,4,5], '2')).to eql(3) 12 | end 13 | 14 | context "with the default paginate_path" do 15 | let(:site) { build_site } 16 | 17 | it "determines the correct pagination path for each page" do 18 | if Jekyll::VERSION < '3.0.0' 19 | expect(described_class.paginate_path(site, 1)).to eql("/index.html") 20 | else 21 | expect(described_class.paginate_path(site, 1)).to eql("/") 22 | end 23 | 24 | expect(described_class.paginate_path(site, 2)).to eql("/page2") 25 | end 26 | end 27 | 28 | context "with paginate_path set to a subdirectory with no index.html" do 29 | let(:site) { build_site({'paginate_path' => '/blog/page-:num'}) } 30 | 31 | it "determines the correct pagination path for each page" do 32 | if Jekyll::VERSION < '3.0.0' 33 | expect(described_class.paginate_path(site, 1)).to eql("/index.html") 34 | else 35 | expect(described_class.paginate_path(site, 1)).to eql("/") 36 | end 37 | 38 | expect(described_class.paginate_path(site, 2)).to eql("/blog/page-2") 39 | end 40 | end 41 | 42 | context "with paginate_path set to a subdirectory with no index.html with num pages being in subdirectories" do 43 | let(:site) { build_site({'paginate_path' => '/blog/page/:num'}) } 44 | 45 | it "determines the correct pagination path for each page" do 46 | if Jekyll::VERSION < '3.0.0' 47 | expect(described_class.paginate_path(site, 1)).to eql("/index.html") 48 | else 49 | expect(described_class.paginate_path(site, 1)).to eql("/") 50 | end 51 | 52 | expect(described_class.paginate_path(site, 2)).to eql("/blog/page/2") 53 | end 54 | end 55 | 56 | context "with paginate_path set to a subdirectory wherein an index.html exists" do 57 | let(:site) { build_site({'paginate_path' => '/contacts/page:num'}) } 58 | 59 | it "determines the correct pagination path for each page" do 60 | if Jekyll::VERSION < '3.0.0' 61 | expect(described_class.paginate_path(site, 1)).to eql("/contacts/index.html") 62 | else 63 | expect(described_class.paginate_path(site, 1)).to eql("/contacts/") 64 | end 65 | 66 | expect(described_class.paginate_path(site, 2)).to eql("/contacts/page2") 67 | end 68 | end 69 | 70 | context "with paginate_path set to a subdir wherein an index.html exists with pages in subdirs" do 71 | let(:site) { build_site({'paginate_path' => '/contacts/page/:num'}) } 72 | 73 | it "determines the correct pagination path for each page" do 74 | if Jekyll::VERSION < '3.0.0' 75 | expect(described_class.paginate_path(site, 1)).to eql("/contacts/index.html") 76 | else 77 | expect(described_class.paginate_path(site, 1)).to eql("/contacts/") 78 | end 79 | 80 | expect(described_class.paginate_path(site, 2)).to eql("/contacts/page/2") 81 | end 82 | end 83 | 84 | context "with an paginate_path devoid of :num" do 85 | let(:site) { build_site({'paginate_path' => '/blog/page'}) } 86 | 87 | it "determines the correct pagination path for each page" do 88 | expect(-> { described_class.paginate_path(site, 1) }).to raise_error 89 | end 90 | end 91 | 92 | context "pagination disabled" do 93 | let(:site) { build_site('paginate' => nil) } 94 | 95 | it "report that pagination is disabled" do 96 | expect(described_class.pagination_enabled?(site)).to be_falsey 97 | end 98 | end 99 | 100 | context "pagination enabled for 2" do 101 | let(:site) { build_site('paginate' => 2) } 102 | if Jekyll::VERSION < '3.0.0' 103 | let(:posts) { site.posts } 104 | else 105 | let(:posts) { site.posts.docs } 106 | end 107 | 108 | it "report that pagination is enabled" do 109 | expect(described_class.pagination_enabled?(site)).to be_truthy 110 | end 111 | 112 | context "with 4 posts" do 113 | if Jekyll::VERSION < '3.0.0' 114 | let(:posts) { site.posts[1..4] } 115 | else 116 | let(:posts) { site.posts.docs[1..4] } 117 | end 118 | 119 | it "create first pager" do 120 | pager = described_class.new(site, 1, posts) 121 | expect(pager.posts.size).to eql(2) 122 | expect(pager.total_pages).to eql(2) 123 | expect(pager.previous_page).to be_nil 124 | expect(pager.next_page).to eql(2) 125 | end 126 | 127 | it "create second pager" do 128 | pager = described_class.new(site, 2, posts) 129 | expect(pager.posts.size).to eql(2) 130 | expect(pager.total_pages).to eql(2) 131 | expect(pager.previous_page).to eql(1) 132 | expect(pager.next_page).to be_nil 133 | end 134 | 135 | it "not create third pager" do 136 | expect { described_class.new(site, 3, posts) }.to raise_error 137 | end 138 | end 139 | 140 | context "with 5 posts" do 141 | if Jekyll::VERSION < '3.0.0' 142 | let(:posts) { site.posts[1..5] } 143 | else 144 | let(:posts) { site.posts.docs[1..5] } 145 | end 146 | 147 | it "create first pager" do 148 | pager = described_class.new(site, 1, posts) 149 | expect(pager.posts.size).to eql(2) 150 | expect(pager.total_pages).to eql(3) 151 | expect(pager.previous_page).to be_nil 152 | expect(pager.next_page).to eql(2) 153 | end 154 | 155 | it "create second pager" do 156 | pager = described_class.new(site, 2, posts) 157 | expect(pager.posts.size).to eql(2) 158 | expect(pager.total_pages).to eql(3) 159 | expect(pager.previous_page).to eql(1) 160 | expect(pager.next_page).to eql(3) 161 | end 162 | 163 | it "create third pager" do 164 | pager = described_class.new(site, 3, posts) 165 | expect(pager.posts.size).to eql(1) 166 | expect(pager.total_pages).to eql(3) 167 | expect(pager.previous_page).to eql(2) 168 | expect(pager.next_page).to be_nil 169 | end 170 | 171 | it "not create fourth pager" do 172 | expect { described_class.new(site, 4, posts) }.to raise_error(RuntimeError) 173 | end 174 | 175 | end 176 | end 177 | 178 | end 179 | -------------------------------------------------------------------------------- /spec/pagination_spec.rb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jekyll/jekyll-paginate/9aa903799f85c8572c2a5977266b194bc6741201/spec/pagination_spec.rb -------------------------------------------------------------------------------- /spec/source/_posts/2014-05-20-blah.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jekyll/jekyll-paginate/9aa903799f85c8572c2a5977266b194bc6741201/spec/source/_posts/2014-05-20-blah.html -------------------------------------------------------------------------------- /spec/source/_posts/2014-05-21-bleh.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jekyll/jekyll-paginate/9aa903799f85c8572c2a5977266b194bc6741201/spec/source/_posts/2014-05-21-bleh.html -------------------------------------------------------------------------------- /spec/source/_posts/2014-05-22-humor.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jekyll/jekyll-paginate/9aa903799f85c8572c2a5977266b194bc6741201/spec/source/_posts/2014-05-22-humor.html -------------------------------------------------------------------------------- /spec/source/_posts/2014-05-23-hey-there.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jekyll/jekyll-paginate/9aa903799f85c8572c2a5977266b194bc6741201/spec/source/_posts/2014-05-23-hey-there.html -------------------------------------------------------------------------------- /spec/source/_posts/2014-05-24-whateva.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jekyll/jekyll-paginate/9aa903799f85c8572c2a5977266b194bc6741201/spec/source/_posts/2014-05-24-whateva.html -------------------------------------------------------------------------------- /spec/source/_posts/2014-05-25-oh-yes.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jekyll/jekyll-paginate/9aa903799f85c8572c2a5977266b194bc6741201/spec/source/_posts/2014-05-25-oh-yes.html -------------------------------------------------------------------------------- /spec/source/contacts/index.html: -------------------------------------------------------------------------------- 1 | --- 2 | --- -------------------------------------------------------------------------------- /spec/source/index.html: -------------------------------------------------------------------------------- 1 | --- 2 | --- -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'jekyll' 2 | require File.expand_path("../lib/jekyll-paginate", File.dirname(__FILE__)) 3 | 4 | module TestMethods 5 | def test_dir(*subdirs) 6 | File.join(File.dirname(__FILE__), *subdirs) 7 | end 8 | 9 | def dest_dir(*subdirs) 10 | test_dir('dest', *subdirs) 11 | end 12 | 13 | def source_dir(*subdirs) 14 | test_dir('source', *subdirs) 15 | end 16 | 17 | def build_configs(overrides, base_hash = Jekyll::Configuration::DEFAULTS) 18 | Jekyll::Utils.deep_merge_hashes(base_hash, overrides) 19 | end 20 | 21 | def site_configuration(overrides = {}) 22 | build_configs({ 23 | "source" => source_dir, 24 | "destination" => dest_dir 25 | }, build_configs(overrides)) 26 | end 27 | 28 | def build_site(config = {}) 29 | site = Jekyll::Site.new(site_configuration( 30 | {"paginate" => 1}.merge(config) 31 | )) 32 | site.process 33 | site 34 | end 35 | end 36 | 37 | RSpec.configure do |config| 38 | config.expect_with :rspec do |expectations| 39 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 40 | end 41 | 42 | # rspec-mocks config goes here. You can use an alternate test double 43 | # library (such as bogus or mocha) by changing the `mock_with` option here. 44 | config.mock_with :rspec do |mocks| 45 | # Prevents you from mocking or stubbing a method that does not exist on 46 | # a real object. This is generally recommended, and will default to 47 | # `true` in RSpec 4. 48 | mocks.verify_partial_doubles = true 49 | end 50 | 51 | # These two settings work together to allow you to limit a spec run 52 | # to individual examples or groups you care about by tagging them with 53 | # `:focus` metadata. When nothing is tagged with `:focus`, all examples 54 | # get run. 55 | config.filter_run :focus 56 | config.run_all_when_everything_filtered = true 57 | 58 | # Limits the available syntax to the non-monkey patched syntax that is recommended. 59 | # For more details, see: 60 | # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax 61 | # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 62 | # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching 63 | config.disable_monkey_patching! 64 | 65 | # This setting enables warnings. It's recommended, but in some cases may 66 | # be too noisy due to issues in dependencies. 67 | # config.warnings = true 68 | 69 | # Many RSpec users commonly either run the entire suite or an individual 70 | # file, and it's useful to allow more verbose output when running an 71 | # individual spec file. 72 | if config.files_to_run.one? 73 | # Use the documentation formatter for detailed output, 74 | # unless a formatter has already been configured 75 | # (e.g. via a command-line flag). 76 | config.default_formatter = 'doc' 77 | end 78 | 79 | # Print the 10 slowest examples and example groups at the 80 | # end of the spec run, to help surface which specs are running 81 | # particularly slow. 82 | config.profile_examples = 10 83 | 84 | # Run specs in random order to surface order dependencies. If you find an 85 | # order dependency and want to debug it, you can fix the order by providing 86 | # the seed, which is printed after each run. 87 | # --seed 1234 88 | config.order = :random 89 | 90 | # Seed global randomization in this process using the `--seed` CLI option. 91 | # Setting this allows you to use `--seed` to deterministically reproduce 92 | # test failures related to randomization by passing the same `--seed` value 93 | # as the one that triggered the failure. 94 | Kernel.srand config.seed 95 | 96 | include TestMethods 97 | end 98 | --------------------------------------------------------------------------------