Skip to content

Commit

Permalink
Autopages adding original tag name as liquid variable, fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
sverrirs committed Feb 3, 2017
1 parent d8c8b55 commit 687e423
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 30 deletions.
17 changes: 17 additions & 0 deletions README-AUTOPAGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ Example of a simple autopage layout can be seen in Example 3 [examples/03-tags/_
The layout does not need anything special beyond the normal pagination logic (e.g. `for post in paginator.posts` and then the next/prev arrows). You can either name the layouts by the default names (see site configuration section above) or give them a custom name and add them to the `layouts:` configuration for the relevant type of autopage.

### Obtaining the original Tag or Category name
Internally the autopage system will trim and downcase the indexing key (tag, category or collection name). To retrieve the original name of the index key you can use the `autopages.display_name` liquid variable.

``` html
<h1 class="page-heading">All pages tagged with <b>{% if page.autopages %}{{page.autopages.display_name}}{% endif %}</b></h1>
```

This variable returns the untouched key value. As an example if your site uses the tag `Science-Fiction` then

```
page.tag = "science-fiction"
page.autopages.display_name = "Science-Fiction"
```
See [#6](https://github.com/sverrirs/jekyll-paginate-v2/issues/6) for more information.
## Advanced configuration
You can customize the look an feel of the permalink structure and the title for the auto-pages. You can also add front-matter `pagination` configuration to the layout pages you're using to specify things like sorting, filtering and all the other configuration options that are available in the normal pagination generator.
Expand Down
2 changes: 1 addition & 1 deletion examples/03-tags/_layouts/autopage_cat.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<div class="home">

<h1 class="page-heading">AutoPage Categories</h1>
<h1 class="page-heading">AutoPage Category <b>{% if page.autopages %}{{page.autopage.display_name}}{% endif %}</b></h1>

Omg! all the categories in one paginated place

Expand Down
2 changes: 1 addition & 1 deletion examples/03-tags/_layouts/autopage_collection.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<div class="home">

<h1 class="page-heading">AutoPage Collections</h1>
<h1 class="page-heading">AutoPage Collection <b>{% if page.autopages %}{{page.autopages.display_name}}{% endif %}</b></h1>

AUTO CREATED FOR ALL COLLECTIONS IN THE SITE

Expand Down
2 changes: 1 addition & 1 deletion examples/03-tags/_layouts/autopage_collections_tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<div class="home">

<h1 class="page-heading">AutoPage Collection ALL : Tags</h1>
<h1 class="page-heading">AutoPage Collection ALL : Tag <b>{% if page.autopages %}{{page.autopages.display_name}}{% endif %}</b></h1>

This page is automagically created and paginated for each tag available in the posts on this site

Expand Down
2 changes: 1 addition & 1 deletion examples/03-tags/_layouts/autopage_tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<div class="home">

<h1 class="page-heading">AutoPage Tags</h1>
<h1 class="page-heading">AutoPage for Tag <b>{% if page.autopages %}{{page.autopages.display_name}}{% endif %}</b></h1>

This page is automagically created and paginated for each tag available in the posts on this site

Expand Down
18 changes: 9 additions & 9 deletions lib/jekyll-paginate-v2/autopages/autoPages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ module PaginateV2::AutoPages

###############################################
# Generate the Tag pages if enabled
createtagpage_lambda = lambda do | autopage_tag_config, pagination_config, layout_name, tag |
site.pages << TagAutoPage.new(site, site.dest, autopage_tag_config, pagination_config, layout_name, tag)
createtagpage_lambda = lambda do | autopage_tag_config, pagination_config, layout_name, tag, tag_original_name |
site.pages << TagAutoPage.new(site, site.dest, autopage_tag_config, pagination_config, layout_name, tag, tag_original_name)
end
autopage_create(autopage_config, pagination_config, posts_to_use, 'tags', 'tags', createtagpage_lambda) # Call the actual function


###############################################
# Generate the category pages if enabled
createcatpage_lambda = lambda do | autopage_cat_config, pagination_config, layout_name, category |
site.pages << CategoryAutoPage.new(site, site.dest, autopage_cat_config, pagination_config, layout_name, category)
createcatpage_lambda = lambda do | autopage_cat_config, pagination_config, layout_name, category, category_original_name |
site.pages << CategoryAutoPage.new(site, site.dest, autopage_cat_config, pagination_config, layout_name, category, category_original_name)
end
autopage_create(autopage_config, pagination_config,posts_to_use, 'categories', 'categories', createcatpage_lambda) # Call the actual function

###############################################
# Generate the Collection pages if enabled
createcolpage_lambda = lambda do | autopage_col_config, pagination_config, layout_name, coll_name |
site.pages << CollectionAutoPage.new(site, site.dest, autopage_col_config, pagination_config, layout_name, coll_name)
createcolpage_lambda = lambda do | autopage_col_config, pagination_config, layout_name, coll_name, coll_original_name |
site.pages << CollectionAutoPage.new(site, site.dest, autopage_col_config, pagination_config, layout_name, coll_name, coll_original_name)
end
autopage_create(autopage_config, pagination_config,posts_to_use, 'collections', '__coll', createcolpage_lambda) # Call the actual function

Expand All @@ -58,13 +58,13 @@ def self.autopage_create(autopage_config, pagination_config, posts_to_use, confi
Jekyll.logger.info "AutoPages:","Generating #{configkey_name} pages"

# Roll through all documents in the posts collection and extract the tags
index_keys = Utils.index_posts_by(posts_to_use, indexkey_name).keys # Cannot use just the posts here, must use all things.. posts, collections...
index_keys = Utils.ap_index_posts_by(posts_to_use, indexkey_name) # Cannot use just the posts here, must use all things.. posts, collections...

index_keys.each do |index_key|
index_keys.each do |index_key, value|
# Iterate over each layout specified in the config
ap_sub_config ['layouts'].each do | layout_name |
# Use site.dest here as these pages are never created in the actual source but only inside the _site folder
createpage_lambda.call(ap_sub_config, pagination_config, layout_name, index_key)
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
end
end
else
Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll-paginate-v2/autopages/pages/baseAutoPage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Jekyll
module PaginateV2::AutoPages

class BaseAutoPage < Jekyll::Page
def initialize(site, base, autopage_config, pagination_config, layout_name, set_autopage_data_lambda, get_autopage_permalink_lambda, get_autopage_title_lambda)
def initialize(site, base, autopage_config, pagination_config, layout_name, set_autopage_data_lambda, get_autopage_permalink_lambda, get_autopage_title_lambda, display_name)
@site = site
@base = base
@name = 'index.html'
Expand Down Expand Up @@ -46,7 +46,7 @@ def initialize(site, base, autopage_config, pagination_config, layout_name, set_

# Add the auto page flag in there to be able to detect the page (necessary when figuring out where to load it from)
# TODO: Need to re-think this variable!!!
self.data['autopage'] = {"layout_path" => File.join( layout_dir, layout_name ) }
self.data['autopage'] = {"layout_path" => File.join( layout_dir, layout_name ), 'display_name' => display_name.to_s }

data.default_proc = proc do |_, key|
site.frontmatter_defaults.find(File.join(layout_dir, layout_name), type, key)
Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll-paginate-v2/autopages/pages/categoryAutoPage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Jekyll
module PaginateV2::AutoPages

class CategoryAutoPage < BaseAutoPage
def initialize(site, base, autopage_config, pagination_config, layout_name, category)
def initialize(site, base, autopage_config, pagination_config, layout_name, category, category_name)

# Construc the lambda function to set the config values
# this function received the pagination config hash and manipulates it
Expand All @@ -19,7 +19,7 @@ def initialize(site, base, autopage_config, pagination_config, layout_name, cate
end

# Call the super constuctor with our custom lambda
super(site, base, autopage_config, pagination_config, layout_name, set_autopage_data_lambda, get_autopage_permalink_lambda, get_autopage_title_lambda)
super(site, base, autopage_config, pagination_config, layout_name, set_autopage_data_lambda, get_autopage_permalink_lambda, get_autopage_title_lambda, category_name)

end #function initialize

Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll-paginate-v2/autopages/pages/collectionAutoPage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Jekyll
module PaginateV2::AutoPages

class CollectionAutoPage < BaseAutoPage
def initialize(site, base, autopage_config, pagination_config, layout_name, collection)
def initialize(site, base, autopage_config, pagination_config, layout_name, collection, collection_name)

# Construc the lambda function to set the config values
# this function received the pagination config hash and manipulates it
Expand All @@ -19,7 +19,7 @@ def initialize(site, base, autopage_config, pagination_config, layout_name, coll
end

# Call the super constuctor with our custom lambda
super(site, base, autopage_config, pagination_config, layout_name, set_autopage_data_lambda, get_autopage_permalink_lambda, get_autopage_title_lambda)
super(site, base, autopage_config, pagination_config, layout_name, set_autopage_data_lambda, get_autopage_permalink_lambda, get_autopage_title_lambda, collection_name)

end #function initialize

Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll-paginate-v2/autopages/pages/tagAutoPage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Jekyll
module PaginateV2::AutoPages

class TagAutoPage < BaseAutoPage
def initialize(site, base, autopage_config, pagination_config, layout_name, tag)
def initialize(site, base, autopage_config, pagination_config, layout_name, tag, tag_name)

# Construc the lambda function to set the config values,
# this function received the pagination config hash and manipulates it
Expand All @@ -19,7 +19,7 @@ def initialize(site, base, autopage_config, pagination_config, layout_name, tag)
end

# Call the super constuctor with our custom lambda
super(site, base, autopage_config, pagination_config, layout_name, set_autopage_data_lambda, get_autopage_permalink_lambda, get_autopage_title_lambda)
super(site, base, autopage_config, pagination_config, layout_name, set_autopage_data_lambda, get_autopage_permalink_lambda, get_autopage_title_lambda, tag_name)

end #function initialize

Expand Down
14 changes: 7 additions & 7 deletions lib/jekyll-paginate-v2/autopages/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def self.collect_all_docs(site_collections)
return coll
end

def self.index_posts_by(all_posts, index_key)
def self.ap_index_posts_by(all_posts, index_key)
return nil if all_posts.nil?
return all_posts if index_key.nil?
index = {}
Expand All @@ -52,16 +52,16 @@ def self.index_posts_by(all_posts, index_key)
end

for key in post_data
key = key.downcase.strip
key = key.strip
# If the key is a delimetered list of values
# (meaning the user didn't use an array but a string with commas)
for k_split in key.split(/;|,/)
k_split = k_split.downcase.strip #Clean whitespace and junk
for raw_k_split in key.split(/;|,/)
k_split = raw_k_split.to_s.downcase.strip #Clean whitespace and junk
if !index.has_key?(k_split)
index[k_split.to_s] = []
# Need to store the original key value here so that I can present it to the users as a page variable they can use (unmodified, e.g. tags not being 'sci-fi' but "Sci-Fi")
# Also, only interested in storing all the keys not the pages in this case
index[k_split.to_s] = [k_split.to_s, raw_k_split.to_s]
end
# TODO: Need to store the original key value here so that I can present it to the users as a page variable they can use (unmodified, e.g. tags not being 'sci-fi' but "Sci-Fi")
index[k_split.to_s] << post
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions lib/jekyll-paginate-v2/generator/paginationPage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ def initialize(page_to_copy, ignored)
self.data = Jekyll::Utils.deep_merge_hashes( page_to_copy.data, {} )
if !page_to_copy.data['autopage']
self.content = page_to_copy.content
else
# If the page is an auto page then migrate the necessary autopage info across into the
# new pagination page (so that users can get the correct keys etc)
if( page_to_copy.data['autopage'].has_key?('display_name') )
self.data['autopages'] = Jekyll::Utils.deep_merge_hashes( page_to_copy.data['autopage'], {} )
puts( "------ AUTOPAGE -------")
puts( self.data['autopages'] )
end
end

# Perform some validation that is also performed in Jekyll::Page
Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll-paginate-v2/version.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Jekyll
module PaginateV2
VERSION = "1.7.0"
VERSION = "1.7.1"
# When modifying remember to issue a new tag command in git before committing, then push the new tag
# git tag -a v1.7.0 -m "Gem v1.7.0"
# git tag -a v1.7.1 -m "Gem v1.7.1"
# git push origin --tags
end # module PaginateV2
end # module Jekyll

0 comments on commit 687e423

Please sign in to comment.