Class: Jekyll::DataReader

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/readers/data_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ DataReader

Returns a new instance of DataReader.


7
8
9
10
11
# File 'lib/jekyll/readers/data_reader.rb', line 7

def initialize(site)
  @site = site
  @content = {}
  @entry_filter = EntryFilter.new(site)
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content


5
6
7
# File 'lib/jekyll/readers/data_reader.rb', line 5

def content
  @content
end

#siteObject (readonly)

Returns the value of attribute site


5
6
7
# File 'lib/jekyll/readers/data_reader.rb', line 5

def site
  @site
end

Instance Method Details

#read(dir) ⇒ Object

Read all the files in <dir> and adds them to @content

dir - The String relative path of the directory to read.

Returns @content, a Hash of the .yaml, .yml, .json, and .csv files in the base directory


19
20
21
22
23
# File 'lib/jekyll/readers/data_reader.rb', line 19

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.


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jekyll/readers/data_reader.rb', line 55

def read_data_file(path)
  case File.extname(path).downcase
  when ".csv"
    CSV.read(path,
             :headers  => true,
             :encoding => site.config["encoding"]).map(&:to_hash)
  when ".tsv"
    CSV.read(path,
             :col_sep  => "\t",
             :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, .yml, .json, .csv and .tsv 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


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jekyll/readers/data_reader.rb', line 32

def read_data_to(dir, data)
  return unless File.directory?(dir) && !@entry_filter.symlink?(dir)

  entries = Dir.chdir(dir) do
    Dir["*.{yaml,yml,json,csv,tsv}"] + Dir["*"].select { |fn| File.directory?(fn) }
  end

  entries.each do |entry|
    path = @site.in_source_dir(dir, entry)
    next if @entry_filter.symlink?(path)

    if File.directory?(path)
      read_data_to(path, data[sanitize_filename(entry)] = {})
    else
      key = sanitize_filename(File.basename(entry, ".*"))
      data[key] = read_data_file(path)
    end
  end
end

#sanitize_filename(name) ⇒ Object


71
72
73
74
# File 'lib/jekyll/readers/data_reader.rb', line 71

def sanitize_filename(name)
  name.gsub(%r![^\w\s-]+|(?<=^|\b\s)\s+(?=$|\s?\b)!, "")
    .gsub(%r!\s+!, "_")
end