Extracted `filter_entries` from site.rb into reader.rb

- Extracted
 - Updated References
 - Ran Tests

Signed-off-by: Martin Jorn Rogalla <martin@martinrogalla.com>
This commit is contained in:
Martin Jorn Rogalla 2015-03-04 19:45:23 +01:00
parent c836ec613d
commit 05bbcddb29
3 changed files with 26 additions and 28 deletions

View File

@ -3,12 +3,10 @@ require 'csv'
module Jekyll module Jekyll
class Reader class Reader
# Public: Initialize a new Reader. attr_reader :site
# @return [Object] def initialize(site)
def initialize(source, dest) @site = site
@source = source
@dest = dest
end end
# Public: Prefix a given path with the source directory. # Public: Prefix a given path with the source directory.
@ -18,7 +16,7 @@ module Jekyll
# #
# Returns a path which is prefixed with the source directory. # Returns a path which is prefixed with the source directory.
def in_source_dir(*paths) def in_source_dir(*paths)
paths.reduce(@source) do |base, path| paths.reduce(site.source) do |base, path|
Jekyll.sanitized_path(base, path) Jekyll.sanitized_path(base, path)
end end
end end
@ -30,9 +28,21 @@ module Jekyll
# #
# Returns a path which is prefixed with the destination directory. # Returns a path which is prefixed with the destination directory.
def in_dest_dir(*paths) def in_dest_dir(*paths)
paths.reduce(@dest) do |base, path| paths.reduce(site.dest) do |base, path|
Jekyll.sanitized_path(base, path) Jekyll.sanitized_path(base, path)
end end
end end
# Filter out any files/directories that are hidden or backup files (start
# with "." or "#" or end with "~"), or contain site content (start with "_"),
# or are excluded in the site configuration, unless they are web server
# files such as '.htaccess'.
#
# entries - The Array of String file/directory entries to filter.
#
# Returns the Array of filtered entries.
def filter_entries(entries, base_directory = nil)
EntryFilter.new(site, base_directory).filter(entries)
end
end end
end end

View File

@ -28,7 +28,7 @@ module Jekyll
@source = File.expand_path(config['source']).freeze @source = File.expand_path(config['source']).freeze
@dest = File.expand_path(config['destination']).freeze @dest = File.expand_path(config['destination']).freeze
@reader = Reader.new(@source,@dest); @reader = Jekyll::Reader.new(self)
# Initialize incremental regenerator # Initialize incremental regenerator
@regenerator = Regenerator.new(self) @regenerator = Regenerator.new(self)
@ -144,7 +144,7 @@ module Jekyll
# Returns nothing. # Returns nothing.
def read_directories(dir = '') def read_directories(dir = '')
base = reader.in_source_dir(dir) base = reader.in_source_dir(dir)
entries = Dir.chdir(base) { filter_entries(Dir.entries('.'), base) } entries = Dir.chdir(base) { reader.filter_entries(Dir.entries('.'), base) }
read_posts(dir) read_posts(dir)
read_drafts(dir) if show_drafts read_drafts(dir) if show_drafts
@ -389,18 +389,6 @@ module Jekyll
} }
end end
# Filter out any files/directories that are hidden or backup files (start
# with "." or "#" or end with "~"), or contain site content (start with "_"),
# or are excluded in the site configuration, unless they are web server
# files such as '.htaccess'.
#
# entries - The Array of String file/directory entries to filter.
#
# Returns the Array of filtered entries.
def filter_entries(entries, base_directory = nil)
EntryFilter.new(self, base_directory).filter(entries)
end
# Get the implementation class for the given Converter. # Get the implementation class for the given Converter.
# #
# klass - The Class of the Converter to fetch. # klass - The Class of the Converter to fetch.
@ -434,7 +422,7 @@ module Jekyll
def get_entries(dir, subfolder) def get_entries(dir, subfolder)
base = reader.in_source_dir(dir, subfolder) base = reader.in_source_dir(dir, subfolder)
return [] unless File.exist?(base) return [] unless File.exist?(base)
entries = Dir.chdir(base) { filter_entries(Dir['**/*'], base) } entries = Dir.chdir(base) { reader.filter_entries(Dir['**/*'], base) }
entries.delete_if { |e| File.directory?(reader.in_source_dir(base, e)) } entries.delete_if { |e| File.directory?(reader.in_source_dir(base, e)) }
end end

View File

@ -19,7 +19,7 @@ class TestEntryFilter < JekyllUnitTest
files = %w[index.html site.css .htaccess vendor] files = %w[index.html site.css .htaccess vendor]
@site.exclude = excludes + ["exclude*"] @site.exclude = excludes + ["exclude*"]
assert_equal files, @site.filter_entries(excludes + files + ["excludeA"]) assert_equal files, @site.reader.filter_entries(excludes + files + ["excludeA"])
end end
should "filter entries with exclude relative to site source" do should "filter entries with exclude relative to site source" do
@ -27,7 +27,7 @@ class TestEntryFilter < JekyllUnitTest
files = %w[index.html vendor/css .htaccess] files = %w[index.html vendor/css .htaccess]
@site.exclude = excludes @site.exclude = excludes
assert_equal files, @site.filter_entries(excludes + files + ["css"]) assert_equal files, @site.reader.filter_entries(excludes + files + ["css"])
end end
should "filter excluded directory and contained files" do should "filter excluded directory and contained files" do
@ -35,7 +35,7 @@ class TestEntryFilter < JekyllUnitTest
files = %w[index.html .htaccess] files = %w[index.html .htaccess]
@site.exclude = excludes @site.exclude = excludes
assert_equal files, @site.filter_entries(excludes + files + ["css", "css/main.css", "css/vendor.css"]) assert_equal files, @site.reader.filter_entries(excludes + files + ["css", "css/main.css", "css/vendor.css"])
end end
should "not filter entries within include" do should "not filter entries within include" do
@ -43,20 +43,20 @@ class TestEntryFilter < JekyllUnitTest
files = %w[index.html _index.html .htaccess includeA] files = %w[index.html _index.html .htaccess includeA]
@site.include = includes @site.include = includes
assert_equal files, @site.filter_entries(files) assert_equal files, @site.reader.filter_entries(files)
end end
should "filter symlink entries when safe mode enabled" do should "filter symlink entries when safe mode enabled" do
site = Site.new(site_configuration('safe' => true)) site = Site.new(site_configuration('safe' => true))
stub(File).symlink?('symlink.js') {true} stub(File).symlink?('symlink.js') {true}
files = %w[symlink.js] files = %w[symlink.js]
assert_equal [], site.filter_entries(files) assert_equal [], site.reader.filter_entries(files)
end end
should "not filter symlink entries when safe mode disabled" do should "not filter symlink entries when safe mode disabled" do
stub(File).symlink?('symlink.js') {true} stub(File).symlink?('symlink.js') {true}
files = %w[symlink.js] files = %w[symlink.js]
assert_equal files, @site.filter_entries(files) assert_equal files, @site.reader.filter_entries(files)
end end
should "not include symlinks in safe mode" do should "not include symlinks in safe mode" do