Skip to content

Commit d3f7e61

Browse files
committed
Rework autopaging implementation + allow custom filters for collections
1 parent e4e1d01 commit d3f7e61

File tree

11 files changed

+165
-331
lines changed

11 files changed

+165
-331
lines changed

lib/jekyll-paginate-v2.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Jekyll::Paginate V2 is a gem built for Jekyll 3 that generates pagiatation for posts, collections, categories and tags.
2-
#
2+
#
33
# It is based on https://github.com/jekyll/jekyll-paginate, the original Jekyll paginator
44
# which was decommissioned in Jekyll 3 release onwards. This code is currently not officially
55
# supported on Jekyll versions < 3.0 (although it might work)
@@ -21,14 +21,10 @@
2121
require "jekyll-paginate-v2/generator/paginationGenerator"
2222
# Files needed for the auto category and tag pages
2323
require "jekyll-paginate-v2/autopages/utils"
24-
require "jekyll-paginate-v2/autopages/defaults"
2524
require "jekyll-paginate-v2/autopages/autoPages"
26-
require "jekyll-paginate-v2/autopages/pages/baseAutoPage"
27-
require "jekyll-paginate-v2/autopages/pages/categoryAutoPage"
28-
require "jekyll-paginate-v2/autopages/pages/collectionAutoPage"
29-
require "jekyll-paginate-v2/autopages/pages/tagAutoPage"
25+
require "jekyll-paginate-v2/autopages/pages/AutoPage"
3026

31-
module Jekyll
27+
module Jekyll
3228
module PaginateV2
3329
end # module PaginateV2
34-
end # module Jekyll
30+
end # module Jekyll

lib/jekyll-paginate-v2/autopages/autoPages.rb

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,43 @@ module PaginateV2::AutoPages
55
# This function is called right after the main generator is triggered by Jekyll
66
# This code is adapted from Stephen Crosby's code https://github.com/stevecrozz
77
def self.create_autopages(site)
8-
98
# Get the configuration for the auto pages
10-
autopage_config = Jekyll::Utils.deep_merge_hashes(DEFAULT, site.config['autopages'] || {})
9+
autopage_config = site.config['autopages'] || {}
1110
pagination_config = Jekyll::Utils.deep_merge_hashes(Jekyll::PaginateV2::Generator::DEFAULT, site.config['pagination'] || {})
1211

12+
# Simply gather all documents across all pages/posts/collections that we have
13+
# and store them under each collection name
14+
coll_docs = Utils.collect_all_docs(site.collections)
15+
# we don't use the posts collection
16+
coll_docs.delete("posts")
17+
coll_names = coll_docs.keys
18+
1319
# If disabled then don't do anything
1420
if !autopage_config['enabled'] || autopage_config['enabled'].nil?
1521
Jekyll.logger.info "AutoPages:","Disabled/Not configured in site.config."
1622
return
1723
end
1824

19-
autopages_log(autopage_config, 'tags', 'categories', 'collections')
20-
21-
# TODO: Should I detect here and disable if we're running the legacy paginate code???!
22-
23-
# Simply gather all documents across all pages/posts/collections that we have
24-
# we could be generating quite a few empty pages but the logic is just vastly simpler than trying to
25-
# figure out what tag/category belong to which collection.
26-
posts_to_use = Utils.collect_all_docs(site.collections)
25+
autopages_log(autopage_config, *coll_names)
2726

28-
###############################################
29-
# Generate the Tag pages if enabled
30-
createtagpage_lambda = lambda do | autopage_tag_config, pagination_config, layout_name, tag, tag_original_name |
31-
site.pages << TagAutoPage.new(site, site.dest, autopage_tag_config, pagination_config, layout_name, tag, tag_original_name)
32-
end
33-
autopage_create(autopage_config, pagination_config, posts_to_use, 'tags', 'tags', createtagpage_lambda) # Call the actual function
34-
27+
coll_docs.each do |coll_name, coll_data|
28+
createcolpage_lambda = lambda do |autopage_col_config, doc|
29+
site.pages << AutoPage.new(site, autopage_col_config, pagination_config, coll_name, doc)
30+
end
3531

