Skip to content

Commit

Permalink
Autoload csv files from data directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Floppy committed Aug 16, 2014
1 parent 59b6caf commit 687176e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions History.markdown
Expand Up @@ -4,6 +4,8 @@

### Minor Enhancements

* Add support for CSV files in the `_data` directory

### Bug Fixes

### Development Fixes
Expand Down
14 changes: 14 additions & 0 deletions features/data.feature
Expand Up @@ -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:
Expand Down
10 changes: 8 additions & 2 deletions lib/jekyll/site.rb
@@ -1,4 +1,5 @@
# encoding: UTF-8
require 'csv'

module Jekyll
class Site
Expand Down Expand Up @@ -212,7 +213,7 @@ 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) }
Dir['*.{yaml,yml,json,csv}'] + Dir['*'].select { |fn| File.directory?(fn) }
end

entries.each do |entry|
Expand All @@ -223,7 +224,12 @@ def read_data_to(dir, data)
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
Expand Down

0 comments on commit 687176e

Please sign in to comment.