jekyll-index-pages 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1ce65b6deaeb39e301bca0272d1bd62e17221afe
4
+ data.tar.gz: e2d7e5c552696eae82bbb78a46403018be516513
5
+ SHA512:
6
+ metadata.gz: 0e1d7966321495e3c404f4736123c32b38413f4b96f228777c7b0281a536463eba5bdaf16753cc8470bed6403e19c189436c57736e89ce9c4ee23210c77ef1f9
7
+ data.tar.gz: 39cf9d4784a6cd473a2267cdbd88696b0f03a7ec5400d08acc27a0a373899bd7b3bd0059ff0394e6b324149febb12b38d17b9deda9744174fe092d48dc776d9e
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .ruby-version
2
+ .ruby-gemset
3
+ Gemfile.lock
4
+ spec/dest
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Jose Miguel Venegas Mendoza
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
+ of the Software, and to permit persons to whom the Software is furnished to do
10
+ so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,134 @@
1
+ # Jekyll Index Pages
2
+
3
+ Index page generator for Jekyll sites. Generates paginated index pages for blog
4
+ posts, categories and tags.
5
+
6
+ ## Installation
7
+
8
+ To perform a manual install, execute this command on your terminal:
9
+
10
+ ```sh
11
+ $ gem install jekyll-index-pages
12
+ ```
13
+
14
+ Or, if you happen to use Bundler, add this line to your Gemfile:
15
+
16
+ ```ruby
17
+ gem "jekyll-index-pages"
18
+ ```
19
+
20
+ Then add the gem to the `gems` setting in your `_config.yml` file:
21
+
22
+ ```yaml
23
+ gems:
24
+ - jekyll-index-pages
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ### Configuring the plugin
30
+
31
+ If you want to generate index pages for your blog posts, you can add the
32
+ following settings to your `_config.yml` file:
33
+
34
+ ```yaml
35
+ index_pages:
36
+ posts:
37
+ title: Post Listing Page Title
38
+ excerpt: Post listing page excerpt
39
+ per_page: 20
40
+ permalink: /blog/
41
+ layout: blog
42
+ ```
43
+
44
+ This will tell the plugin to generate index pages with given title and excerpt,
45
+ using the layout named `_layouts/blog.html`. Each index page will contain up to
46
+ 20 posts. First page can be accessed at `/blog/`. Subsequent pages can be
47
+ accessed at `/blog/<page-num>/`.
48
+
49
+ Default values for each setting are:
50
+
51
+ ```yaml
52
+ index_pages:
53
+ posts:
54
+ title: posts
55
+ excerpt: posts
56
+ per_page: 10
57
+ permalink: /posts/
58
+ layout: posts
59
+ ```
60
+
61
+ If you want to generate index pages for categories, add the `categories`
62
+ setting to `index_page` section:
63
+
64
+ ```yaml
65
+ # Displaying default values
66
+ index_pages:
67
+ categories:
68
+ title: categories
69
+ excerpt: categories
70
+ per_page: 10
71
+ permalink: /categories/
72
+ layout: categories
73
+ ```
74
+
75
+ Given the default values above, categories index pages can be accessed at
76
+ `/categories/<category-name-slug>/`.
77
+
78
+ The same reasoning applies if you want to generate index pages for tags:
79
+
80
+ ```yaml
81
+ # Displaying default values
82
+ index_pages:
83
+ tags:
84
+ title: tags
85
+ excerpt: tags
86
+ per_page: 10
87
+ permalink: /tags/
88
+ layout: tags
89
+ ```
90
+
91
+ Given the default values above, tags index pages can be accessed at
92
+ `/tags/<tag-name-slug>/`.
93
+
94
+ ### Including documents and pagination into templates
95
+
96
+ To include the paginated documents in your layouts, you can use the `pager`
97
+ variable as demonstrated next:
98
+
99
+ ```liquid
100
+ {% assign pager = page.pager %}
101
+
102
+ {% for doc in pager.docs %}
103
+ <h2>{{ doc.title }}</h2>
104
+ {{ doc.excerpt }}
105
+ <a href="{{ doc.url }}">Read more...</a>
106
+ {% endfor %}
107
+ ```
108
+
109
+ Each document in `pager.docs` is a Jekyll document, so you can access all its
110
+ variables as normally do when developing a layout.
111
+
112
+ To include the pagination, you can do the following:
113
+
114
+ ```liquid
115
+ {% assign pager = page.pager %}
116
+
117
+ {% if pager.total_pages > 1 %}
118
+ {% if pager.prev_page > 0 %}
119
+ <a href="{{ pager.prev_page_url }}">Prev. page</a>
120
+ {% endif %}
121
+ <span>Page {{ pager.current_page }} of {{ pager.total_pages }}</span>
122
+ {% if next_page > 0 %}
123
+ <a href="{{ pager.next_page_url }}">Next page</a>
124
+ {% endif %}
125
+ {% endif %}
126
+ ```
127
+
128
+ ## Contributing
129
+
130
+ 1. Fork it (https://github.com/rukbotto/jekyll-index-pages/fork)
131
+ 2. Create your feature branch (git checkout -b my-new-feature)
132
+ 3. Commit your changes (git commit -am 'Add some feature')
133
+ 4. Push to the branch (git push origin my-new-feature)
134
+ 5. Create a new Pull Request
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/jekyll-index-pages/version", __FILE__)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "jekyll-index-pages"
6
+ spec.version = JekyllIndexPages::VERSION
7
+ spec.date = "2017-02-21"
8
+ spec.authors = ["Jose Miguel Venegas Mendoza"]
9
+ spec.email = ["jvenegasmendoza@gmail.com"]
10
+ spec.homepage = "https://github.com/rukbotto/jekyll-index-pages"
11
+ spec.license = "MIT"
12
+
13
+ spec.summary = "Index page generator for Jekyll sites."
14
+ spec.description = <<-DESCRIPTION
15
+ Generates paginated index pages for blog posts, categories, tags, authors
16
+ and collections. It also let you generate a paginated blog post archive.
17
+ DESCRIPTION
18
+
19
+ spec.files = `git ls-files`.split("\n")
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency("jekyll", "~> 3.3")
25
+
26
+ spec.add_development_dependency("bundler", "~> 1.14")
27
+ spec.add_development_dependency("rspec", "~> 3.5")
28
+ end
@@ -0,0 +1,55 @@
1
+ module JekyllIndexPages
2
+ class Generator < Jekyll::Generator
3
+ safe true
4
+
5
+ def generate(site)
6
+ config = site.config["index_pages"] || {}
7
+ config.each do |kind, item|
8
+ permalink = item["permalink"] || "/:label/"
9
+ per_page = item['per_page'] || 10
10
+ layout = item["layout"] || "#{kind}"
11
+
12
+ next if !site.layouts.key? layout
13
+
14
+ doc_group =
15
+ case kind
16
+ when "posts"
17
+ { "posts" => site.posts.docs.reverse }
18
+ when "categories"
19
+ site.categories
20
+ when "tags"
21
+ site.tags
22
+ else
23
+ {}
24
+ end
25
+
26
+ doc_group.each do |label, docs|
27
+ pagination = Pagination.new(docs, per_page)
28
+
29
+ (0...pagination.total).each do |current|
30
+ pagination.paginate(current)
31
+
32
+ pager = pagination.pager
33
+
34
+ dir =
35
+ File.join(
36
+ permalink.sub(":label", Jekyll::Utils.slugify(label)),
37
+ (pager.current_page > 1) ? pager.current_page.to_s : ""
38
+ )
39
+
40
+ site.pages <<
41
+ IndexPage.new(
42
+ site,
43
+ site.source,
44
+ dir,
45
+ item,
46
+ label,
47
+ layout,
48
+ pager
49
+ )
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,38 @@
1
+ module JekyllIndexPages
2
+ class IndexPage < Jekyll::Page
3
+ def initialize(site, base, dir, config, label, layout, pager)
4
+ @site = site
5
+ @base = base
6
+ @dir = dir
7
+ @name = "index.html"
8
+
9
+ title = config["title"] || ":label"
10
+ excerpt = config["excerpt"] || ":label"
11
+
12
+ self.process(@name)
13
+ self.read_yaml(File.join(base, "_layouts"), "#{layout}.html")
14
+
15
+ self.data["title"] = title.sub(":label", label)
16
+ self.data["excerpt"] = excerpt.sub(":label", label)
17
+
18
+ self.data["pager"] = Hash.new
19
+ self.data["pager"]["docs"] = pager.docs.sort { |x, y| y.date <=> x.date }
20
+ self.data["pager"]["total_pages"] = pager.total_pages
21
+ self.data["pager"]["current_page"] = pager.current_page
22
+ self.data["pager"]["prev_page"] = pager.prev_page
23
+ self.data["pager"]["next_page"] = pager.next_page
24
+ self.data["pager"]["prev_page_url"] =
25
+ if (pager.prev_page > 0)
26
+ File.join(dir.sub(%r{/\d+}, ""), pager.prev_page.to_s << "/")
27
+ else
28
+ ""
29
+ end
30
+ self.data["pager"]["next_page_url"] =
31
+ if (pager.next_page > 0)
32
+ File.join(dir.sub(%r{/\d+}, ""), pager.next_page.to_s << "/")
33
+ else
34
+ ""
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,30 @@
1
+ module JekyllIndexPages
2
+ class Pagination
3
+ Pager = Struct.new(
4
+ :docs,
5
+ :total_pages,
6
+ :current_page,
7
+ :prev_page,
8
+ :next_page
9
+ )
10
+
11
+ attr_reader :total, :pager
12
+
13
+ def initialize(docs, per_page)
14
+ @docs = docs
15
+ @per_page = per_page
16
+ @total = per_page > 0 ? (docs.length.to_f / per_page).ceil : 0
17
+ @pager = Pager.new([], @total, 0, 0, 0)
18
+ end
19
+
20
+ def paginate(current)
21
+ first = @per_page * current
22
+ last = first + @per_page
23
+
24
+ @pager.docs = @docs[first...last]
25
+ @pager.current_page = current + 1
26
+ @pager.prev_page = current
27
+ @pager.next_page = (current < total - 1) ? current + 2 : 0
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module JekyllIndexPages
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "jekyll"
2
+ require "jekyll-index-pages/generator"
3
+
4
+ module JekyllIndexPages
5
+ autoload :Pagination, "jekyll-index-pages/pagination"
6
+ autoload :IndexPage, "jekyll-index-pages/index-page"
7
+ end
@@ -0,0 +1,6 @@
1
+ defaults:
2
+ - scope:
3
+ path: ""
4
+ type: posts
5
+ values:
6
+ layout: default
@@ -0,0 +1,27 @@
1
+ ---
2
+ ---
3
+ <html>
4
+ <head>
5
+ <title>{{ page.title }}</title>
6
+ <meta name="description" content="{{ page.excerpt | strip_html }}">
7
+ </head>
8
+ <body>
9
+ {% assign pager = page.pager %}
10
+
11
+ <h1>{{ page.title }}</h1>
12
+
13
+ {% for doc in pager.docs %}
14
+ <h2>{{ doc.title }}</h2>
15
+ {% endfor%}
16
+
17
+ {% if pager.total_pages > 1 %}
18
+ {% if pager.prev_page > 0 %}
19
+ <a href="{{ pager.prev_page_url }}">Prev. page</a>
20
+ {% endif %}
21
+ <span>Page {{ pager.current_page }} of {{ pager.total_pages }}</span>
22
+ {% if pager.next_page > 0 %}
23
+ <a href="{{ pager.next_page_url }}">Next page</a>
24
+ {% endif %}
25
+ {% endif %}
26
+ </body>
27
+ </html>
@@ -0,0 +1,11 @@
1
+ ---
2
+ ---
3
+ <html>
4
+ <head>
5
+ <title>{{ page.title }}</title>
6
+ <meta name="description" content="{{ page.excerpt | strip_html }}">
7
+ </head>
8
+ <body>
9
+ <h1>Custom Layout</h1>
10
+ </body>
11
+ </html>
@@ -0,0 +1,11 @@
1
+ ---
2
+ ---
3
+ <html>
4
+ <head>
5
+ <title>{{ page.title }}</title>
6
+ <meta name="description" content="{{ page.excerpt | strip_html }}">
7
+ </head>
8
+ <body>
9
+ {{ content }}
10
+ </body>
11
+ </html>
@@ -0,0 +1,27 @@
1
+ ---
2
+ ---
3
+ <html>
4
+ <head>
5
+ <title>{{ page.title }}</title>
6
+ <meta name="description" content="{{ page.excerpt | strip_html }}">
7
+ </head>
8
+ <body>
9
+ {% assign pager = page.pager %}
10
+
11
+ <h1>{{ page.title }}</h1>
12
+
13
+ {% for doc in pager.docs %}
14
+ <h2>{{ doc.title }}</h2>
15
+ {% endfor%}
16
+
17
+ {% if pager.total_pages > 1 %}
18
+ {% if pager.prev_page > 0 %}
19
+ <a href="{{ pager.prev_page_url }}">Prev. page</a>
20
+ {% endif %}
21
+ <span>Page {{ pager.current_page }} of {{ pager.total_pages }}</span>
22
+ {% if pager.next_page > 0 %}
23
+ <a href="{{ pager.next_page_url }}">Next page</a>
24
+ {% endif %}
25
+ {% endif %}
26
+ </body>
27
+ </html>
@@ -0,0 +1,27 @@
1
+ ---
2
+ ---
3
+ <html>
4
+ <head>
5
+ <title>{{ page.title }}</title>
6
+ <meta name="description" content="{{ page.excerpt | strip_html }}">
7
+ </head>
8
+ <body>
9
+ {% assign pager = page.pager %}
10
+
11
+ <h1>{{ page.title }}</h1>
12
+
13
+ {% for doc in pager.docs %}
14
+ <h2>{{ doc.title }}</h2>
15
+ {% endfor%}
16
+
17
+ {% if pager.total_pages > 1 %}
18
+ {% if pager.prev_page > 0 %}
19
+ <a href="{{ pager.prev_page_url }}">Prev. page</a>
20
+ {% endif %}
21
+ <span>Page {{ pager.current_page }} of {{ pager.total_pages }}</span>
22
+ {% if pager.next_page > 0 %}
23
+ <a href="{{ pager.next_page_url }}">Next page</a>
24
+ {% endif %}
25
+ {% endif %}
26
+ </body>
27
+ </html>
@@ -0,0 +1,7 @@
1
+ ---
2
+ title: "Star Trek: The Original Series"
3
+ category: Science fiction
4
+ tags: star-trek
5
+ ---
6
+
7
+ # Star Trek: The Original Series
@@ -0,0 +1,7 @@
1
+ ---
2
+ title: "Star Trek: The Next Generation"
3
+ category: Science fiction
4
+ tags: star-trek
5
+ ---
6
+
7
+ # Star Trek: The Next Generation
@@ -0,0 +1,7 @@
1
+ ---
2
+ title: "Star Trek: Deep Space Nine"
3
+ category: Science fiction
4
+ tags: star-trek
5
+ ---
6
+
7
+ # Star Trek: Deep Space Nine
@@ -0,0 +1,7 @@
1
+ ---
2
+ title: "Star Trek: Voyager"
3
+ category: Science fiction
4
+ tags: star-trek
5
+ ---
6
+
7
+ # Star Trek: Voyager
@@ -0,0 +1,7 @@
1
+ ---
2
+ title: "Star Trek: Enterprise"
3
+ category: Science fiction
4
+ tags: star-trek
5
+ ---
6
+
7
+ # Star Trek: Enterprise
@@ -0,0 +1,205 @@
1
+ require "spec_helper"
2
+
3
+ describe(JekyllIndexPages::Generator) do
4
+ let(:overrides) { Hash.new }
5
+ let(:site_config) do
6
+ Jekyll.configuration(Jekyll::Utils.deep_merge_hashes({
7
+ "source" => File.expand_path("../fixtures/index-page", __FILE__),
8
+ "destination" => File.expand_path("../dest", __FILE__)
9
+ }, overrides))
10
+ end
11
+ let(:site) { Jekyll::Site.new(site_config) }
12
+
13
+ before(:each) do
14
+ site.process
15
+ end
16
+
17
+ context("When no configuration is provided") do
18
+ describe("Generator.generate") do
19
+ it("skips index page generation") do
20
+ expect(site.pages.length).to eq(0)
21
+ end
22
+ end
23
+ end
24
+
25
+ context("When default configuration for post index pages is provided") do
26
+ let(:overrides) do
27
+ {
28
+ "index_pages" => {
29
+ "posts" => {}
30
+ }
31
+ }
32
+ end
33
+
34
+ describe("Generator.generate") do
35
+ it("generates a single post index page") do
36
+ expect(site.pages.length).to eq(1)
37
+ end
38
+
39
+ it("generates post index page with default title and excerpt") do
40
+ expect(site.pages[0].data["title"]).to eq("posts")
41
+ expect(site.pages[0].data["excerpt"]).to eq("posts")
42
+ end
43
+
44
+ it("generates a post index page at /posts/") do
45
+ expect(site.pages[0].url).to eq("/posts/")
46
+ end
47
+
48
+ it("generates a post index page with five documents") do
49
+ expect(site.pages[0].data["pager"]["docs"].length).to eq(5)
50
+ end
51
+
52
+ it("generates a post index page with recent documents first") do
53
+ recent_doc = site.pages[0].data["pager"]["docs"][0]
54
+ older_doc = site.pages[0].data["pager"]["docs"][1]
55
+ expect(recent_doc.date).to be > older_doc.date
56
+ end
57
+
58
+ it("generates a post index page with a pager") do
59
+ expect(site.pages[0].data["pager"]).to be_instance_of(Hash)
60
+ end
61
+ end
62
+ end
63
+
64
+ context("When custom title and excerpt for posts index page is provided") do
65
+ let(:overrides) do
66
+ {
67
+ "index_pages" => {
68
+ "posts" => {
69
+ "title" => "Custom Title",
70
+ "excerpt" => "Custom excerpt"
71
+ }
72
+ }
73
+ }
74
+ end
75
+
76
+ describe("Generator.generate") do
77
+ it("generates post index page with custom values") do
78
+ expect(site.pages[0].data["title"]).to eq("Custom Title")
79
+ expect(site.pages[0].data["excerpt"]).to eq("Custom excerpt")
80
+ end
81
+ end
82
+ end
83
+
84
+ context("When custom 'per page' setting for posts index page is provided") do
85
+ let(:overrides) do
86
+ {
87
+ "index_pages" => {
88
+ "posts" => {
89
+ "per_page" => 2
90
+ }
91
+ }
92
+ }
93
+ end
94
+
95
+ describe("Generator.generate") do
96
+ context("generates the first post index page") do
97
+ let(:page) { site.pages[0] }
98
+
99
+ it("with two documents") do
100
+ expect(page.data["pager"]["docs"].length).to eq(2)
101
+ end
102
+
103
+ it("and next page url only") do
104
+ expect(page.data["pager"]["prev_page_url"]).to eq("")
105
+ expect(page.data["pager"]["next_page_url"]).to eq("/posts/2/")
106
+ end
107
+ end
108
+
109
+ context("generates the second post index page") do
110
+ let(:page) { site.pages[1] }
111
+
112
+ it("with previous and next page urls") do
113
+ expect(page.data["pager"]["prev_page_url"]).to eq("/posts/1/")
114
+ expect(page.data["pager"]["next_page_url"]).to eq("/posts/3/")
115
+ end
116
+ end
117
+
118
+ context("generates the third post index page") do
119
+ let(:page) { site.pages[2] }
120
+
121
+ it("with previous page url only") do
122
+ expect(page.data["pager"]["prev_page_url"]).to eq("/posts/2/")
123
+ expect(page.data["pager"]["next_page_url"]).to eq("")
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ context("When custom 'permalink' setting for posts index page is provided") do
130
+ let(:overrides) do
131
+ {
132
+ "index_pages" => {
133
+ "posts" => {
134
+ "permalink" => "/custom-permalink/"
135
+ }
136
+ }
137
+ }
138
+ end
139
+
140
+ describe("Generator.generate") do
141
+ it("generates a post index page at /custom-permalink/") do
142
+ expect(site.pages[0].url).to eq("/custom-permalink/")
143
+ end
144
+ end
145
+ end
146
+
147
+ context("When custom 'layout' setting for posts index page is provided") do
148
+ let(:overrides) do
149
+ {
150
+ "index_pages" => {
151
+ "posts" => {
152
+ "layout" => "custom-layout"
153
+ }
154
+ }
155
+ }
156
+ end
157
+
158
+ describe("Generator.generate") do
159
+ it("generates a post index page using the custom layout") do
160
+ expect(site.pages.length).to eq(1)
161
+ expect(site.pages[0].content).to include("Custom Layout")
162
+ end
163
+ end
164
+ end
165
+
166
+ context("When default settings for category index pages is provided") do
167
+ let(:overrides) do
168
+ {
169
+ "index_pages" => {
170
+ "categories" => {}
171
+ }
172
+ }
173
+ end
174
+
175
+ describe("Generator.generate") do
176
+ it("generates a single category index page") do
177
+ expect(site.pages.length).to eq(1)
178
+ end
179
+
180
+ it("generates a category index page at /science-fiction/") do
181
+ expect(site.pages[0].url).to eq("/science-fiction/")
182
+ end
183
+ end
184
+ end
185
+
186
+ context("When default settings for tag index pages is provided") do
187
+ let(:overrides) do
188
+ {
189
+ "index_pages" => {
190
+ "tags" => {}
191
+ }
192
+ }
193
+ end
194
+
195
+ describe("Generator.generate") do
196
+ it("generates a single tag index page") do
197
+ expect(site.pages.length).to eq(1)
198
+ end
199
+
200
+ it("generates a tag index page at /star-trek/") do
201
+ expect(site.pages[0].url).to eq("/star-trek/")
202
+ end
203
+ end
204
+ end
205
+ end
@@ -0,0 +1,110 @@
1
+ require "spec_helper"
2
+
3
+ describe(JekyllIndexPages::IndexPage) do
4
+ let(:site_config) do
5
+ Jekyll.configuration({
6
+ "source" => File.expand_path("../fixtures/index-page", __FILE__),
7
+ "destination" => File.expand_path("../dest", __FILE__)
8
+ })
9
+ end
10
+ let(:site) { Jekyll::Site.new(site_config) }
11
+ let(:base) { site.source }
12
+ let(:dir) { "/posts/" }
13
+ let(:label) { "posts" }
14
+ let(:layout) { "default" }
15
+ let(:pagination) do
16
+ JekyllIndexPages::Pagination.new(site.posts.docs.reverse, 2)
17
+ end
18
+ let(:pager) { pagination.pager }
19
+ let(:page) do
20
+ JekyllIndexPages::IndexPage.new(
21
+ site,
22
+ base,
23
+ dir,
24
+ config,
25
+ label,
26
+ layout,
27
+ pager
28
+ )
29
+ end
30
+
31
+ before(:each) do
32
+ site.process
33
+ end
34
+
35
+ context("When no configuration is provided") do
36
+ let(:config) { {} }
37
+
38
+ describe("IndexPage.initialize") do
39
+ context("creates the first index page") do
40
+ let(:pager) do
41
+ pagination.paginate(0)
42
+ pagination.pager
43
+ end
44
+
45
+ it("with default title and excerpt") do
46
+ expect(page.data["title"]).to eq("posts")
47
+ expect(page.data["excerpt"]).to eq("posts")
48
+ end
49
+
50
+ it("listing the first two posts") do
51
+ expect(page.data["pager"]["docs"].length).to eq(2)
52
+ end
53
+
54
+ it("containing pagination data") do
55
+ expect(page.data["pager"]["total_pages"]).to eq(3)
56
+ expect(page.data["pager"]["current_page"]).to eq(1)
57
+ expect(page.data["pager"]["prev_page"]).to eq(0)
58
+ expect(page.data["pager"]["next_page"]).to eq(2)
59
+ end
60
+
61
+ it("and url for next page only") do
62
+ expect(page.data["pager"]["prev_page_url"]).to eq("")
63
+ expect(page.data["pager"]["next_page_url"]).to eq("/posts/2/")
64
+ end
65
+ end
66
+
67
+ context("creates the second index page") do
68
+ let(:dir) { "/posts/2" }
69
+ let(:pager) do
70
+ pagination.paginate(1)
71
+ pagination.pager
72
+ end
73
+
74
+ it("containing urls for previous and next pages") do
75
+ expect(page.data["pager"]["prev_page_url"]).to eq("/posts/1/")
76
+ expect(page.data["pager"]["next_page_url"]).to eq("/posts/3/")
77
+ end
78
+ end
79
+
80
+ context("creates the third index page") do
81
+ let(:dir) { "/posts/3" }
82
+ let(:pager) do
83
+ pagination.paginate(2)
84
+ pagination.pager
85
+ end
86
+
87
+ it("containing url for prev page only") do
88
+ expect(page.data["pager"]["prev_page_url"]).to eq("/posts/2/")
89
+ expect(page.data["pager"]["next_page_url"]).to eq("")
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ context("When configuration is provided") do
96
+ let(:config) do
97
+ {
98
+ "title" => "Star Trek Index",
99
+ "excerpt" => "Star Trek Index"
100
+ }
101
+ end
102
+
103
+ describe("IndexPage.initialize") do
104
+ it("creates the first index page with custom title and excerpt") do
105
+ expect(page.data["title"]).to eq("Star Trek Index")
106
+ expect(page.data["excerpt"]).to eq("Star Trek Index")
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+
3
+ describe(JekyllIndexPages::Pagination) do
4
+ context "When no documents and no results per page are provided" do
5
+ let(:pagination) { JekyllIndexPages::Pagination.new([], 0) }
6
+ let(:pager) { pagination.pager }
7
+
8
+ describe "Pagination.initialize" do
9
+ it "creates an empty pagination object" do
10
+ expect(pagination.total).to eq(0)
11
+ end
12
+ end
13
+ end
14
+
15
+ context "When two documents and one result per page are provided" do
16
+ let(:pagination) { JekyllIndexPages::Pagination.new([true, true], 1) }
17
+ let(:pager) { pagination.pager }
18
+
19
+ before(:each) do
20
+ pagination.paginate(0)
21
+ end
22
+
23
+ describe "Pagination.initialize" do
24
+ it "creates a pagination object with two documents" do
25
+ expect(pagination.total).to eq(2)
26
+ end
27
+ end
28
+
29
+ describe "Pagination.paginate" do
30
+ it "creates a pager object with two pages, one document each" do
31
+ expect(pager.docs.length).to eq(1)
32
+ expect(pager.total_pages).to eq(2)
33
+ expect(pager.current_page).to eq(1)
34
+ expect(pager.prev_page).to eq(0)
35
+ expect(pager.next_page).to eq(2)
36
+ end
37
+ end
38
+ end
39
+
40
+ context "When two documents and two result per page are provided" do
41
+ let(:pagination) { JekyllIndexPages::Pagination.new([true, true], 2) }
42
+ let(:pager) { pagination.pager }
43
+
44
+ before(:each) do
45
+ pagination.paginate(0)
46
+ end
47
+
48
+ describe "Pagination.paginate" do
49
+ it "creates a pager object with a single two document page" do
50
+ expect(pager.docs.length).to eq(2)
51
+ expect(pager.total_pages).to eq(1)
52
+ expect(pager.current_page).to eq(1)
53
+ expect(pager.prev_page).to eq(0)
54
+ expect(pager.next_page).to eq(0)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,4 @@
1
+ require "jekyll"
2
+ require File.expand_path("../lib/jekyll-index-pages", File.dirname(__FILE__))
3
+
4
+ Jekyll.logger.log_level = :error
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-index-pages
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jose Miguel Venegas Mendoza
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ description: |2
56
+ Generates paginated index pages for blog posts, categories, tags, authors
57
+ and collections. It also let you generate a paginated blog post archive.
58
+ email:
59
+ - jvenegasmendoza@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - Gemfile
67
+ - LICENSE.md
68
+ - README.md
69
+ - jekyll-index-pages.gemspec
70
+ - lib/jekyll-index-pages.rb
71
+ - lib/jekyll-index-pages/generator.rb
72
+ - lib/jekyll-index-pages/index-page.rb
73
+ - lib/jekyll-index-pages/pagination.rb
74
+ - lib/jekyll-index-pages/version.rb
75
+ - spec/fixtures/index-page/_config.yml
76
+ - spec/fixtures/index-page/_layouts/categories.html
77
+ - spec/fixtures/index-page/_layouts/custom-layout.html
78
+ - spec/fixtures/index-page/_layouts/default.html
79
+ - spec/fixtures/index-page/_layouts/posts.html
80
+ - spec/fixtures/index-page/_layouts/tags.html
81
+ - spec/fixtures/index-page/_posts/1966-09-08-star-trek-the-original-series.md
82
+ - spec/fixtures/index-page/_posts/1987-09-28-star-trek-the-next-generation.md
83
+ - spec/fixtures/index-page/_posts/1993-01-03-star-trek-deep-space-nine.md
84
+ - spec/fixtures/index-page/_posts/1995-01-16-star-trek-voyager.md
85
+ - spec/fixtures/index-page/_posts/2001-09-26-star-trek-enterprise.md
86
+ - spec/generator_spec.rb
87
+ - spec/index-page_spec.rb
88
+ - spec/pagination_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: https://github.com/rukbotto/jekyll-index-pages
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.6.10
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Index page generator for Jekyll sites.
114
+ test_files:
115
+ - spec/fixtures/index-page/_config.yml
116
+ - spec/fixtures/index-page/_layouts/categories.html
117
+ - spec/fixtures/index-page/_layouts/custom-layout.html
118
+ - spec/fixtures/index-page/_layouts/default.html
119
+ - spec/fixtures/index-page/_layouts/posts.html
120
+ - spec/fixtures/index-page/_layouts/tags.html
121
+ - spec/fixtures/index-page/_posts/1966-09-08-star-trek-the-original-series.md
122
+ - spec/fixtures/index-page/_posts/1987-09-28-star-trek-the-next-generation.md
123
+ - spec/fixtures/index-page/_posts/1993-01-03-star-trek-deep-space-nine.md
124
+ - spec/fixtures/index-page/_posts/1995-01-16-star-trek-voyager.md
125
+ - spec/fixtures/index-page/_posts/2001-09-26-star-trek-enterprise.md
126
+ - spec/generator_spec.rb
127
+ - spec/index-page_spec.rb
128
+ - spec/pagination_spec.rb
129
+ - spec/spec_helper.rb