module WaxTasks::Utils

Utility helper methods

Public Class Methods

add_yaml_front_matter_to_file(file) click to toggle source
# File lib/wax_tasks/utils.rb, line 140
def self.add_yaml_front_matter_to_file(file)
  front_matter = "---\nlayout: none\n---\n"
  filestring = File.read file
  return if filestring.start_with? front_matter

  File.open(file, 'w') do |f|
    f.puts front_matter
    f.puts filestring
  end
end
assert_pids(data) click to toggle source

Checks and asserts presence of `pid` value for each item

@param data [Array] array of hashes each representing a collection item @return [Array] same data unless a an item is missing the key `pid` @raise WaxTasks::Error::MissingPid

# File lib/wax_tasks/utils.rb, line 29
def self.assert_pids(data)
  data.each_with_index { |d, i| raise Error::MissingPid, "Collection is missing pid for item #{i}.\nHint: review common .csv formatting issues (such as hidden characters) in the documentation: https://minicomp.github.io/wiki/wax/preparing-your-collection-data/metadata/" unless d.key? 'pid' }
  data
end
assert_unique(data) click to toggle source

Checks and asserts uniqueness of `pid` value for each item

@param data [Array] array of hashes each representing a collection item @return [Array] same data unless an item has non-unique value for `pid` @raise WaxTasks::Error::NonUniquePid

# File lib/wax_tasks/utils.rb, line 39
def self.assert_unique(data)
  pids = data.map { |d| d['pid'] }
  not_unique = pids.select { |p| pids.count(p) > 1 }.uniq! || []
  raise Error::NonUniquePid, "#{@name} has the following nonunique pids:\n#{not_unique}" unless not_unique.empty?

  data
end
content_clean(str) click to toggle source

Scrubs yaml, liquid, html, and etc from content strings @return [String]

# File lib/wax_tasks/utils.rb, line 89
def self.content_clean(str)
  str.gsub!(/\A---(.|\n)*?---/, '') # remove yaml front matter
  str.gsub!(/{%(.*)%}/, '') # remove functional liquid
  str.gsub!(/{{.*}}/, '') # remove referential liquid
  str.gsub!(%r{</?[^>]*>}, '') # remove html
  str.gsub!('\\n', '') # remove newlines
  str.gsub!(/\s+/, ' ') # remove extra space
  str.tr!('"', "'") # replace double quotes with single
  str
end
ingest(source) click to toggle source
# File lib/wax_tasks/utils.rb, line 8
def self.ingest(source)
  metadata =  case File.extname source
              when '.csv'
                WaxTasks::Utils.validate_csv source
              when '.json'
                WaxTasks::Utils.validate_json source
              when /\.ya?ml/
                WaxTasks::Utils.validate_yaml source
              else
                raise Error::InvalidSource, "Can't load #{File.extname source} files. Culprit: #{source}"
              end

  WaxTasks::Utils.assert_pids metadata
  WaxTasks::Utils.assert_unique metadata
end
lunr_normalize(val) click to toggle source
# File lib/wax_tasks/utils.rb, line 128
def self.lunr_normalize(val)
  case val
  when String || Integer
    WaxTasks::Utils.remove_diacritics val.to_s
  when Array
    return val if val.first.is_a? Hash
    WaxTasks::Utils.remove_diacritics val.join(', ')
  else
    val
  end
end
padded_int(idx, max_idx) click to toggle source

Constructs the order variable for each page (if the collection needs to preserve the order of items from the file)

@return [Integer] the order if the item padded with '0's for sorting

# File lib/wax_tasks/utils.rb, line 124
def self.padded_int(idx, max_idx)
  idx.to_s.rjust(Math.log10(max_idx).to_i + 1, '0')
end
remove_diacritics(str) click to toggle source

Normalizes accent marks/diacritics for Lunr indexing @return [String]

# File lib/wax_tasks/utils.rb, line 102
def self.remove_diacritics(str)
  to_replace  = 'ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž'
  replaced_by = 'AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz'
  str.to_s.tr to_replace, replaced_by
end
remove_yaml(str) click to toggle source

Removes YAML front matter from a string @return [String]

# File lib/wax_tasks/utils.rb, line 83
def self.remove_yaml(str)
  str.to_s.gsub!(/\A---(.|\n)*?---/, '')
end
safe_join(*args) click to toggle source
# File lib/wax_tasks/utils.rb, line 116
def self.safe_join(*args)
  File.join(args.compact).sub %r{^/}, ''
end
slug(str) click to toggle source

Converts string to snake case and swaps out special chars @return [String]

# File lib/wax_tasks/utils.rb, line 110
def self.slug(str)
  Utils.remove_diacritics(str).to_s.downcase.tr(' ', '_').gsub(/[^\w-]/, '')
end
validate_csv(source) click to toggle source

Checks that a CSV file is valid

@param source [String] path to CSV file @return [Array] validated CSV data as an Array of Hashes @raise WaxTasks::Error::InvalidCSV

# File lib/wax_tasks/utils.rb, line 52
def self.validate_csv(source)
  CSV.read(source, headers: true).map(&:to_hash)
rescue StandardError => e
  raise Error::InvalidCSV, " #{e}"
end
validate_json(source) click to toggle source

Checks that a JSON file is valid

@param source [String] path to JSON file @return [Array] validated JSON data as an Array of Hashes @raise WaxTasks::Error::InvalidJSON

# File lib/wax_tasks/utils.rb, line 63
def self.validate_json(source)
  file = File.read source
  JSON.parse file
rescue StandardError => e
  raise Error::InvalidJSON, " #{e}"
end
validate_yaml(source) click to toggle source

Checks that a YAML file is valid

@param source [String] path to YAML file @return [Array] validated YAML data as an Array of Hashes @raise WaxTasks::Error::InvalidYAML

# File lib/wax_tasks/utils.rb, line 75
def self.validate_yaml(source)
  SafeYAML.load_file source
rescue StandardError => e
  raise WaxTasks::Error::InvalidYAML, " #{e}"
end