module WaxTasks::Utils
Utility helper methods
Public Class Methods
# 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
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
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
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
# 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
# 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
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
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
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
# File lib/wax_tasks/utils.rb, line 116 def self.safe_join(*args) File.join(args.compact).sub %r{^/}, '' end
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
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
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
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