From 67f842546efa980146778c85fb613e6c9b53dcb4 Mon Sep 17 00:00:00 2001 From: Jordon Bedwell Date: Sat, 28 Nov 2015 01:22:24 -0600 Subject: [PATCH] Fix #4191: Reduce Document#post_read complexity slightly. --- lib/jekyll/document.rb | 34 +++++++++++++++++++--------------- lib/jekyll/utils.rb | 8 ++++++-- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/lib/jekyll/document.rb b/lib/jekyll/document.rb index b8c162a5..c6f3cb87 100644 --- a/lib/jekyll/document.rb +++ b/lib/jekyll/document.rb @@ -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 diff --git a/lib/jekyll/utils.rb b/lib/jekyll/utils.rb index 9ffff548..6e0023de 100644 --- a/lib/jekyll/utils.rb +++ b/lib/jekyll/utils.rb @@ -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.