Extracted `read_data_to` 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 20:05:10 +01:00
parent e586105b46
commit 5b0e2a294d
2 changed files with 28 additions and 28 deletions

View File

@ -74,6 +74,33 @@ module Jekyll
end
end
# Read and parse all yaml files under <dir> and add them to the
# <data> variable.
#
# dir - The string absolute path of the directory to read.
# data - The variable to which data will be added.
#
# Returns nothing
def read_data_to(dir, data)
return unless File.directory?(dir) && (!site.safe || !File.symlink?(dir))
entries = Dir.chdir(dir) do
Dir['*.{yaml,yml,json,csv}'] + Dir['*'].select { |fn| File.directory?(fn) }
end
entries.each do |entry|
path = in_source_dir(dir, entry)
next if File.symlink?(path) && site.safe
key = sanitize_filename(File.basename(entry, '.*'))
if File.directory?(path)
read_data_to(path, data[key] = {})
else
data[key] = read_data_file(path)
end
end
end
def sanitize_filename(name)
name.gsub!(/[^\w\s_-]+/, '')
name.gsub!(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')

View File

@ -211,34 +211,7 @@ module Jekyll
# Returns nothing
def read_data(dir)
base = reader.in_source_dir(dir)
read_data_to(base, self.data)
end
# Read and parse all yaml files under <dir> and add them to the
# <data> variable.
#
# dir - The string absolute path of the directory to read.
# data - The variable to which data will be added.
#
# Returns nothing
def read_data_to(dir, data)
return unless File.directory?(dir) && (!safe || !File.symlink?(dir))
entries = Dir.chdir(dir) do
Dir['*.{yaml,yml,json,csv}'] + Dir['*'].select { |fn| File.directory?(fn) }
end
entries.each do |entry|
path = reader.in_source_dir(dir, entry)
next if File.symlink?(path) && safe
key = reader.sanitize_filename(File.basename(entry, '.*'))
if File.directory?(path)
read_data_to(path, data[key] = {})
else
data[key] = reader.read_data_file(path)
end
end
reader.read_data_to(base, self.data)
end
# Read in all collections specified in the configuration