Merge pull request #2395 from rdumont/data-subdirectories

This commit is contained in:
Parker Moore 2014-05-21 01:09:15 -04:00
commit 3c6377d665
5 changed files with 110 additions and 5 deletions

View File

@ -60,6 +60,35 @@ Feature: Data
And I should see "Jack" in "_site/index.html" And I should see "Jack" in "_site/index.html"
And I should see "Leon" 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: folders should have precedence over files with the same name
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 a "_data/categories.yaml" file with content:
"""
dairy:
name: Should not display this
"""
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 Scenario: should be backward compatible with site.data in _config.yml
Given I have a "_config.yml" file with content: Given I have a "_config.yml" file with content:
""" """

View File

@ -196,17 +196,33 @@ module Jekyll
# Returns nothing # Returns nothing
def read_data(dir) def read_data(dir)
base = File.join(source, 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}'] } # Read and parse all yaml files under <dir> and add them to the
entries.delete_if { |e| File.directory?(File.join(base, e)) } # <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| entries.each do |entry|
path = File.join(source, dir, entry) path = File.join(dir, entry)
next if File.symlink?(path) && safe next if File.symlink?(path) && safe
key = sanitize_filename(File.basename(entry, '.*')) 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
end end

View File

@ -61,3 +61,47 @@ You can now render the list of members in a template:
</ul> </ul>
{% endraw %} {% endraw %}
{% endhighlight %} {% endhighlight %}
## Example: Organizations
Data files can also be placed in sub-folders of the `_data` folder. Each folder level will be added to a variable's namespace. The example bellow shows how GitHub organizations could be defined separately in a file under the `orgs` folder:
In `_data/orgs/jekyll.yml`:
{% highlight yaml %}
username: jekyll
name: Jekyll
members:
- name: Tom Preston-Werner
github: mojombo
- name: Parker Moore
github: parkr
{% endhighlight %}
In `_data/orgs/doeorg.yml`:
{% highlight yaml %}
username: doeorg
name: Doe Org
members:
- name: John Doe
github: jdoe
{% endhighlight %}
The organizations can then be accessed via `site.data.orgs`, followed by the file name:
{% highlight html %}
{% raw %}
<ul>
{% for org in site.data.orgs %}
<li>
<a href="https://github.com/{{ org.username }}">
{{ org.name }}
</a>
({{ org.members | size }} members)
</li>
{% endfor %}
</ul>
{% endraw %}
{% endhighlight %}

View File

@ -0,0 +1,6 @@
name: Dairy
products:
- name: cheese
price: 5.3
- name: milk
price: 2.5

View File

@ -389,6 +389,16 @@ class TestSite < Test::Unit::TestCase
assert_equal site.site_payload['site']['data']['members'], file_content assert_equal site.site_payload['site']['data']['members'], file_content
end 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 should "load symlink files in unsafe mode" do
site = Site.new(Jekyll.configuration.merge({'safe' => false})) site = Site.new(Jekyll.configuration.merge({'safe' => false}))
site.process site.process