Skip to content

Allow using :xxx_name in autopage titles (fixes #225). #240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should call slugify to avoid invalid characters

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the :cat_name expansion? The whole point is to be able to return the category name without being mangled - what characters are "invalid" here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just gave this a bit of a try with the category Tes<tin>g&" and it comes out in the page as follows:

<title>Posts in category Tesg&amp;&quot; | ...</title>
<meta property="og:title" content="Posts in category Tesg&amp;&quot;" />

So quotes and ampersands are handled correctly, but anything between angle brackets is stripped out...

This is the same behaviour I get if I try putting angle brackets in a plain page title, e.g:

title: 'foo"<bar>baz'

in the page front matter comes out as:

<title>foo&quot;baz | ...</title>

An unmatched bracket in either (e.g. foo<bar) comes out correctly (foo&lt;bar).

Any idea if this is a limitation/bug within Jekyll itself? Either way, it seems to be doing the right thing.

})
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,
})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this and other lines in this file, call slugify

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'