Module: WaxTasks::Utils

Defined in:
lib/wax_tasks/utils.rb

Overview

Utility helper methods

Class Method Summary collapse

Class Method Details

.assert_pids(data) ⇒ Array

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

Parameters:

  • data (Array)

    array of hashes each representing a collection item

Returns:

  • (Array)

    same data unless a an item is missing the key ‘pid`

Raises:

  • WaxTasks::Error::MissingPid



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

Parameters:

  • data (Array)

    array of hashes each representing a collection item

Returns:

  • (Array)

    same data unless an item has non-unique value for ‘pid`

Raises:

  • WaxTasks::Error::NonUniquePid



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

Contructs permalink extension from site ‘permalink` variable

Parameters:

  • site (Hash)

    the site config

Returns:

  • (String)

    the end of the permalink, either ‘/’ or ‘.html’



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

Parameters:

  • site (Hash)

    the site config

Returns:

  • (Array)

    a list of collection names

Raises:

  • WaxTasks::Error::NoLunrCollections



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

Returns:



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

Parameters:

  • args (Array)

    items to concatenate in path

Returns:



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

Returns:



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

Returns:



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

Returns:



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

Parameters:

  • source (String)

    path to CSV file

Returns:

  • (Array)

    validated CSV data as an Array of Hashes

Raises:

  • WaxTasks::Error::InvalidCSV



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

Parameters:

  • source (String)

    path to JSON file

Returns:

  • (Array)

    validated JSON data as an Array of Hashes

Raises:

  • WaxTasks::Error::InvalidJSON



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

Parameters:

  • source (String)

    path to YAML file

Returns:

  • (Array)

    validated YAML data as an Array of Hashes

Raises:

  • WaxTasks::Error::InvalidYAML



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