class JekyllData::Reader

Public Class Methods

new(site) click to toggle source
# File lib/jekyll-data/reader.rb, line 7
def initialize(site)
  @site = site
  @theme = site.theme

  if @theme.data_path
    @theme_data_files = Dir[File.join(@theme.data_path, "**", "*.{yaml,yml,json,csv,tsv}")]
  end
end

Public Instance Methods

read() click to toggle source

Read data files within theme-gem.

Returns nothing.

Calls superclass method
# File lib/jekyll-data/reader.rb, line 19
def read
  super
  read_theme_data
end
read_theme_data() click to toggle source

Read data files within a theme gem and add them to internal data

Returns a hash appended with new data

# File lib/jekyll-data/reader.rb, line 27
def read_theme_data
  if @theme.data_path
    #
    # show contents of "<theme>/_data/" dir being read while degugging.
    inspect_theme_data
    theme_data = ThemeDataReader.new(site).read(site.config["data_dir"])
    @site.data = Jekyll::Utils.deep_merge_hashes(theme_data, @site.data)
    #
    # show contents of merged site.data hash while debugging with
    # additional --show-data switch.
    inspect_merged_hash if site.config["show-data"] && site.config["verbose"]
  end
end

Private Instance Methods

extract_hashes_and_print(array) click to toggle source

If an array of strings, print. Otherwise assume as an array of hashes (sequences) that needs further analysis.

# File lib/jekyll-data/reader.rb, line 115
def extract_hashes_and_print(array)
  array.each do |entry|
    if entry.is_a? String
      print_list entry
    else
      inspect_inner_hash entry
    end
  end
end
inspect_hash(hash) click to toggle source

Dissect the (merged) site.data hash and print its contents

  • Print the key string(s)

  • Individually analyse the hash values and extract contents to output.

# File lib/jekyll-data/reader.rb, line 86
def inspect_hash(hash)
  hash.each do |key, value|
    print_key key
    if value.is_a? Hash
      inspect_inner_hash value
    elsif value.is_a? Array
      extract_hashes_and_print value
    else
      print_string value.to_s
    end
  end
end
inspect_inner_hash(hash) click to toggle source

Analyse deeper hashes and extract contents to output

# File lib/jekyll-data/reader.rb, line 100
def inspect_inner_hash(hash)
  hash.each do |key, value|
    if value.is_a? Array
      print_label key
      extract_hashes_and_print value
    elsif value.is_a? Hash
      print_subkey_and_value key, value
    else
      print_hash key, value
    end
  end
end
inspect_merged_hash() click to toggle source

Private: (only while debugging)

Print contents of the merged data hash

# File lib/jekyll-data/reader.rb, line 65
def inspect_merged_hash
  Jekyll.logger.debug "Inspecting:", "Site Data >>"

  # the width of generated logger[message]
  @width = 50
  @dashes = "-" * @width

  inspect_hash @site.data
  print_clear_line
end
inspect_theme_data() click to toggle source

Private: (only while debugging)

Print a list of data file(s) within the theme-gem

# File lib/jekyll-data/reader.rb, line 47
def inspect_theme_data
  print_clear_line
  Jekyll.logger.debug "Reading:", "Theme Data Files..."
  @theme_data_files.each { |file| Jekyll.logger.debug "", file }
  print_clear_line
  Jekyll.logger.debug "Merging:", "Theme Data Hash..."

  unless site.config["show-data"] && site.config["verbose"]
    Jekyll.logger.debug "", "use --show-data with --verbose to output " \
                            "merged Data Hash.".cyan
    print_clear_line
  end
end
print(topic, message = "") click to toggle source

Redefine Jekyll Loggers to have the [topic] indented by 30. (rjust by just 29 to accomodate the additional whitespace added by Jekyll)

print_clear_line() click to toggle source
print_dashes() click to toggle source
print_hash(key, value) click to toggle source

Prints key as logger and value as [message]

print_inner_subkey(key) click to toggle source
print_key(key) click to toggle source

Prints the site.data in color

print_label(key) click to toggle source

Print only logger appended with a colon

print_list(item) click to toggle source
print_long_string(string, label = "") click to toggle source

Splits a string longer than the value of '@width' into smaller strings and prints each line as a logger

string - the long string

label - optional text to designate the printed lines.

print_string(str) click to toggle source
print_subkey_and_value(key, value) click to toggle source

Prints label, keys and values of mappings

print_value(value) click to toggle source

Print only logger, [topic] = nil