class JekyllData::Reader
Public Class Methods
# 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 data files within theme-gem.
Returns nothing.
# File lib/jekyll-data/reader.rb, line 19 def read super read_theme_data end
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
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
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
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
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
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
# File lib/jekyll-data/reader.rb, line 215 def print_clear_line print "" end
# File lib/jekyll-data/reader.rb, line 211 def print_dashes print "", @dashes end
Prints key as logger and value as [message]
# File lib/jekyll-data/reader.rb, line 146 def print_hash(key, value) if value.length > @width print_long_string value, "#{key}:" else print "#{key}:", value.cyan end end
# File lib/jekyll-data/reader.rb, line 207 def print_inner_subkey(key) print "#{key}:", @dashes end
Prints the site.data in color
# File lib/jekyll-data/reader.rb, line 171 def print_key(key) print_clear_line print "Data Key:", " #{key} ".center(@width, "=") print_clear_line end
Print only logger appended with a colon
# File lib/jekyll-data/reader.rb, line 203 def print_label(key) print_value " #{key} ".center(@width, "-") end
# File lib/jekyll-data/reader.rb, line 154 def print_list(item) if item.length > @width print_long_string item, "-" else print "-", item.cyan end end
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.
# File lib/jekyll-data/reader.rb, line 137 def print_long_string(string, label = "") strings = string.scan(%r!(.{1,#{@width}})(\s+|\W|\Z)!).map { |s| s.join.strip } first_line = strings.first.cyan label.empty? ? print_value(first_line) : print(label, first_line) strings[1..-1].each { |s| print_value s.cyan } end
# File lib/jekyll-data/reader.rb, line 162 def print_string(str) if str.length > @width print_long_string str else print_value str.inspect end end
Prints label, keys and values of mappings
# File lib/jekyll-data/reader.rb, line 178 def print_subkey_and_value(key, value) print_label key value.each do |subkey, val| if val.is_a? Hash print_inner_subkey subkey inspect_inner_hash val elsif val.is_a? Array print_inner_subkey subkey extract_hashes_and_print val elsif val.is_a? String print_hash subkey, val end end end
Print only logger, [topic] = nil
# File lib/jekyll-data/reader.rb, line 194 def print_value(value) if value.is_a? Array extract_hashes_and_print value else print "", value end end