Merge pull request #5030 from ayastreb/document-stylefix
Merge pull request 5030
This commit is contained in:
commit
549371fd4e
|
@ -7,9 +7,9 @@ module Jekyll
|
||||||
attr_reader :path, :site, :extname, :collection
|
attr_reader :path, :site, :extname, :collection
|
||||||
attr_accessor :content, :output
|
attr_accessor :content, :output
|
||||||
|
|
||||||
YAML_FRONT_MATTER_REGEXP = /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m
|
YAML_FRONT_MATTER_REGEXP = %r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m
|
||||||
DATELESS_FILENAME_MATCHER = /^(.+\/)*(.*)(\.[^.]+)$/
|
DATELESS_FILENAME_MATCHER = %r!^(?:.+/)*(.*)(\.[^.]+)$!
|
||||||
DATE_FILENAME_MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$/
|
DATE_FILENAME_MATCHER = %r!^(?:.+/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$!
|
||||||
|
|
||||||
# Create a new Document.
|
# Create a new Document.
|
||||||
#
|
#
|
||||||
|
@ -51,16 +51,16 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns the merged data.
|
# Returns the merged data.
|
||||||
def merge_data!(other, source: "YAML front matter")
|
def merge_data!(other, source: "YAML front matter")
|
||||||
if other.key?('categories') && !other['categories'].nil?
|
if other.key?("categories") && !other["categories"].nil?
|
||||||
if other['categories'].is_a?(String)
|
if other["categories"].is_a?(String)
|
||||||
other['categories'] = other['categories'].split(" ").map(&:strip)
|
other["categories"] = other["categories"].split(" ").map(&:strip)
|
||||||
end
|
end
|
||||||
other['categories'] = (data['categories'] || []) | other['categories']
|
other["categories"] = (data["categories"] || []) | other["categories"]
|
||||||
end
|
end
|
||||||
Utils.deep_merge_hashes!(data, other)
|
Utils.deep_merge_hashes!(data, other)
|
||||||
if data.key?('date') && !data['date'].is_a?(Time)
|
if data.key?("date") && !data["date"].is_a?(Time)
|
||||||
data['date'] = Utils.parse_date(
|
data["date"] = Utils.parse_date(
|
||||||
data['date'].to_s,
|
data["date"].to_s,
|
||||||
"Document '#{relative_path}' does not have a valid date in the #{source}."
|
"Document '#{relative_path}' does not have a valid date in the #{source}."
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
@ -68,7 +68,7 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
def date
|
def date
|
||||||
data['date'] ||= (draft? ? source_file_mtime : site.time)
|
data["date"] ||= (draft? ? source_file_mtime : site.time)
|
||||||
end
|
end
|
||||||
|
|
||||||
def source_file_mtime
|
def source_file_mtime
|
||||||
|
@ -81,7 +81,8 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns whether the document is a draft.
|
# Returns whether the document is a draft.
|
||||||
def draft?
|
def draft?
|
||||||
data['draft'] ||= relative_path.index(collection.relative_directory).nil? && collection.label == "posts"
|
data["draft"] ||= relative_path.index(collection.relative_directory).nil? &&
|
||||||
|
collection.label == "posts"
|
||||||
end
|
end
|
||||||
|
|
||||||
# The path to the document, relative to the site source.
|
# The path to the document, relative to the site source.
|
||||||
|
@ -89,7 +90,8 @@ module Jekyll
|
||||||
# Returns a String path which represents the relative path
|
# Returns a String path which represents the relative path
|
||||||
# from the site source to this document
|
# from the site source to this document
|
||||||
def relative_path
|
def relative_path
|
||||||
@relative_path ||= Pathname.new(path).relative_path_from(Pathname.new(site.source)).to_s
|
@relative_path ||= Pathname.new(path)
|
||||||
|
.relative_path_from(Pathname.new(site.source)).to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
# The output extension of the document.
|
# The output extension of the document.
|
||||||
|
@ -103,7 +105,7 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns the basename without the file extname.
|
# Returns the basename without the file extname.
|
||||||
def basename_without_ext
|
def basename_without_ext
|
||||||
@basename_without_ext ||= File.basename(path, '.*')
|
@basename_without_ext ||= File.basename(path, ".*")
|
||||||
end
|
end
|
||||||
|
|
||||||
# The base filename of the document.
|
# The base filename of the document.
|
||||||
|
@ -156,7 +158,7 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns true if extname == .coffee, false otherwise.
|
# Returns true if extname == .coffee, false otherwise.
|
||||||
def coffeescript_file?
|
def coffeescript_file?
|
||||||
'.coffee'.eql?(extname)
|
".coffee" == extname
|
||||||
end
|
end
|
||||||
|
|
||||||
# Determine whether the file should be rendered with Liquid.
|
# Determine whether the file should be rendered with Liquid.
|
||||||
|
@ -195,7 +197,7 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns the permalink or nil if no permalink was set in the data.
|
# Returns the permalink or nil if no permalink was set in the data.
|
||||||
def permalink
|
def permalink
|
||||||
data && data.is_a?(Hash) && data['permalink']
|
data && data.is_a?(Hash) && data["permalink"]
|
||||||
end
|
end
|
||||||
|
|
||||||
# The computed URL for the document. See `Jekyll::URL#to_s` for more details.
|
# The computed URL for the document. See `Jekyll::URL#to_s` for more details.
|
||||||
|
@ -203,9 +205,9 @@ module Jekyll
|
||||||
# Returns the computed URL for the document.
|
# Returns the computed URL for the document.
|
||||||
def url
|
def url
|
||||||
@url = URL.new({
|
@url = URL.new({
|
||||||
:template => url_template,
|
:template => url_template,
|
||||||
:placeholders => url_placeholders,
|
:placeholders => url_placeholders,
|
||||||
:permalink => permalink
|
:permalink => permalink
|
||||||
}).to_s
|
}).to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -237,18 +239,17 @@ module Jekyll
|
||||||
def write(dest)
|
def write(dest)
|
||||||
path = destination(dest)
|
path = destination(dest)
|
||||||
FileUtils.mkdir_p(File.dirname(path))
|
FileUtils.mkdir_p(File.dirname(path))
|
||||||
File.open(path, 'wb') do |f|
|
File.write(path, output, :mode => "wb")
|
||||||
f.write(output)
|
|
||||||
end
|
|
||||||
|
|
||||||
trigger_hooks(:post_write)
|
trigger_hooks(:post_write)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Whether the file is published or not, as indicated in YAML front-matter
|
# Whether the file is published or not, as indicated in YAML front-matter
|
||||||
#
|
#
|
||||||
# Returns true if the 'published' key is specified in the YAML front-matter and not `false`.
|
# Returns 'false' if the 'published' key is specified in the
|
||||||
|
# YAML front-matter and is 'false'. Otherwise returns 'true'.
|
||||||
def published?
|
def published?
|
||||||
!(data.key?('published') && data['published'] == false)
|
!(data.key?("published") && data["published"] == false)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Read in the file and assign the content and data based on the file contents.
|
# Read in the file and assign the content and data based on the file contents.
|
||||||
|
@ -263,23 +264,24 @@ module Jekyll
|
||||||
@data = SafeYAML.load_file(path)
|
@data = SafeYAML.load_file(path)
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
defaults = @site.frontmatter_defaults.all(relative_path, collection.label.to_sym)
|
defaults = @site.frontmatter_defaults.all(
|
||||||
merge_data!(defaults, source: "front matter defaults") unless defaults.empty?
|
relative_path,
|
||||||
|
collection.label.to_sym
|
||||||
|
)
|
||||||
|
merge_data!(defaults, :source => "front matter defaults") unless defaults.empty?
|
||||||
|
|
||||||
self.content = File.read(path, Utils.merged_file_read_opts(site, opts))
|
self.content = File.read(path, Utils.merged_file_read_opts(site, opts))
|
||||||
if content =~ YAML_FRONT_MATTER_REGEXP
|
if content =~ YAML_FRONT_MATTER_REGEXP
|
||||||
self.content = $POSTMATCH
|
self.content = $POSTMATCH
|
||||||
data_file = SafeYAML.load(Regexp.last_match(1))
|
data_file = SafeYAML.load(Regexp.last_match(1))
|
||||||
merge_data!(data_file, source: "YAML front matter") if data_file
|
merge_data!(data_file, :source => "YAML front matter") if data_file
|
||||||
end
|
end
|
||||||
|
|
||||||
post_read
|
post_read
|
||||||
rescue SyntaxError => e
|
rescue SyntaxError => e
|
||||||
Jekyll.logger.error "Error:", "YAML Exception reading #{path}: #{e.message}"
|
Jekyll.logger.error "Error:", "YAML Exception reading #{path}: #{e.message}"
|
||||||
rescue Exception => e
|
rescue => e
|
||||||
if e.is_a? Jekyll::Errors::FatalException
|
raise e if e.is_a? Jekyll::Errors::FatalException
|
||||||
raise e
|
|
||||||
end
|
|
||||||
Jekyll.logger.error "Error:", "could not read file #{path}: #{e.message}"
|
Jekyll.logger.error "Error:", "could not read file #{path}: #{e.message}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -287,19 +289,19 @@ module Jekyll
|
||||||
|
|
||||||
def post_read
|
def post_read
|
||||||
if relative_path =~ DATE_FILENAME_MATCHER
|
if relative_path =~ DATE_FILENAME_MATCHER
|
||||||
date, slug, ext = $2, $3, $4
|
date, slug, ext = Regexp.last_match.captures
|
||||||
if !data['date'] || data['date'].to_i == site.time.to_i
|
if !data["date"] || data["date"].to_i == site.time.to_i
|
||||||
merge_data!({"date" => date}, source: "filename")
|
merge_data!({ "date" => date }, :source => "filename")
|
||||||
end
|
end
|
||||||
elsif relative_path =~ DATELESS_FILENAME_MATCHER
|
elsif relative_path =~ DATELESS_FILENAME_MATCHER
|
||||||
slug, ext = $2, $3
|
slug, ext = Regexp.last_match.captures
|
||||||
end
|
end
|
||||||
|
|
||||||
# Try to ensure the user gets a title.
|
# Try to ensure the user gets a title.
|
||||||
data["title"] ||= Utils.titleize_slug(slug)
|
data["title"] ||= Utils.titleize_slug(slug)
|
||||||
# Only overwrite slug & ext if they aren't specified.
|
# Only overwrite slug & ext if they aren't specified.
|
||||||
data['slug'] ||= slug
|
data["slug"] ||= slug
|
||||||
data['ext'] ||= ext
|
data["ext"] ||= ext
|
||||||
|
|
||||||
populate_categories
|
populate_categories
|
||||||
populate_tags
|
populate_tags
|
||||||
|
@ -312,16 +314,19 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
def categories_from_path(special_dir)
|
def categories_from_path(special_dir)
|
||||||
superdirs = relative_path.sub(/#{special_dir}(.*)/, '').split(File::SEPARATOR).reject do |c|
|
superdirs = relative_path.sub(%r!#{special_dir}(.*)!, "")
|
||||||
|
.split(File::SEPARATOR)
|
||||||
|
.reject do |c|
|
||||||
c.empty? || c.eql?(special_dir) || c.eql?(basename)
|
c.empty? || c.eql?(special_dir) || c.eql?(basename)
|
||||||
end
|
end
|
||||||
merge_data!({ 'categories' => superdirs }, source: "file path")
|
merge_data!({ "categories" => superdirs }, :source => "file path")
|
||||||
end
|
end
|
||||||
|
|
||||||
def populate_categories
|
def populate_categories
|
||||||
merge_data!({
|
merge_data!({
|
||||||
'categories' => (
|
"categories" => (
|
||||||
Array(data['categories']) + Utils.pluralized_array_from_hash(data, 'category', 'categories')
|
Array(data["categories"]) +
|
||||||
|
Utils.pluralized_array_from_hash(data, "category", "categories")
|
||||||
).map(&:to_s).flatten.uniq
|
).map(&:to_s).flatten.uniq
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
@ -351,7 +356,7 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns the content of the document
|
# Returns the content of the document
|
||||||
def to_s
|
def to_s
|
||||||
output || content || 'NO CONTENT'
|
output || content || "NO CONTENT"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Compare this document against another document.
|
# Compare this document against another document.
|
||||||
|
@ -361,7 +366,7 @@ module Jekyll
|
||||||
# equal or greater than the other doc's path. See String#<=> for more details.
|
# equal or greater than the other doc's path. See String#<=> for more details.
|
||||||
def <=>(other)
|
def <=>(other)
|
||||||
return nil unless other.respond_to?(:data)
|
return nil unless other.respond_to?(:data)
|
||||||
cmp = data['date'] <=> other.data['date']
|
cmp = data["date"] <=> other.data["date"]
|
||||||
cmp = path <=> other.path if cmp.nil? || cmp == 0
|
cmp = path <=> other.path if cmp.nil? || cmp == 0
|
||||||
cmp
|
cmp
|
||||||
end
|
end
|
||||||
|
@ -380,7 +385,7 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns the document excerpt_separator
|
# Returns the document excerpt_separator
|
||||||
def excerpt_separator
|
def excerpt_separator
|
||||||
(data['excerpt_separator'] || site.config['excerpt_separator']).to_s
|
(data["excerpt_separator"] || site.config["excerpt_separator"]).to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
# Whether to generate an excerpt
|
# Whether to generate an excerpt
|
||||||
|
@ -394,8 +399,6 @@ module Jekyll
|
||||||
pos = collection.docs.index { |post| post.equal?(self) }
|
pos = collection.docs.index { |post| post.equal?(self) }
|
||||||
if pos && pos < collection.docs.length - 1
|
if pos && pos < collection.docs.length - 1
|
||||||
collection.docs[pos + 1]
|
collection.docs[pos + 1]
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -403,8 +406,6 @@ module Jekyll
|
||||||
pos = collection.docs.index { |post| post.equal?(self) }
|
pos = collection.docs.index { |post| post.equal?(self) }
|
||||||
if pos && pos > 0
|
if pos && pos > 0
|
||||||
collection.docs[pos - 1]
|
collection.docs[pos - 1]
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -414,7 +415,7 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
def id
|
def id
|
||||||
@id ||= File.join(File.dirname(url), (data['slug'] || basename_without_ext).to_s)
|
@id ||= File.join(File.dirname(url), (data["slug"] || basename_without_ext).to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Calculate related posts.
|
# Calculate related posts.
|
||||||
|
@ -433,8 +434,9 @@ module Jekyll
|
||||||
# Override of method_missing to check in @data for the key.
|
# Override of method_missing to check in @data for the key.
|
||||||
def method_missing(method, *args, &blck)
|
def method_missing(method, *args, &blck)
|
||||||
if data.key?(method.to_s)
|
if data.key?(method.to_s)
|
||||||
Jekyll.logger.warn "Deprecation:", "Document##{method} is now a key in the #data hash."
|
Jekyll::Deprecator.deprecation_message "Document##{method} is now a key "\
|
||||||
Jekyll.logger.warn "", "Called by #{caller.first}."
|
"in the #data hash."
|
||||||
|
Jekyll::Deprecator.deprecation_message "Called by #{caller.first}."
|
||||||
data[method.to_s]
|
data[method.to_s]
|
||||||
else
|
else
|
||||||
super
|
super
|
||||||
|
|
Loading…
Reference in New Issue