Match post.name instead of slugs and dates

This commit is contained in:
Alfred Xing 2014-11-04 16:13:04 -08:00
parent ab8441259e
commit 50d0fc3c85
1 changed files with 3 additions and 23 deletions

View File

@ -3,35 +3,15 @@ module Jekyll
class PostComparer class PostComparer
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/ MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/
attr_accessor :date, :slug attr_accessor :path, :date, :slug
def initialize(name) 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 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 end
def ==(other) def ==(other)
slug == post_slug(other) && other.name.match(/^#{path}#{date}-#{slug}\.[^.]+/)
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 end