URL#generate_url_from_drop: be smarter about replacing *just* the keys

This commit is contained in:
Parker Moore 2016-11-10 15:34:03 -08:00
parent 9f8f031469
commit 347651e571
No known key found for this signature in database
GPG Key ID: 193CDEBA72063C58
1 changed files with 20 additions and 8 deletions

View File

@ -84,17 +84,29 @@ module Jekyll
end
end
def possible_keys(key)
if key.end_with?("_")
[key, key.chomp("_")]
else
[key]
end
end
def generate_url_from_drop(template)
template.gsub(%r!:([a-z_]+)!) do |match|
key = match.sub(":".freeze, "".freeze)
unless @placeholders.key?(key)
raise NoMethodError, "The URL template key #{key} doesn't exist!"
end
if @placeholders[key].nil?
"".freeze
else
self.class.escape_path(@placeholders[key])
pool = possible_keys(match.sub(":".freeze, "".freeze))
winner = pool.find { |key| @placeholders.key?(key) }
if winner.nil?
raise NoMethodError,
"The URL template doesn't have #{pool.join(" or ")} keys. Check your permalink template!"
end
value = @placeholders[winner]
value = "" if value.nil?
replacement = self.class.escape_path(value)
match.sub(":#{winner}", replacement)
end.gsub(%r!//!, "/".freeze)
end