-
Notifications
You must be signed in to change notification settings - Fork 291
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
solemnwarning
wants to merge
1
commit into
sverrirs:master
Choose a base branch
from
solemnwarning:title-name
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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: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"baz | ...</title>
An unmatched bracket in either (e.g.
foo<bar
) comes out correctly (foo<bar
).Any idea if this is a limitation/bug within Jekyll itself? Either way, it seems to be doing the right thing.