Skip to content

Commit

Permalink
props @DirtyF: handle static files in a collection
Browse files Browse the repository at this point in the history
Static files within a collection inside a custom collections_dir needs
to output to the same path as when the collection is at the root of the
source directory.
  • Loading branch information
ashmaroli committed Jan 15, 2018
1 parent 39d0bad commit bfe2a2c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions features/collections_dir.feature
Expand Up @@ -144,6 +144,9 @@ Feature: Collections Directory
collections_dir: gathering
"""
And I have a "gathering/_puppies/static_file.txt" file that contains "Static content."
And I have a gathering/_puppies/nested directory
And I have a "gathering/_puppies/nested/static_file.txt" file that contains "Nested Static content."
When I run jekyll build --drafts
Then I should get a zero exit status
And the _site directory should exist
Expand All @@ -152,5 +155,7 @@ Feature: Collections Directory
And the "_site/2009/03/27/draft-in-gathering.html" file should exist
And the "_site/2009/03/27/draft-at-root.html" file should not exist
And the "_site/puppies/rover-at-root.html" file should not exist
And I should see exactly "Static content." in "_site/puppies/static_file.txt"
And I should see exactly "Nested Static content." in "_site/puppies/nested/static_file.txt"
And the _site/gathering directory should not exist
And the _site/_posts directory should not exist
3 changes: 2 additions & 1 deletion lib/jekyll/collection.rb
Expand Up @@ -226,7 +226,8 @@ def read_static_file(file_path, full_path)
site.source,
relative_dir,
File.basename(full_path),
self
self,
site.config["collections_dir"]
)
end
end
Expand Down
10 changes: 8 additions & 2 deletions lib/jekyll/static_file.rb
Expand Up @@ -26,12 +26,13 @@ def reset_cache
# dir - The String path between <source> and the file.
# name - The String filename of the file.
# rubocop: disable ParameterLists
def initialize(site, base, dir, name, collection = nil)
def initialize(site, base, dir, name, collection = nil, collections_dir = "")
@site = site
@base = base
@dir = dir
@name = name
@collection = collection
@collections_dir = collections_dir
@relative_path = File.join(*[@dir, @name].compact)
@extname = File.extname(@name)
@data = @site.frontmatter_defaults.all(relative_path, type)
Expand All @@ -40,7 +41,12 @@ def initialize(site, base, dir, name, collection = nil)

# Returns source file path.
def path
File.join(*[@base, @dir, @name].compact)
# Static file is from a collection inside custom collections directory
if !@collection.nil? && !@collections_dir.empty?
File.join(*[@base, @collections_dir, @dir, @name].compact)
else
File.join(*[@base, @dir, @name].compact)
end
end

# Obtain destination path.
Expand Down

0 comments on commit bfe2a2c

Please sign in to comment.