Class: Jekyll::DataReader
- Inherits:
-
Object
- Object
- Jekyll::DataReader
- Defined in:
- lib/jekyll/readers/data_reader.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
Instance Method Summary collapse
-
#initialize(site) ⇒ DataReader
constructor
A new instance of DataReader.
-
#read(dir) ⇒ Object
Read all the files in <source>/<dir>/_drafts and create a new Draft object with each one.
-
#read_data_file(path) ⇒ Object
Determines how to read a data file.
-
#read_data_to(dir, data) ⇒ Object
Read and parse all yaml files under <dir> and add them to the <data> variable.
- #sanitize_filename(name) ⇒ Object
Constructor Details
#initialize(site) ⇒ DataReader
Returns a new instance of DataReader.
4 5 6 7 |
# File 'lib/jekyll/readers/data_reader.rb', line 4 def initialize(site) @site = site @content = {} end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
3 4 5 |
# File 'lib/jekyll/readers/data_reader.rb', line 3 def content @content end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
3 4 5 |
# File 'lib/jekyll/readers/data_reader.rb', line 3 def site @site end |
Instance Method Details
#read(dir) ⇒ Object
Read all the files in <source>/<dir>/_drafts and create a new Draft object with each one.
dir - The String relative path of the directory to read.
Returns nothing.
15 16 17 18 19 |
# File 'lib/jekyll/readers/data_reader.rb', line 15 def read(dir) base = site.in_source_dir(dir) read_data_to(base, @content) @content end |
#read_data_file(path) ⇒ Object
Determines how to read a data file.
Returns the contents of the data file.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/jekyll/readers/data_reader.rb', line 51 def read_data_file(path) case File.extname(path).downcase when '.csv' CSV.read(path, { :headers => true, :encoding => site.config['encoding'] }).map(&:to_hash) else SafeYAML.load_file(path) end end |
#read_data_to(dir, data) ⇒ Object
Read and parse all yaml files under <dir> and add them to the <data> variable.
dir - The string absolute path of the directory to read. data - The variable to which data will be added.
Returns nothing
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/jekyll/readers/data_reader.rb', line 28 def read_data_to(dir, data) return unless File.directory?(dir) && (!site.safe || !File.symlink?(dir)) entries = Dir.chdir(dir) do Dir['*.{yaml,yml,json,csv}'] + Dir['*'].select { |fn| File.directory?(fn) } end entries.each do |entry| path = @site.in_source_dir(dir, entry) next if File.symlink?(path) && site.safe key = sanitize_filename(File.basename(entry, '.*')) if File.directory?(path) read_data_to(path, data[key] = {}) else data[key] = read_data_file(path) end end end |
#sanitize_filename(name) ⇒ Object
63 64 65 66 67 |
# File 'lib/jekyll/readers/data_reader.rb', line 63 def sanitize_filename(name) name.gsub!(/[^\w\s-]+/, '') name.gsub!(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2') name.gsub(/\s+/, '_') end |