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

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.

Parameters:

  • message (String)

    the message to print.

  • type (Symbol) (defaults to: :info)

    severity, defaults to :info. Common values: :info, :warning, :error, :fatal. The configured logs list determines which severities are printed; :fatal always aborts on top of (or instead of) printing.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/metanorma/util/util.rb', line 18

def log(message, type = :info)
  log_types = Metanorma.configuration.logs.map(&:to_s) || []

  if log_types.include?(type.to_s)
    puts(message)
  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.

Parameters:

  • hash (Hash, Enumerable, Object)

    the value to normalise.

Returns:

  • (Hash, Array, Object)

    the normalised value; non-collections are returned unchanged.



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).

Parameters:

  • ext (Array<Symbol>)

    extensions to sort.

Returns:

  • (Array<Symbol>)

    sorted in execution order.



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).

Parameters:

  • ext (Symbol)

    output-format extension symbol.

Returns:

  • (Integer)

    ordinal for sorting.



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