Module: WaxTasks::Utils

Defined in:
lib/wax_tasks/utils.rb

Overview

Utility helper methods

Class Method Summary collapse

Class Method Details

.add_yaml_front_matter_to_file(file) ⇒ Object


140
141
142
143
144
145
146
147
148
149
# 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) ⇒ 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


29
30
31
32
# 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) ⇒ 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


39
40
41
42
43
44
45
# 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) ⇒ String

Scrubs yaml, liquid, html, and etc from content strings

Returns:

  • (String)

89
90
91
92
93
94
95
96
97
98
# 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) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/wax_tasks/utils.rb', line 8

def self.ingest(source)
   =  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 
  WaxTasks::Utils.assert_unique 
end

.lunr_normalize(val) ⇒ Object


128
129
130
131
132
133
134
135
136
137
138
# 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) ⇒ Integer

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

Returns:

  • (Integer)

    the order if the item padded with '0's for sorting


124
125
126
# 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) ⇒ String

Normalizes accent marks/diacritics for Lunr indexing

Returns:

  • (String)

102
103
104
105
106
# 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) ⇒ String

Removes YAML front matter from a string

Returns:

  • (String)

83
84
85
# File 'lib/wax_tasks/utils.rb', line 83

def self.remove_yaml(str)
  str.to_s.gsub!(/\A---(.|\n)*?---/, '')
end

.safe_join(*args) ⇒ Object


116
117
118
# File 'lib/wax_tasks/utils.rb', line 116

def self.safe_join(*args)
  File.join(args.compact).sub %r{^/}, ''
end

.slug(str) ⇒ String

Converts string to snake case and swaps out special chars

Returns:

  • (String)

110
111
112
# 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) ⇒ 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


52
53
54
55
56
# 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) ⇒ 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


63
64
65
66
67
68
# 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) ⇒ 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


75
76
77
78
79
# 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