jekyll-paginate-multiple 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/jekyll-paginate-multiple.rb +155 -0
  3. metadata +87 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d3d9fd0b4566f022d7a5e5a5138cce27c1a148ca
4
+ data.tar.gz: 9ccb849fd24b2a02940f76bb5d3cdaf7eccec743
5
+ SHA512:
6
+ metadata.gz: f017a811d0b8545f38425c6ba3c59641af381049b199e44ac6d8e9636b00142e3ed42691b075b12067b0fc8b87c23ddc413a200ed52c5be50e4f162616b5abfd
7
+ data.tar.gz: 59764311eb294112aae03aa8c552f2232d151f519832df0af14703db33f46d9c4acba5ce4aa9125649ef355a834c4684f2899c008768b5eb0520daabf04a223c
@@ -0,0 +1,155 @@
1
+ require 'jekyll'
2
+
3
+ module Jekyll
4
+ module Paginate
5
+ module Multiple
6
+
7
+ class MultiplePagination < Jekyll::Paginate::Pagination
8
+ safe true
9
+ priority :lowest
10
+
11
+ # Generate paginated pages for multiple sub directories.
12
+ #
13
+ # Example config:
14
+ #
15
+ # paginate_multiple:
16
+ # - paginate: 10
17
+ # paginate_path: '/blog/page:num/'
18
+ # sub_dir: '/en'
19
+ # - paginate: 10
20
+ # paginate_path: '/blog/de/page:num/'
21
+ # sub_dir: '/de'
22
+ #
23
+ # site - The Site.
24
+ #
25
+ # Returns nothing
26
+ def generate(site)
27
+ paginate_multiple = site.config['paginate_multiple']
28
+
29
+ unless paginate_multiple.nil?
30
+ if paginate_multiple.kind_of?(Array)
31
+
32
+ # save original paginate and paginate_path so they can be restored
33
+ original_paginate = site.config['paginate']
34
+ original_paginate_path = site.config['paginate_path']
35
+
36
+ paginate_multiple.each_with_index do |pagination, index|
37
+ paginate = pagination['paginate']
38
+ paginate_path = pagination['paginate_path']
39
+ sub_dir = pagination['sub_dir']
40
+
41
+ if paginate && paginate_path && sub_dir
42
+ # set paginate and paginate_path for each entry for the Jekyll::Paginate::Pager
43
+ site.config['paginate'] = paginate
44
+ site.config['paginate_path'] = paginate_path
45
+
46
+ template = template_page(site)
47
+
48
+ if template
49
+ posts = posts_for_sub_dir(site, sub_dir)
50
+ paginate(site, template, posts, paginate)
51
+ else
52
+ Jekyll.logger.warn 'Pagination Multiple:', "Cannot find an index.html to use as the" +
53
+ "pagination template for pagination path: '#{pagination['paginate_path']}'. "
54
+ "Skipping pagination for entry #{index}."
55
+ end
56
+ else
57
+ Jekyll.logger.warn 'Pagination Multiple:', "For each 'pagination_multiple' entry, 'paginate', " +
58
+ "'paginate_path' and 'sub_dir' must be set. Skipping pagination for entry #{index}."
59
+ end
60
+ end
61
+
62
+ # restore original paginate and paginate_path
63
+ site.config['paginate_path'] = original_paginate_path
64
+ site.config['paginate'] = original_paginate
65
+
66
+ else
67
+ Jekyll.logger.warn 'Pagination Multiple:', "Config parameter 'pagination_multiple' must be an array. " +
68
+ "Skipping pagination."
69
+ end
70
+
71
+ end
72
+ end
73
+
74
+ # Returns all the posts in a given sub directory.
75
+ #
76
+ # site - The Site.
77
+ # sub_dir - The sub directory of the posts.
78
+ #
79
+ # Returns the posts in the sub directory
80
+ def posts_for_sub_dir(site, sub_dir)
81
+ site.site_payload['site']['posts'].reject{ |post| !post_is_in_sub_dir(post, sub_dir) || post['hidden'] }
82
+ end
83
+
84
+ # Paginates blog posts with a given page template.
85
+ #
86
+ # site - The Site.
87
+ # template - The template page for the pagination.
88
+ # posts - All the posts that should be paginated for the template.
89
+ # posts_per_page - Number of posts a page should have.
90
+ #
91
+ # Returns nothing
92
+ def paginate(site, template, posts, posts_per_page)
93
+ total_pages = Pager.calculate_pages(posts, posts_per_page.to_i)
94
+ (1..total_pages).each do |page_num|
95
+ current_page = page_num > 1 ? create_page(site, template, page_num) : template
96
+ pager = Pager.new(site, page_num, posts, total_pages)
97
+ current_page.pager = pager
98
+ end
99
+ end
100
+
101
+ # Creates a paginated page from a given template.
102
+ #
103
+ # site - The Site.
104
+ # template - The template page.
105
+ # page_num - The page number.
106
+ #
107
+ # Returns the created page
108
+ def create_page(site, template, page_num)
109
+ page = Page.new(site, site.source, template.dir, template.name)
110
+ page.dir = Pager.paginate_path(site, page_num)
111
+ site.pages << page
112
+ page
113
+ end
114
+
115
+ # Checks whether a post is in a given sub directory in '_posts'.
116
+ #
117
+ # post - The post.
118
+ # sub_dir - The sub directory
119
+ #
120
+ # Returns true if the post is in the sub directory, false otherwise
121
+ def post_is_in_sub_dir(post, sub_dir)
122
+ sub_dir = ensure_leading_slash(sub_dir)
123
+ sub_dir = ensure_no_trailing_slash(sub_dir)
124
+
125
+ posts_dir = '_posts'
126
+ post_path = post.relative_path
127
+
128
+ post_sub_dir = post_path[posts_dir.length..post.relative_path.rindex('/')-1]
129
+
130
+ sub_dir == post_sub_dir
131
+ end
132
+
133
+ # Ensures a directory has a leading slash.
134
+ #
135
+ # dir - The directory.
136
+ #
137
+ # Returns the directory with a leading slash
138
+ def ensure_leading_slash(dir)
139
+ dir[0] == '/' ? dir : '/' + dir
140
+ end
141
+
142
+ # Ensures a directory has no trailing slash.
143
+ #
144
+ # dir - The directory.
145
+ #
146
+ # Returns the directory with no trailing slash
147
+ def ensure_no_trailing_slash(dir)
148
+ dir[-1] != '/' ? dir : dir[0..-2]
149
+ end
150
+
151
+ end
152
+
153
+ end
154
+ end
155
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-paginate-multiple
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Georg Schmidl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-06 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: '2.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '2.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jekyll-paginate
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Jekyll pagination generator for multiple paths
56
+ email:
57
+ - georg.schmidl@scandio.de
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/jekyll-paginate-multiple.rb
63
+ homepage: https://github.com/scandio/jekyll-paginate-multiple
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.5.0
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Jekyll pagination generator for multiple paths
87
+ test_files: []