Fix #4191: Reduce Document#post_read complexity slightly.

This commit is contained in:
Jordon Bedwell 2015-11-28 01:22:24 -06:00 committed by Parker Moore
parent 1298ba6908
commit 67f842546e
2 changed files with 25 additions and 17 deletions

View File

@ -289,26 +289,23 @@ module Jekyll
end
def post_read
if DATE_FILENAME_MATCHER =~ relative_path
_, _, date, slug, ext = *relative_path.match(DATE_FILENAME_MATCHER)
merge_data!({
"slug" => slug,
"ext" => ext
}, source: "filename")
data['title'] ||= slug.split('-').select(&:capitalize).join(' ')
if data['date'].nil? || data['date'].to_i == site.time.to_i
if relative_path =~ DATE_FILENAME_MATCHER
_, date, slug, ext = $1, $2, $3, $4
if !data['date'] || data['date'].to_i == site.time.to_i
merge_data!({"date" => date}, source: "filename")
end
elsif DATELESS_FILENAME_MATCHER =~ relative_path
m, cats, slug, ext = *relative_path.match(DATELESS_FILENAME_MATCHER)
data['title'] ||= slug.split('-').select {|w| w.capitalize! || w }.join(' ')
elsif relative_path =~ DATELESS_FILENAME_MATCHER
_, slug, ext = $1, $2, $3
end
merge_data!({"slug" => slug, "ext" => ext}, source: "filename")
# Try to ensure the user gets a title.
data["title"] ||= Utils.titleize_slug(slug)
populate_categories
populate_tags
if generate_excerpt?
data['excerpt'] ||= Jekyll::Excerpt.new(self)
end
generate_excerpt
end
# Add superdirectories of the special_dir to categories.
@ -445,5 +442,12 @@ module Jekyll
super
end
end
private # :nodoc:
def generate_excerpt
if generate_excerpt?
data["excerpt"] ||= Jekyll::Excerpt.new(self)
end
end
end
end

View File

@ -10,8 +10,12 @@ module Jekyll
SLUGIFY_DEFAULT_REGEXP = Regexp.new('[^[:alnum:]]+').freeze
SLUGIFY_PRETTY_REGEXP = Regexp.new("[^[:alnum:]._~!$&'()+,;=@]+").freeze
def strip_heredoc(str)
str.gsub(/^[ \t]{#{(str.scan(/^[ \t]*(?=\S)/).min || "").size}}/, "")
# Takes a slug and turns it into a simple title.
def titleize_slug(slug)
slug.split("-").map! do |val|
val.capitalize!
end.join(" ")
end
# Non-destructive version of deep_merge_hashes! See that method.