From 50d0fc3c85feb287551f79a537c2ff81e3ecf5fa Mon Sep 17 00:00:00 2001 From: Alfred Xing Date: Tue, 4 Nov 2014 16:13:04 -0800 Subject: [PATCH 1/3] Match post.name instead of slugs and dates --- lib/jekyll/tags/post_url.rb | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/lib/jekyll/tags/post_url.rb b/lib/jekyll/tags/post_url.rb index 0f92df49..39adc51a 100644 --- a/lib/jekyll/tags/post_url.rb +++ b/lib/jekyll/tags/post_url.rb @@ -3,35 +3,15 @@ module Jekyll class PostComparer MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/ - attr_accessor :date, :slug + attr_accessor :path, :date, :slug def initialize(name) - all, path, date, slug = *name.sub(/^\//, "").match(MATCHER) + all, @path, @date, @slug = *name.sub(/^\//, "").match(MATCHER) raise ArgumentError.new("'#{name}' does not contain valid date and/or title.") unless all - @slug = path ? path + slug : slug - @date = Utils.parse_date(date, "'#{name}' does not contain valid date.") end def ==(other) - slug == post_slug(other) && - date.year == other.date.year && - date.month == other.date.month && - date.day == other.date.day - end - - private - # Construct the directory-aware post slug for a Jekyll::Post - # - # other - the Jekyll::Post - # - # Returns the post slug with the subdirectory (relative to _posts) - def post_slug(other) - path = other.name.split("/")[0...-1].join("/") - if path.nil? || path == "" - other.slug - else - path + '/' + other.slug - end + other.name.match(/^#{path}#{date}-#{slug}\.[^.]+/) end end From eaa132c65beae9a3e63ab604b7a3126acb5057a1 Mon Sep 17 00:00:00 2001 From: Alfred Xing Date: Sun, 9 Nov 2014 11:55:47 -0800 Subject: [PATCH 2/3] Fall back to old method with deprecation warning --- lib/jekyll/tags/post_url.rb | 39 ++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/jekyll/tags/post_url.rb b/lib/jekyll/tags/post_url.rb index 39adc51a..1ffff635 100644 --- a/lib/jekyll/tags/post_url.rb +++ b/lib/jekyll/tags/post_url.rb @@ -3,9 +3,10 @@ module Jekyll class PostComparer MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/ - attr_accessor :path, :date, :slug + attr_reader :path, :date, :slug, :name def initialize(name) + @name = name all, @path, @date, @slug = *name.sub(/^\//, "").match(MATCHER) raise ArgumentError.new("'#{name}' does not contain valid date and/or title.") unless all end @@ -13,6 +14,29 @@ module Jekyll def ==(other) other.name.match(/^#{path}#{date}-#{slug}\.[^.]+/) end + + def deprecated_equality(other) + date = Utils.parse_date(name, "'#{name}' does not contain valid date and/or title.") + slug == post_slug(other) && + date.year == other.date.year && + date.month == other.date.month && + date.day == other.date.day + end + + private + # Construct the directory-aware post slug for a Jekyll::Post + # + # other - the Jekyll::Post + # + # Returns the post slug with the subdirectory (relative to _posts) + def post_slug(other) + path = other.name.split("/")[0...-1].join("/") + if path.nil? || path == "" + other.slug + else + path + '/' + other.slug + end + end end class PostUrl < Liquid::Tag @@ -39,6 +63,19 @@ eos end end + # New matching method did not match, fall back to old method + # with deprecation warning if this matches + + site.posts.each do |p| + if @post.deprecated_equality p + Jekyll::Deprecator.deprecation_message "A call to '{{ post_url #{name} }}' did not match " + + "a post using the new matching method of checking name " + + "(path-date-slug) equality. Please make sure that you " + + "change this tag to match the post's name exactly." + return p.url + end + end + raise ArgumentError.new <<-eos Could not find post "#{@orig_post}" in tag 'post_url'. From d4c15efff9fc1138e429652771787066268cd248 Mon Sep 17 00:00:00 2001 From: Alfred Xing Date: Tue, 11 Nov 2014 17:35:09 -0800 Subject: [PATCH 3/3] Cache name matching regex --- lib/jekyll/tags/post_url.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/jekyll/tags/post_url.rb b/lib/jekyll/tags/post_url.rb index 1ffff635..ce97642f 100644 --- a/lib/jekyll/tags/post_url.rb +++ b/lib/jekyll/tags/post_url.rb @@ -9,10 +9,12 @@ module Jekyll @name = name all, @path, @date, @slug = *name.sub(/^\//, "").match(MATCHER) raise ArgumentError.new("'#{name}' does not contain valid date and/or title.") unless all + + @name_regex = /^#{path}#{date}-#{slug}\.[^.]+/ end def ==(other) - other.name.match(/^#{path}#{date}-#{slug}\.[^.]+/) + other.name.match(@name_regex) end def deprecated_equality(other)