Merge pull request #2761 from theodi/csv-data
This commit is contained in:
commit
c4a2ac2c4b
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
### Minor Enhancements
|
### Minor Enhancements
|
||||||
|
|
||||||
|
* Add support for CSV files in the `_data` directory (#2761)
|
||||||
|
|
||||||
### Bug Fixes
|
### Bug Fixes
|
||||||
|
|
||||||
### Development Fixes
|
### Development Fixes
|
||||||
|
|
|
@ -45,6 +45,20 @@ 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 *.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
|
Scenario: autoload *.yml files in _data directory with space in file name
|
||||||
Given I have a _data directory
|
Given I have a _data directory
|
||||||
And I have a "_data/team members.yml" file with content:
|
And I have a "_data/team members.yml" file with content:
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
# encoding: UTF-8
|
# encoding: UTF-8
|
||||||
|
require 'csv'
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class Site
|
class Site
|
||||||
|
@ -212,7 +213,7 @@ module Jekyll
|
||||||
return unless File.directory?(dir) && (!safe || !File.symlink?(dir))
|
return unless File.directory?(dir) && (!safe || !File.symlink?(dir))
|
||||||
|
|
||||||
entries = Dir.chdir(dir) do
|
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
|
end
|
||||||
|
|
||||||
entries.each do |entry|
|
entries.each do |entry|
|
||||||
|
@ -222,11 +223,16 @@ module Jekyll
|
||||||
key = sanitize_filename(File.basename(entry, '.*'))
|
key = sanitize_filename(File.basename(entry, '.*'))
|
||||||
if File.directory?(path)
|
if File.directory?(path)
|
||||||
read_data_to(path, data[key] = {})
|
read_data_to(path, data[key] = {})
|
||||||
|
else
|
||||||
|
case File.extname(path).downcase
|
||||||
|
when '.csv'
|
||||||
|
data[key] = CSV.read(path, :headers => true).map(&:to_hash)
|
||||||
else
|
else
|
||||||
data[key] = SafeYAML.load_file(path)
|
data[key] = SafeYAML.load_file(path)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Read in all collections specified in the configuration
|
# Read in all collections specified in the configuration
|
||||||
#
|
#
|
||||||
|
|
Loading…
Reference in New Issue