From bf3a20b2d71342540ccd3e31c487178502ce1305 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Fri, 9 May 2014 10:13:12 -0400 Subject: [PATCH 1/4] allow json files in _data dir --- lib/jekyll/site.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 288662a5..5f1580bb 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -198,7 +198,7 @@ module Jekyll base = File.join(source, dir) return unless File.directory?(base) && (!safe || !File.symlink?(base)) - entries = Dir.chdir(base) { Dir['*.{yaml,yml}'] } + entries = Dir.chdir(base) { Dir['*.{yaml,yml,json}'] } entries.delete_if { |e| File.directory?(File.join(base, e)) } entries.each do |entry| From 5129a3ccc3bca2d47e76402be207f5caf438463d Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Fri, 9 May 2014 10:29:37 -0400 Subject: [PATCH 2/4] add tests for json data --- features/data.feature | 13 ++++++++++++- test/source/_data/members.json | 12 ++++++++++++ test/test_site.rb | 10 ++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/source/_data/members.json diff --git a/features/data.feature b/features/data.feature index 9bfac1ee..45f5554e 100644 --- a/features/data.feature +++ b/features/data.feature @@ -33,6 +33,18 @@ Feature: Data And I should see "Jack" in "_site/index.html" And I should see "Leon" in "_site/index.html" + Scenario: autoload *.json files in _data directory + Given I have a _data directory + And I have a "_data/members.json" file with content: + """ + [{"name": "Jack", "age": 28},{"name": "Leon", "age": 34}] + """ + And I have an "index.html" page that contains "{% for member in site.data.members %}{{member.name}}{% endfor %}" + When I run jekyll build + Then the "_site/index.html" file should exist + And I should see "Jack" in "_site/index.html" + And I should see "Leon" in "_site/index.html" + Scenario: autoload *.yml files in _data directory with space in file name Given I have a _data directory And I have a "_data/team members.yml" file with content: @@ -62,4 +74,3 @@ Feature: Data Then the "_site/index.html" file should exist And I should see "Jack" in "_site/index.html" And I should see "Leon" in "_site/index.html" - diff --git a/test/source/_data/members.json b/test/source/_data/members.json new file mode 100644 index 00000000..5ca4821f --- /dev/null +++ b/test/source/_data/members.json @@ -0,0 +1,12 @@ +[ + { + "name": "Jack", + "age": 27, + "blog": "http://example.com/jack" + }, + { + "name": "John", + "age": 32, + "blog": "http://example.com/john" + } +] diff --git a/test/test_site.rb b/test/test_site.rb index c5d78594..94263491 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -378,6 +378,16 @@ class TestSite < Test::Unit::TestCase assert_equal site.site_payload['site']['data']['languages'], file_content end + should 'auto load json files' do + site = Site.new(Jekyll.configuration) + site.process + + file_content = SafeYAML.load_file(File.join(source_dir, '_data', 'members.json')) + + assert_equal site.data['members'], file_content + assert_equal site.site_payload['site']['data']['members'], file_content + end + should "load symlink files in unsafe mode" do site = Site.new(Jekyll.configuration.merge({'safe' => false})) site.process From d3f9da7645397d63aecade6c1a64d204ffa1350a Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Fri, 9 May 2014 10:30:36 -0400 Subject: [PATCH 3/4] document json support --- site/docs/datafiles.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/site/docs/datafiles.md b/site/docs/datafiles.md index 1dbe4097..fd6b0d51 100644 --- a/site/docs/datafiles.md +++ b/site/docs/datafiles.md @@ -10,7 +10,7 @@ In addition to the [built-in variables](../variables/) available from Jekyll, you can specify your own custom data that can be accessed via the [Liquid templating system](http://wiki.github.com/shopify/liquid/liquid-for-designers). -Jekyll supports loading data from [YAML](http://yaml.org/) files located in the +Jekyll supports loading data from [YAML](http://yaml.org/) and [JSON](http://www.json.org/) files located in the `_data` directory. This powerful feature allows you to avoid repetition in your templates and to @@ -22,7 +22,7 @@ Plugins/themes can also leverage Data Files to set configuration variables. As explained on the [directory structure](../structure/) page, the `_data` folder is where you can store additional data for Jekyll to use when generating -your site. These files must be YAML files (using either the `.yml` or `.yaml` +your site. These files must be YAML files (using either the `.yml`, `.yaml` or `json` extension) and they will be accessible via `site.data`. ## Example: List of members From 1315112ab51f292cdf95fc2b857de32f01c20eba Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Fri, 9 May 2014 10:32:18 -0400 Subject: [PATCH 4/4] dot json; --- site/docs/datafiles.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/docs/datafiles.md b/site/docs/datafiles.md index fd6b0d51..05e59345 100644 --- a/site/docs/datafiles.md +++ b/site/docs/datafiles.md @@ -22,7 +22,7 @@ Plugins/themes can also leverage Data Files to set configuration variables. As explained on the [directory structure](../structure/) page, the `_data` folder is where you can store additional data for Jekyll to use when generating -your site. These files must be YAML files (using either the `.yml`, `.yaml` or `json` +your site. These files must be YAML files (using either the `.yml`, `.yaml` or `.json` extension) and they will be accessible via `site.data`. ## Example: List of members