Skip to content

Commit

Permalink
Allow using :xxx_name in autopage titles (fixes sverrirs#225).
Browse files Browse the repository at this point in the history
  • Loading branch information
solemnwarning committed Oct 22, 2022
1 parent e4e1d01 commit be5bb89
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 20 deletions.
10 changes: 5 additions & 5 deletions README-AUTOPAGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ autopages:
# Optional, the list of layouts that should be processed for every category found in the site
layouts:
- 'autopage_category.html'
# Optional, the title that each category paginate page should get (:cat is replaced by the Category name)
title: 'Posts in category :cat'
# Optional, the title that each category paginate page should get (:cat_name is replaced by the Category name)
title: 'Posts in category :cat_name'
# Optional, the permalink for the pagination page (:cat is replaced),
# the pagination permalink path is then appended to this permalink structure
permalink: '/category/:cat'
Expand All @@ -52,8 +52,8 @@ autopages:
collections:
layouts:
- 'autopage_collection.html'
title: 'Posts in collection :coll' # :coll is replaced by the collection name
permalink: '/collection/:coll'
title: 'Posts in collection :coll' # :coll_name is replaced by the collection name
permalink: '/collection/:coll_name'
silent: false
slugify:
mode: 'default' # :coll is slugified.
Expand All @@ -63,7 +63,7 @@ autopages:
tags:
layouts:
- 'autopage_tags.html'
title: 'Posts tagged with :tag' # :tag is replaced by the tag name
title: 'Posts tagged with :tag_name' # :tag_name is replaced by the tag name
permalink: '/tag/:tag'
silent: false
slugify:
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 @@ -14,11 +14,11 @@ def initialize(site, base, autopage_config, pagination_config, layout_name, cate
end

get_autopage_permalink_lambda = lambda do |permalink_pattern|
return Utils.format_cat_macro(permalink_pattern, category, slugify_config)
return Utils.format_cat_macro(permalink_pattern, category, category_name, slugify_config)
end

get_autopage_title_lambda = lambda do |title_pattern|
return Utils.format_cat_macro(title_pattern, category, slugify_config)
return Utils.format_cat_macro(title_pattern, category, category_name, slugify_config)
end

# Call the super constuctor with our custom lambda
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 @@ -14,11 +14,11 @@ def initialize(site, base, autopage_config, pagination_config, layout_name, coll
end

get_autopage_permalink_lambda = lambda do |permalink_pattern|
return Utils.format_coll_macro(permalink_pattern, collection, slugify_config)
return Utils.format_coll_macro(permalink_pattern, collection, collection_name, slugify_config)
end

get_autopage_title_lambda = lambda do |title_pattern|
return Utils.format_coll_macro(title_pattern, collection, slugify_config)
return Utils.format_coll_macro(title_pattern, collection, collection_name, slugify_config)
end

# Call the super constuctor with our custom lambda
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 @@ -14,11 +14,11 @@ def initialize(site, base, autopage_config, pagination_config, layout_name, tag,
end

get_autopage_permalink_lambda = lambda do |permalink_pattern|
return Utils.format_tag_macro(permalink_pattern, tag, slugify_config)
return Utils.format_tag_macro(permalink_pattern, tag, tag_name, slugify_config)
end

get_autopage_title_lambda = lambda do |title_pattern|
return Utils.format_tag_macro(title_pattern, tag, slugify_config)
return Utils.format_tag_macro(title_pattern, tag, tag_name, slugify_config)
end

# Call the super constuctor with our custom lambda
Expand Down
41 changes: 33 additions & 8 deletions lib/jekyll-paginate-v2/autopages/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,53 @@ module PaginateV2::AutoPages

class Utils

# Static: returns a fully formatted string with the tag macro (:tag) replaced
# Static: Expand placeholders within s
def self.expand_placeholders(s, placeholders)
# Create a pattern like /abc|def|./ for each key in placeholders
# Longer keys come first to ensure that a key like :foobar will take priority over :foo.
pattern = Regexp.new(((placeholders.keys.sort { |a, b| b.length <=> a.length }) + ["."]).join("|"))

# Format the output string. The pattern will cause scan() to return an array where each
# element is a single placeholder key, or a single character.
return s.scan(pattern).map { |token| placeholders.key?(token) ? placeholders[token] : token }.join
end

# Static: returns a fully formatted string with the tag macro (:tag) and tag name macro
# (:tag_name) replaced
#
def self.format_tag_macro(toFormat, tag, slugify_config=nil)
def self.format_tag_macro(toFormat, tag, tag_name, slugify_config=nil)
slugify_mode = slugify_config.has_key?('mode') ? slugify_config['mode'] : nil
slugify_cased = slugify_config.has_key?('cased') ? slugify_config['cased'] : false
return toFormat.sub(':tag', Jekyll::Utils.slugify(tag.to_s, mode:slugify_mode, cased:slugify_cased))

return expand_placeholders(toFormat, {
":tag" => Jekyll::Utils.slugify(tag.to_s, mode:slugify_mode, cased:slugify_cased),
":tag_name" => tag_name,
})
end #function format_tag_macro

# Static: returns a fully formatted string with the category macro (:cat) replaced
# Static: returns a fully formatted string with the category macro (:cat) and category
# name macro (:cat_name) replaced
#
def self.format_cat_macro(toFormat, category, slugify_config=nil)
def self.format_cat_macro(toFormat, category, category_name, slugify_config=nil)
slugify_mode = slugify_config.has_key?('mode') ? slugify_config['mode'] : nil
slugify_cased = slugify_config.has_key?('cased') ? slugify_config['cased'] : false
return toFormat.sub(':cat', Jekyll::Utils.slugify(category.to_s, mode:slugify_mode, cased:slugify_cased))

return expand_placeholders(toFormat, {
":cat" => Jekyll::Utils.slugify(category.to_s, mode:slugify_mode, cased:slugify_cased),
":cat_name" => category_name,
})
end #function format_cat_macro

# Static: returns a fully formatted string with the collection macro (:coll) replaced
#
def self.format_coll_macro(toFormat, collection, slugify_config=nil)
def self.format_coll_macro(toFormat, collection, collection_name, slugify_config=nil)
slugify_mode = slugify_config.has_key?('mode') ? slugify_config['mode'] : nil
slugify_cased = slugify_config.has_key?('cased') ? slugify_config['cased'] : false
return toFormat.sub(':coll', Jekyll::Utils.slugify(collection.to_s, mode:slugify_mode, cased:slugify_cased))

return expand_placeholders(toFormat, {
":coll" => Jekyll::Utils.slugify(collection.to_s, mode:slugify_mode, cased:slugify_cased),
":coll_name" => collection_name,
})
end #function format_coll_macro

# Static: returns all documents from all collections defined in the hash of collections passed in
Expand Down
22 changes: 22 additions & 0 deletions spec/autopages/utils_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require_relative '../spec_helper.rb'

module Jekyll::PaginateV2::AutoPages
describe Utils do

describe "expand_placeholders" do
it "should return string unmodified when no placeholders are present" do
Utils.expand_placeholders("hello world", {}).must_equal "hello world"
Utils.expand_placeholders("hello world", { ":foo" => "bar" }).must_equal "hello world"
end

it "should replace placeholders in the string" do
Utils.expand_placeholders("xyz:foo:bar:foo", { ":foo" => "abc", ":bar" => "def" }).must_equal "xyzabcdefabc"
Utils.expand_placeholders("xyz:foo:foobar:foo", { ":foo" => "abc", ":foobar" => "def" }).must_equal "xyzabcdefabc"
end

it "should not replace placeholders inside replacements" do
Utils.expand_placeholders(":foo:bar:foo", { ":foo" => ":bar", ":bar" => ":foo" }).must_equal ":bar:foo:bar"
end
end
end
end
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
require_relative '../lib/jekyll-paginate-v2/generator/paginator'
require_relative '../lib/jekyll-paginate-v2/generator/paginationPage'
require_relative '../lib/jekyll-paginate-v2/generator/paginationModel'
require_relative '../lib/jekyll-paginate-v2/generator/paginationGenerator'
require_relative '../lib/jekyll-paginate-v2/generator/paginationGenerator'
require_relative '../lib/jekyll-paginate-v2/autopages/utils'

0 comments on commit be5bb89

Please sign in to comment.