Skip to content

Commit

Permalink
Merge pull request #4195 from jekyll/pull/cleanup-document__post_read
Browse files Browse the repository at this point in the history
Merge pull request 4195
  • Loading branch information
jekyllbot committed Jan 22, 2016
2 parents 6d67e9b + 2b8de59 commit ccb3826
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 14 deletions.
18 changes: 17 additions & 1 deletion features/collections.feature
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,21 @@ Feature: Collections
"""
When I run jekyll build
Then I should get a zero exit status
Then the _site directory should exist
And I should see "Collections: Jekyll.configuration, Jekyll.escape, Jekyll.sanitized_path, Site#generate, Initialize, Site#generate, YAML with Dots," in "_site/index.html"

Scenario: Rendered collection with date/dateless filename
Given I have an "index.html" page that contains "Collections: {% for method in site.thanksgiving %}{{ method.title }} {% endfor %}"
And I have fixture collections
And I have a "_config.yml" file with content:
"""
collections:
thanksgiving:
output: true
"""
When I run jekyll build
Then I should get a zero exit status
And the _site directory should exist
And I should see "Collections: Jekyll.configuration, Jekyll.escape, Jekyll.sanitized_path, Site#generate, , Site#generate," in "_site/index.html"
And I should see "Thanksgiving Black Friday" in "_site/index.html"
And I should see "Happy Thanksgiving" in "_site/thanksgiving/2015-11-26-thanksgiving.html"
And I should see "Black Friday" in "_site/thanksgiving/black-friday.html"
1 change: 1 addition & 0 deletions features/step_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@

Given %r{^I have fixture collections$} do
FileUtils.cp_r Paths.source_dir.join("test", "source", "_methods"), source_dir
FileUtils.cp_r Paths.source_dir.join("test", "source", "_thanksgiving"), source_dir
end

#
Expand Down
34 changes: 21 additions & 13 deletions lib/jekyll/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Document
attr_accessor :content, :output

YAML_FRONT_MATTER_REGEXP = /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m
DATELESS_FILENAME_MATCHER = /^(.*)(\.[^.]+)$/
DATELESS_FILENAME_MATCHER = /^(.+\/)*(.*)(\.[^.]+)$/
DATE_FILENAME_MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$/

# Create a new Document.
Expand Down Expand Up @@ -289,23 +289,24 @@ def read(opts = {})
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 = $2, $3, $4
if !data['date'] || data['date'].to_i == site.time.to_i
merge_data!({"date" => date}, source: "filename")
end
elsif relative_path =~ DATELESS_FILENAME_MATCHER
slug, ext = $2, $3
end

# Try to ensure the user gets a title.
data["title"] ||= Utils.titleize_slug(slug)
# Only overwrite slug & ext if they aren't specified.
data['slug'] ||= slug
data['ext'] ||= ext

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.
Expand Down Expand Up @@ -442,5 +443,12 @@ def method_missing(method, *args, &blck)
super
end
end

private # :nodoc:
def generate_excerpt
if generate_excerpt?
data["excerpt"] ||= Jekyll::Excerpt.new(self)
end
end
end
end
10 changes: 10 additions & 0 deletions lib/jekyll/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@ module Utils
SLUGIFY_DEFAULT_REGEXP = Regexp.new('[^[:alnum:]]+').freeze
SLUGIFY_PRETTY_REGEXP = Regexp.new("[^[:alnum:]._~!$&'()+,;=@]+").freeze

# Takes an indented string and removes the preceding spaces on each line

def strip_heredoc(str)
str.gsub(/^[ \t]{#{(str.scan(/^[ \t]*(?=\S)/).min || "").size}}/, "")
end

# 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.
#
# Returns the merged hashes.
Expand Down
3 changes: 3 additions & 0 deletions test/source/_thanksgiving/2015-11-26-thanksgiving.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
---
Happy {{ page.title }} !
3 changes: 3 additions & 0 deletions test/source/_thanksgiving/black-friday.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
---
{{ page.title }}

0 comments on commit ccb3826

Please sign in to comment.