36-
###############################################
37-
# Generate the category pages if enabled
38-
createcatpage_lambda = lambda do | autopage_cat_config, pagination_config, layout_name, category, category_original_name |
39-
site.pages << CategoryAutoPage.new(site, site.dest, autopage_cat_config, pagination_config, layout_name, category, category_original_name)
40-
end
41-
autopage_create(autopage_config, pagination_config,posts_to_use, 'categories', 'categories', createcatpage_lambda) # Call the actual function
42-
43-
###############################################
44-
# Generate the Collection pages if enabled
45-
createcolpage_lambda = lambda do | autopage_col_config, pagination_config, layout_name, coll_name, coll_original_name |
46-
site.pages << CollectionAutoPage.new(site, site.dest, autopage_col_config, pagination_config, layout_name, coll_name, coll_original_name)
32+
autopage_create(autopage_config, coll_name, coll_data, createcolpage_lambda)
4733
end
48-
autopage_create(autopage_config, pagination_config,posts_to_use, 'collections', '__coll', createcolpage_lambda) # Call the actual function
49-
5034
end # create_autopages
5135

52-
5336
# STATIC: this function actually performs the steps to generate the autopages. It uses a lambda function to delegate the creation of the individual
5437
# page types to the calling code (this way all features can reuse the logic).
5538
#
56-
def self.autopage_create(autopage_config, pagination_config, posts_to_use, configkey_name, indexkey_name, createpage_lambda )
57-
if !autopage_config[configkey_name].nil?
39+
def self.autopage_create(autopage_config, configkey_name, coll_data, createpage_lambda)
40+
unless autopage_config[configkey_name].nil?
5841
ap_sub_config = autopage_config[configkey_name]
59-
if ap_sub_config ['enabled']
60-
# Roll through all documents in the posts collection and extract the tags
61-
index_keys = Utils.ap_index_posts_by(posts_to_use, indexkey_name) # Cannot use just the posts here, must use all things.. posts, collections...
62-
63-
index_keys.each do |index_key, value|
64-
# Iterate over each layout specified in the config
65-
ap_sub_config ['layouts'].each do | layout_name |
66-
# Use site.dest here as these pages are never created in the actual source but only inside the _site folder
67-
createpage_lambda.call(ap_sub_config, pagination_config, layout_name, index_key, value[-1]) # the last item in the value array will be the display name
68-
end
42+
if ap_sub_config['enabled']
43+
coll_data.each do |doc|
44+
createpage_lambda.call(ap_sub_config, doc)
6945
end
7046
end
7147
end

