Handle files with trailing dots in their basename (#7315)

Merge pull request 7315
This commit is contained in:
Ashwin Maroli
2019-02-16 22:01:14 +05:30
committed by jekyllbot
parent 996431ba60
commit dedfb0748f
12 changed files with 93 additions and 24 deletions

View File

@@ -119,15 +119,19 @@ module Jekyll
# and with the collection's directory removed as well.
# This method is useful when building the URL of the document.
#
# NOTE: `String#gsub` removes all trailing periods (in comparison to `String#chomp`)
#
# Examples:
# When relative_path is "_methods/site/generate.md":
# When relative_path is "_methods/site/generate...md":
# cleaned_relative_path
# # => "/site/generate"
#
# Returns the cleaned relative path of the document.
def cleaned_relative_path
@cleaned_relative_path ||=
relative_path[0..-extname.length - 1].sub(collection.relative_directory, "")
relative_path[0..-extname.length - 1]
.sub(collection.relative_directory, "")
.gsub(%r!\.*\z!, "")
end
# Determine whether the document is a YAML file.
@@ -483,6 +487,10 @@ module Jekyll
slug, ext = Regexp.last_match.captures
end
# slugs shouldn't end with a period
# `String#gsub!` removes all trailing periods (in comparison to `String#chomp!`)
slug.gsub!(%r!\.*\z!, "")
# Try to ensure the user gets a title.
data["title"] ||= Utils.titleize_slug(slug)
# Only overwrite slug & ext if they aren't specified.

View File

@@ -116,10 +116,11 @@ module Jekyll
#
# name - The String filename of the page file.
#
# NOTE: `String#gsub` removes all trailing periods (in comparison to `String#chomp`)
# Returns nothing.
def process(name)
self.ext = File.extname(name)
self.basename = name[0..-ext.length - 1]
self.basename = name[0..-ext.length - 1].gsub(%r!\.*\z!, "")
end
# Add any necessary layouts to this post

View File

@@ -54,7 +54,8 @@ module Jekyll
#
# Returns destination file path.
def destination(dest)
@site.in_dest_dir(*[dest, destination_rel_dir, @name].compact)
dest = @site.in_dest_dir(dest)
@site.in_dest_dir(dest, Jekyll::URL.unescape_path(url))
end
def destination_rel_dir
@@ -99,7 +100,6 @@ module Jekyll
# Returns false if the file was not modified since last time (no-op).
def write(dest)
dest_path = destination(dest)
return false if File.exist?(dest_path) && !modified?
self.class.mtimes[path] = mtime
@@ -115,33 +115,58 @@ module Jekyll
@to_liquid ||= Drops::StaticFileDrop.new(self)
end
# Generate "basename without extension" and strip away any trailing periods.
# NOTE: `String#gsub` removes all trailing periods (in comparison to `String#chomp`)
def basename
File.basename(name, extname)
@basename ||= File.basename(name, extname).gsub(%r!\.*\z!, "")
end
def placeholders
{
:collection => @collection.label,
:path => relative_path[
@collection.relative_directory.size..relative_path.size],
:path => cleaned_relative_path,
:output_ext => "",
:name => "",
:title => "",
}
end
# Similar to Jekyll::Document#cleaned_relative_path.
# Generates a relative path with the collection's directory removed when applicable
# and additionally removes any multiple periods in the string.
#
# NOTE: `String#gsub!` removes all trailing periods (in comparison to `String#chomp!`)
#
# Examples:
# When `relative_path` is "_methods/site/my-cool-avatar...png":
# cleaned_relative_path
# # => "/site/my-cool-avatar"
#
# Returns the cleaned relative path of the static file.
def cleaned_relative_path
@cleaned_relative_path ||= begin
cleaned = relative_path[0..-extname.length - 1]
cleaned.gsub!(%r!\.*\z!, "")
cleaned.sub!(@collection.relative_directory, "") if @collection
cleaned
end
end
# Applies a similar URL-building technique as Jekyll::Document that takes
# the collection's URL template into account. The default URL template can
# be overriden in the collection's configuration in _config.yml.
def url
@url ||= if @collection.nil?
relative_path
@url ||= begin
base = if @collection.nil?
cleaned_relative_path
else
::Jekyll::URL.new(
Jekyll::URL.new(
:template => @collection.url_template,
:placeholders => placeholders
)
end.to_s.chomp("/")
base << extname
end
end
# Returns the type of the collection if present, nil otherwise.