Module: Metanorma::Util
- Defined in:
- lib/metanorma/util/util.rb
Overview
Cross-cutting helpers used by the metanorma stack: logging gated by Configuration#logs, ordering of output-format extensions for execution, and recursive hash key normalisation.
Class Method Summary collapse
-
.log(message, type = :info) ⇒ void
Print
messageto stdout iftypeis in Configuration#logs; abort the process iftypeis:fatalregardless of whether it was printed. -
.recursive_string_keys(hash) ⇒ Hash, ...
Recursively normalise the keys of a Hash (and any nested Hashes / Enumerables) to Strings.
-
.sort_extensions_execution(ext) ⇒ Array<Symbol>
Stable-sort a list of output-format extensions by execution priority (see Util.sort_extensions_execution_ord).
-
.sort_extensions_execution_ord(ext) ⇒ Integer
Sort key used to put output-format extensions in execution order.
Class Method Details
.log(message, type = :info) ⇒ void
This method returns an undefined value.
Print message to stdout if type is in Configuration#logs; abort the process if type is :fatal regardless of whether it was printed.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/metanorma/util/util.rb', line 18 def log(, type = :info) log_types = Metanorma.configuration.logs.map(&:to_s) || [] if log_types.include?(type.to_s) puts() end if type == :fatal abort end end |
.recursive_string_keys(hash) ⇒ Hash, ...
Recursively normalise the keys of a Hash (and any nested Hashes / Enumerables) to Strings. Used to canonicalise YAML/JSON-derived configuration that may have been parsed with mixed symbol / string keys.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/metanorma/util/util.rb', line 67 def recursive_string_keys(hash) case hash when Hash then hash.map do |k, v| [k.to_s, recursive_string_keys(v)] end.to_h when Enumerable then hash.map { |v| recursive_string_keys(v) } else hash end end |
.sort_extensions_execution(ext) ⇒ Array<Symbol>
Stable-sort a list of output-format extensions by execution priority (see sort_extensions_execution_ord).
53 54 55 56 57 |
# File 'lib/metanorma/util/util.rb', line 53 def sort_extensions_execution(ext) ext.sort do |a, b| sort_extensions_execution_ord(a) <=> sort_extensions_execution_ord(b) end end |
.sort_extensions_execution_ord(ext) ⇒ Integer
Sort key used to put output-format extensions in execution order. xml runs first because the semantic XML feeds everything else; rxl second; presentation third (consumed by HTML/Word/PDF); everything else last (alphabetical via the caller’s sort stability).
38 39 40 41 42 43 44 45 46 |
# File 'lib/metanorma/util/util.rb', line 38 def sort_extensions_execution_ord(ext) case ext when :xml then 0 when :rxl then 1 when :presentation then 2 else 99 end end |