Fall back to old method with deprecation warning

This commit is contained in:
Alfred Xing 2014-11-09 11:55:47 -08:00
parent 50d0fc3c85
commit eaa132c65b
1 changed files with 38 additions and 1 deletions

View File

@ -3,9 +3,10 @@ module Jekyll
class PostComparer class PostComparer
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/ MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/
attr_accessor :path, :date, :slug attr_reader :path, :date, :slug, :name
def initialize(name) def initialize(name)
@name = 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 raise ArgumentError.new("'#{name}' does not contain valid date and/or title.") unless all
end end
@ -13,6 +14,29 @@ module Jekyll
def ==(other) def ==(other)
other.name.match(/^#{path}#{date}-#{slug}\.[^.]+/) other.name.match(/^#{path}#{date}-#{slug}\.[^.]+/)
end 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 end
class PostUrl < Liquid::Tag class PostUrl < Liquid::Tag
@ -39,6 +63,19 @@ eos
end end
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 raise ArgumentError.new <<-eos
Could not find post "#{@orig_post}" in tag 'post_url'. Could not find post "#{@orig_post}" in tag 'post_url'.