eigenmethod-jekyll-paginate 0.0.3 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2439546d6f65d004f6e60f467479c18cd7b8a1c3
4
- data.tar.gz: 6be6d4fc5f2ef8f79e22aab34f0e05219205b7f6
3
+ metadata.gz: 1253c0b026e77d4e9285969561ccb1b89c0825d8
4
+ data.tar.gz: be4f8e179aac6f65ae2a91dc241f4308f92defa0
5
5
  SHA512:
6
- metadata.gz: 65f5c2e410bc4c649a06aeb6ad061b889e90f27eb9393cd122ecac4f5e018470b523e81ab3437bd4e4660b913bbda08461cbc479400a28ae22bcdc10f4210d83
7
- data.tar.gz: 484b452fa6c775716b0ae84ca45873db817c5d5fc54200ca574774fdb65fe87b4581c1c217d8e8df70f37b4a0dc286983d24cf1c38b7bc2e3eea2519c97c1e9a
6
+ metadata.gz: 224deeeecd15a871e943c033e2940f79cd528d746293498302dd6d39f803d52ef7e16062fc0443b3be7d9daf66cfc19b8d742a3f9fdb826abac23d5967e75bca
7
+ data.tar.gz: a153d4de143de6e9d1501353c3341b50c7c64adf26ed7df34e3dcd6ce96cddf70176f83b5ccb3a3bc01cd218af8dfc11283765059cb3f76360ac0fd3f530cbcd
@@ -0,0 +1,7 @@
1
+ require "pager"
2
+ require "pagination"
3
+
4
+ module Jekyll
5
+ module Paginate
6
+ end
7
+ end
data/pager.rb ADDED
@@ -0,0 +1,137 @@
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
+ format = format.sub(':num', num_page.to_s)
68
+ ensure_leading_slash(format)
69
+ end
70
+
71
+ # Static: Return a String version of the input which has a leading slash.
72
+ # If the input already has a forward slash in position zero, it will be
73
+ # returned unchanged.
74
+ #
75
+ # path - a String path
76
+ #
77
+ # Returns the path with a leading slash
78
+ def self.ensure_leading_slash(path)
79
+ path[0..0] == "/" ? path : "/#{path}"
80
+ end
81
+
82
+ # Static: Return a String version of the input without a leading slash.
83
+ #
84
+ # path - a String path
85
+ #
86
+ # Returns the input without the leading slash
87
+ def self.remove_leading_slash(path)
88
+ ensure_leading_slash(path)[1..-1]
89
+ end
90
+
91
+ # Initialize a new Pager.
92
+ #
93
+ # site - the Jekyll::Site object
94
+ # page - The Integer page number.
95
+ # all_posts - The Array of all the site's Posts.
96
+ # num_pages - The Integer number of pages or nil if you'd like the number
97
+ # of pages calculated.
98
+ def initialize(site, page, all_posts, num_pages = nil)
99
+ @page = page
100
+ @per_page = site.config['paginate'].to_i
101
+ @total_pages = num_pages || Pager.calculate_pages(all_posts, @per_page)
102
+
103
+ if @page > @total_pages
104
+ raise RuntimeError, "page number can't be greater than total pages: #{@page} > #{@total_pages}"
105
+ end
106
+
107
+ init = (@page - 1) * @per_page
108
+ offset = (init + @per_page - 1) >= all_posts.size ? all_posts.size : (init + @per_page - 1)
109
+
110
+ @total_posts = all_posts.size
111
+ @posts = all_posts[init..offset]
112
+ @previous_page = @page != 1 ? @page - 1 : nil
113
+ @previous_page_path = Pager.paginate_path(site, @previous_page)
114
+ @next_page = @page != @total_pages ? @page + 1 : nil
115
+ @next_page_path = Pager.paginate_path(site, @next_page)
116
+ end
117
+
118
+ # Convert this Pager's data to a Hash suitable for use by Liquid.
119
+ #
120
+ # Returns the Hash representation of this Pager.
121
+ def to_liquid
122
+ {
123
+ 'page' => page,
124
+ 'per_page' => per_page,
125
+ 'posts' => posts,
126
+ 'total_posts' => total_posts,
127
+ 'total_pages' => total_pages,
128
+ 'previous_page' => previous_page,
129
+ 'previous_page_path' => previous_page_path,
130
+ 'next_page' => next_page,
131
+ 'next_page_path' => next_page_path
132
+ }
133
+ end
134
+
135
+ end
136
+ end
137
+ end
data/pagination.rb ADDED
@@ -0,0 +1,87 @@
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 = 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" => <Number>,
34
+ # "per_page" => <Number>,
35
+ # "posts" => [<Post>],
36
+ # "total_posts" => <Number>,
37
+ # "total_pages" => <Number>,
38
+ # "previous_page" => <Number>,
39
+ # "next_page" => <Number> }}
40
+ def paginate(site, page)
41
+ all_posts = site.site_payload['site']['posts']
42
+ all_posts = all_posts.reject { |p| p['hidden'] }
43
+ all_posts = all_posts.reject { |p| p['language'] != site.config['languages'][0] }
44
+ pages = Pager.calculate_pages(all_posts, site.config['paginate'].to_i)
45
+ (1..pages).each do |num_page|
46
+ pager = Pager.new(site, num_page, all_posts, pages)
47
+ if num_page > 1
48
+ newpage = Page.new(site, site.source, page.dir, page.name)
49
+ newpage.pager = pager
50
+ newpage.dir = Pager.paginate_path(site, num_page)
51
+ site.pages << newpage
52
+ else
53
+ page.pager = pager
54
+ end
55
+ end
56
+ end
57
+
58
+ # Static: Fetch the URL of the template page. Used to determine the
59
+ # path to the first pager in the series.
60
+ #
61
+ # site - the Jekyll::Site object
62
+ #
63
+ # Returns the url of the template page
64
+ def self.first_page_url(site)
65
+ if page = Pagination.new.template_page(site)
66
+ page.url
67
+ else
68
+ nil
69
+ end
70
+ end
71
+
72
+ # Public: Find the Jekyll::Page which will act as the pager template
73
+ #
74
+ # site - the Jekyll::Site object
75
+ #
76
+ # Returns the Jekyll::Page which will act as the pager template
77
+ def template_page(site)
78
+ site.pages.dup.select do |page|
79
+ Pager.pagination_candidate?(site.config, page)
80
+ end.sort do |one, two|
81
+ two.path.size <=> one.path.size
82
+ end.first
83
+ end
84
+
85
+ end
86
+ end
87
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eigenmethod-jekyll-paginate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fedor Ortyanov
@@ -9,71 +9,17 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2016-02-24 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.0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '2.0'
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.5'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '1.5'
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
- - !ruby/object:Gem::Dependency
56
- name: rspec
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '3.0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '3.0'
12
+ dependencies: []
69
13
  description:
70
- email:
71
- - ortyanovfi@gmail.com
14
+ email: ortyanovfi@gmail.com
72
15
  executables: []
73
16
  extensions: []
74
17
  extra_rdoc_files: []
75
- files: []
76
- homepage:
18
+ files:
19
+ - eigenmethod-jekyll-paginate.rb
20
+ - pager.rb
21
+ - pagination.rb
22
+ homepage: http://rubygems.org/gems/eigenmethod-jekyll-paginate
77
23
  licenses:
78
24
  - MIT
79
25
  metadata: {}