class Jekyll::DataReader

def config

def config
  @config ||= site.config
end

def convert_row(row)

Returns:
  • (Array, Hash) -

Parameters:
  • row (Array, CSV::Row) --
def convert_row(row)
  row.instance_of?(CSV::Row) ? row.to_hash : row
end

def csv_config

Returns:
  • (Hash) -
def csv_config
  @csv_config ||= read_config("csv_reader")
end

def initialize(site, in_source_dir: nil)

def initialize(site, in_source_dir: nil)
  @site = site
  @content = {}
  @entry_filter = EntryFilter.new(site)
  @in_source_dir = in_source_dir || @site.method(:in_source_dir)
  @source_dir = @in_source_dir.call("/")
end

def read(dir)

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

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

Read all the files in and adds them to @content
def read(dir)
  base = @in_source_dir.call(dir)
  read_data_to(base, @content)
  @content
end

def read_config(config_key, overrides = {})

Other tags:
    See: https://ruby-doc.org/stdlib-2.5.0/libdoc/csv/rdoc/CSV.html#Converters -

Returns:
  • (Hash) -

Parameters:
  • overrides (Hash) --
  • config_key (String) --
def read_config(config_key, overrides = {})
  reader_config = config[config_key] || {}
  defaults = {
    :converters => reader_config.fetch("csv_converters", []).map(&:to_sym),
    :headers    => reader_config.fetch("headers", true),
    :encoding   => reader_config.fetch("encoding", config["encoding"]),
  }
  defaults.merge(overrides)
end

def read_data_file(path)

Returns the contents of the data file.

Determines how to read a data file.
def read_data_file(path)
  Jekyll.logger.debug "Reading:", path.sub(@source_dir, "")
  case File.extname(path).downcase
  when ".csv"
    CSV.read(path, **csv_config).map { |row| convert_row(row) }
  when ".tsv"
    CSV.read(path, **tsv_config).map { |row| convert_row(row) }
  else
    SafeYAML.load_file(path)
  end
end

def read_data_to(dir, data)

Returns nothing

data - The variable to which data will be added.
dir - The string absolute path of the directory to read.

files under and add them to the variable.
Read and parse all .yaml, .yml, .json, .csv and .tsv
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 = @in_source_dir.call(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

def sanitize_filename(name)

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

def tsv_config

Returns:
  • (Hash) -
def tsv_config
  @tsv_config ||= read_config("tsv_reader", { :col_sep => "\t" })
end