Allow subdirectories in _data
This commit is contained in:
parent
8f90ba82a4
commit
604fb3286c
|
@ -60,6 +60,18 @@ Feature: Data
|
|||
And I should see "Jack" in "_site/index.html"
|
||||
And I should see "Leon" in "_site/index.html"
|
||||
|
||||
Scenario: autoload *.yaml files in subdirectories in _data directory
|
||||
Given I have a _data directory
|
||||
And I have a _data/categories directory
|
||||
And I have a "_data/categories/dairy.yaml" file with content:
|
||||
"""
|
||||
name: Dairy Products
|
||||
"""
|
||||
And I have an "index.html" page that contains "{{ site.data.categories.dairy.name }}"
|
||||
When I run jekyll build
|
||||
Then the "_site/index.html" file should exist
|
||||
And I should see "Dairy Products" in "_site/index.html"
|
||||
|
||||
Scenario: should be backward compatible with site.data in _config.yml
|
||||
Given I have a "_config.yml" file with content:
|
||||
"""
|
||||
|
|
|
@ -196,17 +196,33 @@ module Jekyll
|
|||
# Returns nothing
|
||||
def read_data(dir)
|
||||
base = File.join(source, dir)
|
||||
return unless File.directory?(base) && (!safe || !File.symlink?(base))
|
||||
read_data_to(base, self.data)
|
||||
end
|
||||
|
||||
entries = Dir.chdir(base) { Dir['*.{yaml,yml,json}'] }
|
||||
entries.delete_if { |e| File.directory?(File.join(base, e)) }
|
||||
# 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}'] + Dir['*'].select { |fn| File.directory?(fn) }
|
||||
end
|
||||
|
||||
entries.each do |entry|
|
||||
path = File.join(source, dir, entry)
|
||||
path = File.join(dir, entry)
|
||||
next if File.symlink?(path) && safe
|
||||
|
||||
key = sanitize_filename(File.basename(entry, '.*'))
|
||||
self.data[key] = SafeYAML.load_file(path)
|
||||
if File.directory?(path)
|
||||
read_data_to(path, data[key] = {})
|
||||
else
|
||||
data[key] = SafeYAML.load_file(path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
name: Dairy
|
||||
products:
|
||||
- name: cheese
|
||||
price: 5.3
|
||||
- name: milk
|
||||
price: 2.5
|
|
@ -388,6 +388,16 @@ class TestSite < Test::Unit::TestCase
|
|||
assert_equal site.site_payload['site']['data']['members'], file_content
|
||||
end
|
||||
|
||||
should 'auto load yaml files in subdirectory' do
|
||||
site = Site.new(Jekyll.configuration)
|
||||
site.process
|
||||
|
||||
file_content = SafeYAML.load_file(File.join(source_dir, '_data', 'categories', 'dairy.yaml'))
|
||||
|
||||
assert_equal site.data['categories']['dairy'], file_content
|
||||
assert_equal site.site_payload['site']['data']['categories']['dairy'], file_content
|
||||
end
|
||||
|
||||
should "load symlink files in unsafe mode" do
|
||||
site = Site.new(Jekyll.configuration.merge({'safe' => false}))
|
||||
site.process
|
||||
|
|
Loading…
Reference in New Issue