Merge pull request #1697 from mojombo/entry-filter-method-object

Extract `Site#filter_entries` into a method object
This commit is contained in:
Matt Rogers 2013-11-05 05:44:25 -08:00
commit a05e8af5da
5 changed files with 111 additions and 71 deletions

View File

@ -45,6 +45,7 @@ require 'jekyll/static_file'
require 'jekyll/errors' require 'jekyll/errors'
require 'jekyll/related_posts' require 'jekyll/related_posts'
require 'jekyll/cleaner' require 'jekyll/cleaner'
require 'jekyll/entry_filter'
# extensions # extensions
require 'jekyll/plugin' require 'jekyll/plugin'

View File

@ -0,0 +1,35 @@
class EntryFilter
attr_reader :site
def initialize(site)
@site = site
end
def filter(entries)
entries.reject do |e|
unless included?(e)
special?(e) || backup?(e) || excluded?(e) || symlink?(e)
end
end
end
def included?(entry)
site.include.glob_include?(entry)
end
def special?(entry)
['.', '_', '#'].include?(entry[0..0])
end
def backup?(entry)
entry[-1..-1] == '~'
end
def excluded?(entry)
site.exclude.glob_include?(entry)
end
def symlink?(entry)
File.symlink?(entry) && site.safe
end
end

View File

@ -325,14 +325,7 @@ module Jekyll
# #
# Returns the Array of filtered entries. # Returns the Array of filtered entries.
def filter_entries(entries) def filter_entries(entries)
entries.reject do |e| EntryFilter.new(self).filter(entries)
unless self.include.glob_include?(e)
['.', '_', '#'].include?(e[0..0]) ||
e[-1..-1] == '~' ||
self.exclude.glob_include?(e) ||
(File.symlink?(e) && self.safe)
end
end
end end
# Get the implementation class for the given Converter. # Get the implementation class for the given Converter.

74
test/test_entry_filter.rb Normal file
View File

@ -0,0 +1,74 @@
require 'helper'
class TestEntryFilter < Test::Unit::TestCase
context "Filtering entries" do
setup do
stub(Jekyll).configuration do
Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir})
end
@site = Site.new(Jekyll.configuration)
end
should "filter entries" do
ent1 = %w[foo.markdown bar.markdown baz.markdown #baz.markdown#
.baz.markdow foo.markdown~ .htaccess _posts _pages]
entries = EntryFilter.new(@site).filter(ent1)
assert_equal %w[foo.markdown bar.markdown baz.markdown .htaccess], entries
end
should "filter entries with exclude" do
excludes = %w[README TODO]
files = %w[index.html site.css .htaccess]
@site.exclude = excludes + ["exclude*"]
assert_equal files, @site.filter_entries(excludes + files + ["excludeA"])
end
should "not filter entries within include" do
includes = %w[_index.html .htaccess include*]
files = %w[index.html _index.html .htaccess includeA]
@site.include = includes
assert_equal files, @site.filter_entries(files)
end
should "filter symlink entries when safe mode enabled" do
stub(Jekyll).configuration do
Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'safe' => true})
end
site = Site.new(Jekyll.configuration)
stub(File).symlink?('symlink.js') {true}
files = %w[symlink.js]
assert_equal [], site.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)
end
should "not include symlinks in safe mode" do
stub(Jekyll).configuration do
Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'safe' => true})
end
site = Site.new(Jekyll.configuration)
site.read_directories("symlink-test")
assert_equal [], site.pages
assert_equal [], site.static_files
end
should "include symlinks in unsafe mode" do
stub(Jekyll).configuration do
Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'safe' => false})
end
site = Site.new(Jekyll.configuration)
site.read_directories("symlink-test")
assert_not_equal [], site.pages
assert_not_equal [], site.static_files
end
end
end

View File

@ -179,69 +179,6 @@ class TestSite < Test::Unit::TestCase
assert_equal 4, @site.categories['foo'].size assert_equal 4, @site.categories['foo'].size
end end
should "filter entries" do
ent1 = %w[foo.markdown bar.markdown baz.markdown #baz.markdown#
.baz.markdow foo.markdown~]
ent2 = %w[.htaccess _posts _pages bla.bla]
assert_equal %w[foo.markdown bar.markdown baz.markdown], @site.filter_entries(ent1)
assert_equal %w[.htaccess bla.bla], @site.filter_entries(ent2)
end
should "filter entries with exclude" do
excludes = %w[README TODO]
files = %w[index.html site.css .htaccess]
@site.exclude = excludes + ["exclude*"]
assert_equal files, @site.filter_entries(excludes + files + ["excludeA"])
end
should "not filter entries within include" do
includes = %w[_index.html .htaccess include*]
files = %w[index.html _index.html .htaccess includeA]
@site.include = includes
assert_equal files, @site.filter_entries(files)
end
should "filter symlink entries when safe mode enabled" do
stub(Jekyll).configuration do
Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'safe' => true})
end
site = Site.new(Jekyll.configuration)
stub(File).symlink?('symlink.js') {true}
files = %w[symlink.js]
assert_equal [], site.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)
end
should "not include symlinks in safe mode" do
stub(Jekyll).configuration do
Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'safe' => true})
end
site = Site.new(Jekyll.configuration)
site.read_directories("symlink-test")
assert_equal [], site.pages
assert_equal [], site.static_files
end
should "include symlinks in unsafe mode" do
stub(Jekyll).configuration do
Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'safe' => false})
end
site = Site.new(Jekyll.configuration)
site.read_directories("symlink-test")
assert_not_equal [], site.pages
assert_not_equal [], site.static_files
end
context 'error handling' do context 'error handling' do
should "raise if destination is included in source" do should "raise if destination is included in source" do
stub(Jekyll).configuration do stub(Jekyll).configuration do