Class: JekyllData::ThemeDataReader

Inherits:
Jekyll::DataReader
  • Object
show all
Defined in:
lib/jekyll-data/theme_data_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ ThemeDataReader

Returns a new instance of ThemeDataReader.



7
8
9
10
11
# File 'lib/jekyll-data/theme_data_reader.rb', line 7

def initialize(site)
  super

  @source_dir = site.in_theme_dir("/")
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



5
6
7
# File 'lib/jekyll-data/theme_data_reader.rb', line 5

def content
  @content
end

#siteObject (readonly)

Returns the value of attribute site.



5
6
7
# File 'lib/jekyll-data/theme_data_reader.rb', line 5

def site
  @site
end

Instance Method Details

#read(dir) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/jekyll-data/theme_data_reader.rb', line 13

def read(dir)
  return unless site.theme && site.theme.data_path

  base = site.in_theme_dir(dir)
  read_data_to(base, @content)
  @content
end

#read_data_to(dir, data) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jekyll-data/theme_data_reader.rb', line 21

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_theme_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