diff --git a/README-AUTOPAGES.md b/README-AUTOPAGES.md index 75d1516..be03077 100644 --- a/README-AUTOPAGES.md +++ b/README-AUTOPAGES.md @@ -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' @@ -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. @@ -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: diff --git a/lib/jekyll-paginate-v2/autopages/pages/categoryAutoPage.rb b/lib/jekyll-paginate-v2/autopages/pages/categoryAutoPage.rb index 4358edc..662f29a 100644 --- a/lib/jekyll-paginate-v2/autopages/pages/categoryAutoPage.rb +++ b/lib/jekyll-paginate-v2/autopages/pages/categoryAutoPage.rb @@ -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 diff --git a/lib/jekyll-paginate-v2/autopages/pages/collectionAutoPage.rb b/lib/jekyll-paginate-v2/autopages/pages/collectionAutoPage.rb index 8a17a4b..4b2d8ed 100644 --- a/lib/jekyll-paginate-v2/autopages/pages/collectionAutoPage.rb +++ b/lib/jekyll-paginate-v2/autopages/pages/collectionAutoPage.rb @@ -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 diff --git a/lib/jekyll-paginate-v2/autopages/pages/tagAutoPage.rb b/lib/jekyll-paginate-v2/autopages/pages/tagAutoPage.rb index 4f69e22..70a299a 100644 --- a/lib/jekyll-paginate-v2/autopages/pages/tagAutoPage.rb +++ b/lib/jekyll-paginate-v2/autopages/pages/tagAutoPage.rb @@ -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 diff --git a/lib/jekyll-paginate-v2/autopages/utils.rb b/lib/jekyll-paginate-v2/autopages/utils.rb index 3881da2..f009302 100644 --- a/lib/jekyll-paginate-v2/autopages/utils.rb +++ b/lib/jekyll-paginate-v2/autopages/utils.rb @@ -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 diff --git a/spec/autopages/utils_spec.rb b/spec/autopages/utils_spec.rb new file mode 100644 index 0000000..1dcf1c5 --- /dev/null +++ b/spec/autopages/utils_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 45c059b..8bedfc6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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' \ No newline at end of file +require_relative '../lib/jekyll-paginate-v2/generator/paginationGenerator' +require_relative '../lib/jekyll-paginate-v2/autopages/utils'