From 687176e22c49484b502bb341fabf139ee6ecb25e Mon Sep 17 00:00:00 2001 From: James Smith Date: Sat, 16 Aug 2014 14:54:14 +0100 Subject: [PATCH 1/4] Autoload csv files from data directory --- History.markdown | 2 ++ features/data.feature | 14 ++++++++++++++ lib/jekyll/site.rb | 10 ++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/History.markdown b/History.markdown index 58eb7b6b..fbc857ae 100644 --- a/History.markdown +++ b/History.markdown @@ -4,6 +4,8 @@ ### Minor Enhancements +* Add support for CSV files in the `_data` directory + ### Bug Fixes ### Development Fixes diff --git a/features/data.feature b/features/data.feature index 4f0e32ed..e44be8a5 100644 --- a/features/data.feature +++ b/features/data.feature @@ -45,6 +45,20 @@ Feature: Data And I should see "Jack" in "_site/index.html" And I should see "Leon" in "_site/index.html" + Scenario: autoload *.csv files in _data directory + Given I have a _data directory + And I have a "_data/members.csv" file with content: + """ + name,age + Jack,28 + Leon,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: diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 5aee3e72..3b5da452 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -1,4 +1,5 @@ # encoding: UTF-8 +require 'csv' module Jekyll class Site @@ -212,7 +213,7 @@ module Jekyll return unless File.directory?(dir) && (!safe || !File.symlink?(dir)) entries = Dir.chdir(dir) do - Dir['*.{yaml,yml,json}'] + Dir['*'].select { |fn| File.directory?(fn) } + Dir['*.{yaml,yml,json,csv}'] + Dir['*'].select { |fn| File.directory?(fn) } end entries.each do |entry| @@ -223,7 +224,12 @@ module Jekyll if File.directory?(path) read_data_to(path, data[key] = {}) else - data[key] = SafeYAML.load_file(path) + case File.extname(path).downcase + when '.csv' + data[key] = CSV.read(path, headers: true).map{|x| x.to_hash} + else + data[key] = SafeYAML.load_file(path) + end end end end From 3a8992314245a79a546a7ee0a7c6b0fd50845f46 Mon Sep 17 00:00:00 2001 From: James Smith Date: Sat, 16 Aug 2014 14:55:37 +0100 Subject: [PATCH 2/4] link issue number --- History.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/History.markdown b/History.markdown index fbc857ae..c23c9840 100644 --- a/History.markdown +++ b/History.markdown @@ -4,7 +4,7 @@ ### Minor Enhancements -* Add support for CSV files in the `_data` directory +* Add support for CSV files in the `_data` directory (#2761) ### Bug Fixes From 866935dadf75ca6d04fd3486381b2ddd7e96f79e Mon Sep 17 00:00:00 2001 From: James Smith Date: Sat, 16 Aug 2014 20:26:29 +0100 Subject: [PATCH 3/4] map with proc for CSV loading --- 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 3b5da452..e37829d2 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -226,7 +226,7 @@ module Jekyll else case File.extname(path).downcase when '.csv' - data[key] = CSV.read(path, headers: true).map{|x| x.to_hash} + data[key] = CSV.read(path, headers: true).map(&:to_hash) else data[key] = SafeYAML.load_file(path) end From cccfda7de877a899ffb0ffdffa2fc5e4c2fae593 Mon Sep 17 00:00:00 2001 From: James Smith Date: Sun, 17 Aug 2014 08:34:38 +0100 Subject: [PATCH 4/4] hashrockets in CSV loading --- 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 e37829d2..502e3ecb 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -226,7 +226,7 @@ module Jekyll else case File.extname(path).downcase when '.csv' - data[key] = CSV.read(path, headers: true).map(&:to_hash) + data[key] = CSV.read(path, :headers => true).map(&:to_hash) else data[key] = SafeYAML.load_file(path) end