From 05bbcddb29885d8f1b19ec9f5d0874de63fc4c98 Mon Sep 17 00:00:00 2001 From: Martin Jorn Rogalla Date: Wed, 4 Mar 2015 19:45:23 +0100 Subject: [PATCH] Extracted `filter_entries` from site.rb into reader.rb - Extracted - Updated References - Ran Tests Signed-off-by: Martin Jorn Rogalla --- lib/jekyll/reader.rb | 24 +++++++++++++++++------- lib/jekyll/site.rb | 18 +++--------------- test/test_entry_filter.rb | 12 ++++++------ 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/lib/jekyll/reader.rb b/lib/jekyll/reader.rb index 10748b04..8e898388 100644 --- a/lib/jekyll/reader.rb +++ b/lib/jekyll/reader.rb @@ -3,12 +3,10 @@ require 'csv' module Jekyll class Reader - # Public: Initialize a new Reader. + attr_reader :site - # @return [Object] - def initialize(source, dest) - @source = source - @dest = dest + def initialize(site) + @site = site end # 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. def in_source_dir(*paths) - paths.reduce(@source) do |base, path| + paths.reduce(site.source) do |base, path| Jekyll.sanitized_path(base, path) end end @@ -30,9 +28,21 @@ module Jekyll # # Returns a path which is prefixed with the destination directory. def in_dest_dir(*paths) - paths.reduce(@dest) do |base, path| + paths.reduce(site.dest) do |base, path| Jekyll.sanitized_path(base, path) 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 \ No newline at end of file diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 9b71db3b..4936e177 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -28,7 +28,7 @@ module Jekyll @source = File.expand_path(config['source']).freeze @dest = File.expand_path(config['destination']).freeze - @reader = Reader.new(@source,@dest); + @reader = Jekyll::Reader.new(self) # Initialize incremental regenerator @regenerator = Regenerator.new(self) @@ -144,7 +144,7 @@ module Jekyll # Returns nothing. def read_directories(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_drafts(dir) if show_drafts @@ -389,18 +389,6 @@ module Jekyll } 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. # # klass - The Class of the Converter to fetch. @@ -434,7 +422,7 @@ module Jekyll def get_entries(dir, subfolder) base = reader.in_source_dir(dir, subfolder) 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)) } end diff --git a/test/test_entry_filter.rb b/test/test_entry_filter.rb index e3a7e8c0..341828ba 100644 --- a/test/test_entry_filter.rb +++ b/test/test_entry_filter.rb @@ -19,7 +19,7 @@ class TestEntryFilter < JekyllUnitTest files = %w[index.html site.css .htaccess vendor] @site.exclude = excludes + ["exclude*"] - assert_equal files, @site.filter_entries(excludes + files + ["excludeA"]) + assert_equal files, @site.reader.filter_entries(excludes + files + ["excludeA"]) end should "filter entries with exclude relative to site source" do @@ -27,7 +27,7 @@ class TestEntryFilter < JekyllUnitTest files = %w[index.html vendor/css .htaccess] @site.exclude = excludes - assert_equal files, @site.filter_entries(excludes + files + ["css"]) + assert_equal files, @site.reader.filter_entries(excludes + files + ["css"]) end should "filter excluded directory and contained files" do @@ -35,7 +35,7 @@ class TestEntryFilter < JekyllUnitTest files = %w[index.html .htaccess] @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 should "not filter entries within include" do @@ -43,20 +43,20 @@ class TestEntryFilter < JekyllUnitTest files = %w[index.html _index.html .htaccess includeA] @site.include = includes - assert_equal files, @site.filter_entries(files) + assert_equal files, @site.reader.filter_entries(files) end should "filter symlink entries when safe mode enabled" do site = Site.new(site_configuration('safe' => true)) stub(File).symlink?('symlink.js') {true} files = %w[symlink.js] - assert_equal [], site.filter_entries(files) + assert_equal [], site.reader.filter_entries(files) end should "not filter symlink entries when safe mode disabled" do stub(File).symlink?('symlink.js') {true} files = %w[symlink.js] - assert_equal files, @site.filter_entries(files) + assert_equal files, @site.reader.filter_entries(files) end should "not include symlinks in safe mode" do