lib/jekyll-paginate-v2/autopages/defaults.rb

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
module Jekyll
2+
module PaginateV2::AutoPages
3+
4+
class AutoPage < Jekyll::Page
5+
def initialize(site, autopage_config, pagination_config, coll_name, doc)
6+
@site = site
7+
@base = site.dest
8+
@name = 'index.html'
9+
10+
layout_dir = '_layouts'
11+
12+
# use layout in document priority, or specify one for it in the autopage config section
13+
# you need to have at least one of these specified. the autopage layout will be a fallback if none are sepcified
14+
layout_name = doc['layout'] || autopage_config['layout']
15+
layout_name += '.html'
16+
17+
if coll_name.eql?('categories')
18+
index_key = 'category'
19+
elsif coll_name.eql?('tags')
20+
index_key = 'tag'
21+
else
22+
index_key = autopage_config['indexkey'].nil? ? coll_name : autopage_config['indexkey']
23+
end
24+
25+
# used to do the pagination matching
26+
# defined in frontmatter like, author: Cherryleafroad
27+
# which matches the same in posts
28+
item = doc[index_key]
29+
30+
# Path is only used by the convertible module and accessed below when calling read_yaml
31+
# Handling themes stored in a gem
32+
@path = if site.in_theme_dir(site.source) == site.source # we're in a theme
33+
site.in_theme_dir(site.source, layout_dir, layout_name)
34+
else
35+
site.in_source_dir(site.source, layout_dir, layout_name)
36+
end
37+
38+
self.process(@name) # Creates the base name and extension
39+
self.read_yaml(File.join(site.source, layout_dir), layout_name)
40+
41+
# Merge the config with any config that might already be defined in the layout
42+
pagination_layout_config = Jekyll::Utils.deep_merge_hashes(pagination_config, self.data['pagination'] || {})
43+
44+
# Read any possible autopage overrides in the layout page
45+
autopage_layout_config = Jekyll::Utils.deep_merge_hashes(autopage_config, self.data['autopages'] || {})
46+
47+
# Construct the title
48+
# as usual, you have 3 different options for title
49+
# you can use cased to get upper or lower case on the key if using ap config title
50+
item_cased = autopage_config['cased'] ? item : item.downcase
51+
ap_title = autopage_config['title']&.gsub(":#{index_key}", item_cased)
52+
page_title = ap_title || autopage_layout_config['title'] || doc['title']
53+
54+
# NOTE: Should we set this before calling read_yaml as that function validates the permalink structure
55+
# use doc permalink if available
56+
# Get permalink structure
57+
# ap permalink setting overrides document setting
58+
# It MUST be a directory notation NOT a file ending
59+
ap_perma = autopage_config['permalink']&.gsub(":#{index_key}", item_cased)
60+
permalink = ap_perma || doc.url.to_s
61+
self.data['permalink'] = permalink
62+
@url = File.join(permalink, @name)
63+
@dir = permalink
64+
65+
# set up pagination
66+
if coll_name.eql?('categories') or coll_name.eql?('tags')
67+
pagination_layout_config[index_key] = doc[index_key]
68+
else
69+
# user definable different collections, or default
70+
pagination_layout_config['collection'] = autopage_config['collection'] || 'posts'
71+
pagination_layout_config[index_key] = doc[index_key]
72+
pagination_layout_config['filter_coll_by'] = index_key
73+
end
74+
75+
# use ap config layout or doc layout. ap overrides doc layout
76+
self.data['layout'] = autopage_config['layout'] || doc.data['layout']
77+
# use the docs title first if available
78+
self.data['title'] = page_title
79+
# inject pagination variable
80+
self.data['pagination'] = pagination_layout_config # Store the pagination configuration
81+
82+
# inject the rest of the frontmatter data into the page as if it was originating from the original page
83+
ignore_keys = %w[layout title permalink autopages]
84+
# include all data unless it's a disallowed key
85+
doc.data.each do |key, value|
86+
self.data[key] = value unless ignore_keys.include?(key)
87+
end
88+
89+
unless doc.content.nil?
90+
# run renderer on document to render out data to get the rendered content
91+
doc.renderer.run
92+
# set content separately since it's not part of data
93+
self.data['content'] = doc.content
94+
end
95+
96+
# Add the auto page flag in there to be able to detect the page (necessary when figuring out where to load it from)
97+
# TODO: Need to re-think this variable!!!
98+
self.data['autopage'] = {'layout_path' => File.join( layout_dir, layout_name ), 'display_name' => item }
99+
100+
data.default_proc = proc do |_, key|
101+
site.frontmatter_defaults.find(File.join(layout_dir, layout_name), type, key)
102+
end
103+
104+
# Trigger a page event
105+
#Jekyll::Hooks.trigger :pages, :post_init, self
106+
end #function initialize
107+
end #class BaseAutoPage
108+
end # module PaginateV2
109+
end # module Jekyll

lib/jekyll-paginate-v2/autopages/pages/baseAutoPage.rb

Lines changed: 0 additions & 60 deletions
This file was deleted.

lib/jekyll-paginate-v2/autopages/pages/categoryAutoPage.rb

Lines changed: 0 additions & 31 deletions
This file was deleted.

lib/jekyll-paginate-v2/autopages/pages/collectionAutoPage.rb

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)