Module: Metanorma::Util

Defined in:
lib/metanorma/util.rb

Defined Under Namespace

Classes: DisambigFiles

Class Method Summary collapse

Class Method Details

.add_suffix_to_attributes(doc, suffix, tag_name, attr_name, isodoc) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/metanorma/util.rb', line 68

def self.add_suffix_to_attributes(doc, suffix, tag_name, attr_name, isodoc)
  (suffix.nil? || suffix.empty?) and return
  doc.xpath(isodoc.ns("//#{tag_name}[@#{attr_name}]")).each do |elem|
    a = elem.attributes[attr_name].value
    /_#{suffix}$/.match?(a) or
      elem.attributes[attr_name].value = "#{a}_#{suffix}"
  end
end

.gather_bibitemids(xml) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/metanorma/util.rb', line 52

def self.gather_bibitemids(xml)
  xml.xpath("//*[@bibitemid]").each_with_object({}) do |e, m|
    /^semantic__/.match?(e.name) and next
    m[e["bibitemid"]] ||= []
    m[e["bibitemid"]] << e
  end
end

.gather_bibitems(xml) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/metanorma/util.rb', line 41

def self.gather_bibitems(xml)
  xml.xpath("//xmlns:bibitem[@id]").each_with_object({}) do |b, m|
    if m[b["id"]]
      b.remove
      next # we can't update duplicate bibitem, processing updates wrong one
    else
      m[b["id"]] = b
    end
  end
end

.gather_citeases(xml) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/metanorma/util.rb', line 60

def self.gather_citeases(xml)
  xml.xpath("//*[@citeas]").each_with_object({}) do |e, m|
    /^semantic__/.match?(e.name) and next
    m[e["citeas"]] ||= []
    m[e["citeas"]] << e
  end
end

.hash_key_detect(directives, key, variable) ⇒ Object



77
78
79
80
81
# File 'lib/metanorma/util.rb', line 77

def self.hash_key_detect(directives, key, variable)
  c = directives.detect { |x| x.is_a?(Hash) && x.has_key?(key) } or
    return variable
  c[key]
end

.key(ident) ⇒ Object



90
91
92
93
# File 'lib/metanorma/util.rb', line 90

def self.key(ident)
  @c ||= HTMLEntities.new
  @c.decode(ident).gsub(/(\p{Zs})+/, " ")
end

.log(message, type = :info) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/metanorma/util.rb', line 3

def self.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
    exit(1)
  end
end

.recursive_string_keys(hash) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/metanorma/util.rb', line 32

def self.recursive_string_keys(hash)
  case hash
  when Hash then hash.map { |k, v| [k.to_s, recursive_string_keys(v)] }.to_h
  when Enumerable then hash.map { |v| recursive_string_keys(v) }
  else
    hash
  end
end

.rel_path_resolve(dir, path) ⇒ Object



83
84
85
86
87
88
# File 'lib/metanorma/util.rb', line 83

def self.rel_path_resolve(dir, path)
  path.nil? and return path
  path.empty? and return path
  p = Pathname.new(path)
  p.absolute? ? path : File.join(dir, path)
end

.sort_extensions_execution(ext) ⇒ Object



26
27
28
29
30
# File 'lib/metanorma/util.rb', line 26

def self.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) ⇒ Object

dependency ordering



16
17
18
19
20
21
22
23
24
# File 'lib/metanorma/util.rb', line 16

def self.sort_extensions_execution_ord(ext)
  case ext
  when :xml then 0
  when :rxl then 1
  when :presentation then 2
  else
    99
  end
end