Module: WaxTasks::Utils
- Defined in:
- lib/wax_tasks/utils.rb
Overview
Utility helper methods
Class Method Summary collapse
-
.assert_pids(data) ⇒ Array
Checks and asserts presence of ‘pid` value for each item.
-
.assert_unique(data) ⇒ Array
Checks and asserts uniqueness of ‘pid` value for each item.
-
.construct_permalink(site) ⇒ String
Contructs permalink extension from site ‘permalink` variable.
-
.get_lunr_collections(site) ⇒ Array
Finds collections in site config where ‘lunr_index` is enabled.
-
.html_strip(str) ⇒ String
Cleans YAML front matter + markdown pages for lunr indexing.
-
.make_path(*args) ⇒ String
Creates a file path valid file path with empty strings and null values dropped.
-
.remove_diacritics(str) ⇒ String
Normalizes accent marks/diacritics for Lunr indexing.
-
.remove_yaml(str) ⇒ String
Removes YAML front matter from a string.
-
.slug(str) ⇒ String
Converts string to snake case and swaps out special chars.
-
.validate_csv(source) ⇒ Array
Checks that a CSV file is valid.
-
.validate_json(source) ⇒ Array
Checks that a JSON file is valid.
-
.validate_yaml(source) ⇒ Array
Checks that a YAML file is valid.
Class Method Details
.assert_pids(data) ⇒ Array
Checks and asserts presence of ‘pid` value for each item
22 23 24 25 |
# File 'lib/wax_tasks/utils.rb', line 22 def self.assert_pids(data) data.each_with_index { |d, i| raise Error::MissingPid, "Collection #{@name} is missing pid for item #{i}." unless d.key? 'pid' } data end |
.assert_unique(data) ⇒ Array
Checks and asserts uniqueness of ‘pid` value for each item
32 33 34 35 36 37 |
# File 'lib/wax_tasks/utils.rb', line 32 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 |
.construct_permalink(site) ⇒ String
Contructs permalink extension from site ‘permalink` variable
8 9 10 11 12 13 14 15 |
# File 'lib/wax_tasks/utils.rb', line 8 def self.construct_permalink(site) case site.fetch(:permalink, false) when 'pretty' || '/' '/' else '.html' end end |
.get_lunr_collections(site) ⇒ Array
Finds collections in site config where ‘lunr_index` is enabled
87 88 89 90 91 |
# File 'lib/wax_tasks/utils.rb', line 87 def self.get_lunr_collections(site) to_index = site[:collections].find_all { |c| c[1].key?('lunr_index') } raise Error::NoLunrCollections, 'There are no lunr collections to index.' if to_index.nil? to_index.map { |c| c[0] } end |
.html_strip(str) ⇒ String
Cleans YAML front matter + markdown pages for lunr indexing
101 102 103 104 105 106 107 108 109 |
# File 'lib/wax_tasks/utils.rb', line 101 def self.html_strip(str) str.gsub!(/\A---(.|\n)*?---/, '') # remove yaml front matter str.gsub!(/{%(.*)%}/, '') # remove functional 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 |
.make_path(*args) ⇒ String
Creates a file path valid file path with empty strings and null values dropped
78 79 80 |
# File 'lib/wax_tasks/utils.rb', line 78 def self.make_path(*args) args.compact.reject(&:empty?).join('/') end |
.remove_diacritics(str) ⇒ String
Normalizes accent marks/diacritics for Lunr indexing
113 114 115 116 117 |
# File 'lib/wax_tasks/utils.rb', line 113 def self.remove_diacritics(str) to_replace = 'ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž' replaced_by = 'AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz' str.to_s.tr(to_replace, replaced_by) end |
.remove_yaml(str) ⇒ String
Removes YAML front matter from a string
95 96 97 |
# File 'lib/wax_tasks/utils.rb', line 95 def self.remove_yaml(str) str.to_s.gsub!(/\A---(.|\n)*?---/, '') end |
.slug(str) ⇒ String
Converts string to snake case and swaps out special chars
121 122 123 |
# File 'lib/wax_tasks/utils.rb', line 121 def self.slug(str) str.to_s.downcase.tr(' ', '_').gsub(/[^\w-]/, '') end |
.validate_csv(source) ⇒ Array
Checks that a CSV file is valid
44 45 46 47 48 |
# File 'lib/wax_tasks/utils.rb', line 44 def self.validate_csv(source) CSV.read(source, headers: true).map(&:to_hash) rescue StandardError => e raise Error::InvalidCSV, " #{e}" end |
.validate_json(source) ⇒ Array
Checks that a JSON file is valid
55 56 57 58 59 60 |
# File 'lib/wax_tasks/utils.rb', line 55 def self.validate_json(source) file = File.read(source) JSON.parse(file) rescue StandardError => e raise Error::InvalidJSON, " #{e}" end |
.validate_yaml(source) ⇒ Array
Checks that a YAML file is valid
67 68 69 70 71 |
# File 'lib/wax_tasks/utils.rb', line 67 def self.validate_yaml(source) YAML.load_file(source) rescue StandardError => e raise WaxTasks::Error::InvalidYAML, " #{e}" end |