Module: Metanorma::Utils

Defined in:
lib/utils/log.rb,
lib/utils/xml.rb,
lib/utils/main.rb,
lib/utils/image.rb,
lib/utils/version.rb,
lib/utils/hash_transform_keys.rb,
lib/utils/hash_transform_keys.rb

Defined Under Namespace

Modules: Array, Hash Classes: Log, Namespace

Constant Summary collapse

NAMECHAR =
"\u0000-\u002c\u002f\u003a-\u0040\\u005b-\u005e"\
"\u0060\u007b-\u00b6\u00b8-\u00bf\u00d7\u00f7\u037e"\
"\u2000-\u200b"\
"\u200e-\u203e\u2041-\u206f\u2190-\u2bff\u2ff0-\u3000".freeze
NAMESTARTCHAR =
"\\u002d\u002e\u0030-\u0039\u00b7\u0300-\u036f"\
"\u203f-\u2040".freeze
NOKOHEAD =
"<!DOCTYPE html SYSTEM\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head> <title></title> <meta charset=\"UTF-8\" /> </head>\n<body> </body> </html>\n".freeze
SVG_NS =
"http://www.w3.org/2000/svg".freeze
VERSION =
"1.4.3".freeze

Class Method Summary collapse

Class Method Details

.anchor_or_uuid(node = nil) ⇒ Object



29
30
31
32
# File 'lib/utils/xml.rb', line 29

def anchor_or_uuid(node = nil)
  uuid = UUIDTools::UUID.random_create
  node.nil? || node.id.nil? || node.id.empty? ? "_#{uuid}" : node.id
end

.asciidoc_sub(text, flavour = :standoc) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/utils/main.rb', line 35

def asciidoc_sub(text, flavour = :standoc)
  return nil if text.nil?
  return "" if text.empty?

  d = Asciidoctor::Document.new(
    text.lines.entries,
    { header_footer: false, backend: flavour },
  )
  b = d.parse.blocks.first
  b.apply_subs(b.source)
end

.attr_code(attributes) ⇒ Object



10
11
12
13
14
# File 'lib/utils/main.rb', line 10

def attr_code(attributes)
  attributes.compact.transform_values do |v|
    v.is_a?(String) ? HTMLEntities.new.decode(v) : v
  end
end

.csv_split(text, delim = ";") ⇒ Object

, “ => ,” : CSV definition does not deal with space followed by quote at start of field



18
19
20
21
22
23
24
# File 'lib/utils/main.rb', line 18

def csv_split(text, delim = ";")
  return if text.nil?

  CSV.parse_line(text&.gsub(/#{delim} "(?!")/, "#{delim}\""),
                 liberal_parsing: true,
                 col_sep: delim)&.compact&.map(&:strip)
end

.datauri(uri, local_dir = ".") ⇒ Object

sources/plantuml/plantuml20200524-90467-1iqek5i.png already includes localdir



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/utils/image.rb', line 127

def datauri(uri, local_dir = ".")
  # Return the data URI if it already is a data URI
  return uri if datauri?(uri)

  # Return the URL if it is a URL
  return uri if url?(uri)

  local_path = uri
  relative_path = File.join(local_dir, uri)

  # Check whether just the local path or the other specified relative path
  # works.
  path = [local_path, relative_path].detect do |p|
    File.exist?(p) ? p : nil
  end

  unless path && File.exist?(path)
    warn "Image specified at `#{uri}` does not exist."
    # Return original provided location
    return uri
  end

  encode_datauri(path)
end

.datauri2mime(uri) ⇒ Object

FIXME: This method should ONLY return 1 type, remove Array wrapper



187
188
189
190
191
192
# File 'lib/utils/image.rb', line 187

def datauri2mime(uri)
  output = decode_datauri(uri)
  return nil unless output && output[:type_detected]

  [output[:type_detected]]
end

.datauri?(uri) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/utils/image.rb', line 166

def datauri?(uri)
  /^data:/.match?(uri)
end

.decode_datauri(uri) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/utils/image.rb', line 174

def decode_datauri(uri)
  %r{^data:(?<mimetype>[^;]+);base64,(?<mimedata>.+)$} =~ uri
  return nil unless mimetype && mimedata

  data = Base64.strict_decode64(mimedata)
  {
    type_declared: mimetype,
    type_detected: Marcel::MimeType.for(data, declared_type: mimetype),
    data: data,
  }
end

.default_script(lang) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/utils/main.rb', line 124

def default_script(lang)
  case lang
  when "ar", "fa" then "Arab"
  when "ur" then "Aran"
  when "ru", "bg" then "Cyrl"
  when "hi" then "Deva"
  when "el" then "Grek"
  when "zh" then "Hans"
  when "ko" then "Kore"
  when "he" then "Hebr"
  when "ja" then "Jpan"
  else
    "Latn"
  end
end

.dl_to_attrs(elem, dlist, name) ⇒ Object

convert definition list term/value pair into Nokogiri XML attribute



145
146
147
148
149
# File 'lib/utils/main.rb', line 145

def dl_to_attrs(elem, dlist, name)
  e = dlist.at("./dt[text()='#{name}']") or return
  val = e.at("./following::dd/p") || e.at("./following::dd") or return
  elem[name] = val.text
end

.dl_to_elems(ins, elem, dlist, name) ⇒ Object

convert definition list term/value pairs into Nokogiri XML elements



152
153
154
155
156
157
158
159
# File 'lib/utils/main.rb', line 152

def dl_to_elems(ins, elem, dlist, name)
  a = elem.at("./#{name}[last()]")
  ins = a if a
  dlist.xpath("./dt[text()='#{name}']").each do |e|
    ins = dl_to_elems1(e, name, ins)
  end
  ins
end

.dl_to_elems1(term, name, ins) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/utils/main.rb', line 161

def dl_to_elems1(term, name, ins)
  v = term.at("./following::dd")
  e = v.elements and e.size == 1 && e.first.name == "p" and v = e.first
  v.name = name
  ins.next = v
  ins.next
end

.encode_datauri(path) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/utils/image.rb', line 152

def encode_datauri(path)
  return nil unless File.exist?(path)

  type = Marcel::MimeType.for(Pathname.new(path)) ||
    'text/plain; charset="utf-8"'

  bin = File.binread(path)
  data = Base64.strict_encode64(bin)
  "data:#{type};base64,#{data}"
rescue StandardError
  warn "Data-URI encoding of `#{path}` failed."
  nil
end

.endash_date(elem) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/utils/main.rb', line 63

def endash_date(elem)
  elem.traverse do |n|
    next unless n.text?

    n.replace(n.text.gsub(/\s+--?\s+/, "&#8211;").gsub(/--/, "&#8211;"))
  end
end

.external_path(path) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/utils/main.rb', line 113

def external_path(path)
  win = !!((RUBY_PLATFORM =~ /(win|w)(32|64)$/) ||
           (RUBY_PLATFORM =~ /mswin|mingw/))
  if win
    path.gsub!(%{/}, "\\")
    path[/\s/] ? "\"#{path}\"" : path
  else
    path
  end
end

.localdir(node) ⇒ Object



47
48
49
50
# File 'lib/utils/main.rb', line 47

def localdir(node)
  docfile = node.attr("docfile")
  docfile.nil? ? "./" : "#{Pathname.new(docfile).parent}/"
end

.noko(&block) ⇒ Object

block for processing XML document fragments as XHTML, to allow for HTMLentities Unescape special chars used in Asciidoctor substitution processing



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/utils/xml.rb', line 45

def noko(&block)
  doc = ::Nokogiri::XML.parse(NOKOHEAD)
  fragment = doc.fragment("")
  ::Nokogiri::XML::Builder.with fragment, &block
  fragment.to_xml(encoding: "US-ASCII", indent: 0,
                  save_with: Nokogiri::XML::Node::SaveOptions::AS_XML)
    .lines.map do |l|
    l.gsub(/>\n$/, ">").gsub(/\s*\n$/m, " ").gsub("&#150;", "\u0096")
      .gsub("&#151;", "\u0097").gsub("&#x96;", "\u0096")
      .gsub("&#x97;", "\u0097")
  end
end

.ns(xpath) ⇒ Object



58
59
60
61
62
63
# File 'lib/utils/xml.rb', line 58

def ns(xpath)
  xpath.gsub(%r{/([a-zA-z])}, "/xmlns:\\1")
    .gsub(%r{::([a-zA-z])}, "::xmlns:\\1")
    .gsub(%r{\[([a-zA-z][a-z0-9A-Z@/-]* ?=)}, "[xmlns:\\1")
    .gsub(%r{\[([a-zA-z][a-z0-9A-Z@/-]*[/\[\]])}, "[xmlns:\\1")
end

.rtl_script?(script) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/utils/main.rb', line 140

def rtl_script?(script)
  %w(Arab Aran Hebr).include? script
end

.save_dataimage(uri) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/utils/image.rb', line 23

def save_dataimage(uri)
  %r{^data:(image|application)/(?<imgtype>[^;]+);(charset=[^;]+;)?base64,(?<imgdata>.+)$} =~ uri
  imgtype.sub!(/\+[a-z0-9]+$/, "") # svg+xml
  imgtype = "png" unless /^[a-z0-9]+$/.match? imgtype
  Tempfile.open(["image", ".#{imgtype}"]) do |f|
    f.binmode
    f.write(Base64.strict_decode64(imgdata))
    f.path
  end
end

.set_nested_value(hash, keys, new_val) ⇒ Object

Set hash value using keys path mod from stackoverflow.com/a/42425884



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/utils/main.rb', line 73

def set_nested_value(hash, keys, new_val)
  key = keys[0]
  if keys.length == 1
    hash[key] = if hash[key].is_a?(::Array) then (hash[key] << new_val)
                else hash[key].nil? ? new_val : [hash[key], new_val]
                end
  elsif hash[key].is_a?(::Array)
    hash[key][-1] = {} if !hash[key].empty? && hash[key][-1].nil?
    hash[key] << {} if hash[key].empty? || !hash[key][-1].is_a?(::Hash)
    set_nested_value(hash[key][-1], keys[1..-1], new_val)
  elsif hash[key].nil? || hash[key].empty?
    hash[key] = {}
    set_nested_value(hash[key], keys[1..-1], new_val)
  elsif hash[key].is_a?(::Hash) && !hash[key][keys[1]]
    set_nested_value(hash[key], keys[1..-1], new_val)
  elsif !hash[key][keys[1]]
    hash[key] = [hash[key], {}]
    set_nested_value(hash[key][-1], keys[1..-1], new_val)
  else
    set_nested_value(hash[key], keys[1..-1], new_val)
  end
  hash
end

.smartformat(text) ⇒ Object

TODO needs internationalisation



53
54
55
56
57
58
59
60
61
# File 'lib/utils/main.rb', line 53

def smartformat(text)
  HTMLEntities.new.encode(
    HTMLEntities.new.decode(
      text.gsub(/ --? /, "&#8201;&#8212;&#8201;")
      .gsub(/--/, "&#8212;"),
    )
      .smart_format, :basic
  )
end

.strict_capitalize_first(str) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/utils/main.rb', line 105

def strict_capitalize_first(str)
  str.split(/ /).each_with_index.map do |w, i|
    letters = w.chars
    letters.first.upcase! if i.zero?
    letters.join
  end.join(" ")
end

.strict_capitalize_phrase(str) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/utils/main.rb', line 97

def strict_capitalize_phrase(str)
  str.split(/ /).map do |w|
    letters = w.chars
    letters.first.upcase!
    letters.join
  end.join(" ")
end

.svg_update_href(svgmap, svg, namespace) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/utils/image.rb', line 75

def svg_update_href(svgmap, svg, namespace)
  targ = svgmap_rewrite1_targets(svgmap, namespace)
  svg.xpath(".//m:a", "m" => SVG_NS).each do |a|
    ["xlink:href", "href"].each do |p|
      a[p] and x = targ[File.expand_path(a[p])] and a[p] = x
    end
  end
end

.svg_update_ids(svg, idx) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/utils/image.rb', line 95

def svg_update_ids(svg, idx)
  ids = svg.xpath("./@id | .//@id")
    .each_with_object([]) { |i, m| m << i.value }
  return if ids.empty?

  svg_update_ids_attrs(svg, ids, idx)
  svg_update_ids_css(svg, ids, idx)
end

.svg_update_ids_attrs(svg, ids, idx) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/utils/image.rb', line 104

def svg_update_ids_attrs(svg, ids, idx)
  svg.xpath(". | .//*[@*]").each do |a|
    a.attribute_nodes.each do |x|
      ids.include?(x.value) and x.value += sprintf("_%09d", idx)
    end
  end
end

.svg_update_ids_css(svg, ids, idx) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/utils/image.rb', line 112

def svg_update_ids_css(svg, ids, idx)
  svg.xpath("//m:style", "m" => SVG_NS).each do |s|
    c = s.children.to_xml
    ids.each do |i|
      c = c.gsub(%r[##{i}\b],
                 sprintf("#%<id>s_%<idx>09d", id: i, idx: idx))
        .gsub(%r(\[id\s*=\s*['"]?#{i}['"]?\]),
              sprintf("[id='%<id>s_%<idx>09d']", id: i, idx: idx))
    end
    s.children = c
  end
end

.svgmap_rewrite(xmldoc, localdirectory = "") ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/utils/image.rb', line 36

def svgmap_rewrite(xmldoc, localdirectory = "")
  n = Namespace.new(xmldoc)
  xmldoc.xpath(n.ns("//svgmap")).each_with_index do |s, i|
    next unless svgmap_rewrite0(s, n, localdirectory, i)
    next if s.at(n.ns("./target/eref"))

    s.replace(s.at(n.ns("./figure")))
  end
end

.svgmap_rewrite0(svgmap, namespace, localdirectory, idx) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/utils/image.rb', line 46

def svgmap_rewrite0(svgmap, namespace, localdirectory, idx)
  if (i = svgmap.at(namespace.ns(".//image"))) && (src = i["src"])
    path = svgmap_rewrite0_path(src, localdirectory)
    File.file?(path) or return false
    svg = Nokogiri::XML(File.read(path, encoding: "utf-8"))
    i.replace(svgmap_rewrite1(svgmap, svg.root, namespace, idx))
    /^data:/.match(src) and i["src"] = datauri(path)
  elsif i = svgmap.at(".//m:svg", "m" => SVG_NS)
    i.replace(svgmap_rewrite1(svgmap, i, namespace, idx))
  else return false
  end
  true
end

.svgmap_rewrite0_path(src, localdirectory) ⇒ Object



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

def svgmap_rewrite0_path(src, localdirectory)
  if /^data:/.match?(src)
    save_dataimage(src)
  else
    File.file?(src) ? src : localdirectory + src
  end
end

.svgmap_rewrite1(svgmap, svg, namespace, idx) ⇒ Object



68
69
70
71
72
73
# File 'lib/utils/image.rb', line 68

def svgmap_rewrite1(svgmap, svg, namespace, idx)
  svg_update_href(svgmap, svg, namespace)
  svg_update_ids(svg, idx)
  svg.xpath("processing-instruction()|.//processing-instruction()").remove
  svg.to_xml
end

.svgmap_rewrite1_targets(svgmap, namespace) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/utils/image.rb', line 84

def svgmap_rewrite1_targets(svgmap, namespace)
  svgmap.xpath(namespace.ns("./target"))
    .each_with_object({}) do |t, m|
    x = t.at(namespace.ns("./xref")) and
      m[File.expand_path(t["href"])] = "##{x['target']}"
    x = t.at(namespace.ns("./link")) and
      m[File.expand_path(t["href"])] = x["target"]
    t.remove if t.at(namespace.ns("./xref | ./link"))
  end
end

.to_ncname(tag, asciionly: true) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/utils/xml.rb', line 17

def to_ncname(tag, asciionly: true)
  asciionly and tag = HTMLEntities.new.encode(tag, :basic, :hexadecimal)
  start = tag[0]
  ret1 = if %r([#{NAMECHAR}#])o.match?(start)
           "_"
         else
           (%r([#{NAMESTARTCHAR}#])o.match?(start) ? "_#{start}" : start)
         end
  ret2 = tag[1..-1] || ""
  (ret1 || "") + ret2.gsub(%r([#{NAMECHAR}#])o, "_")
end

.url?(url) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/utils/image.rb', line 170

def url?(url)
  %r{^[A-Z]{2,}://}i.match?(url)
end

.wrap_in_para(node, out) ⇒ Object

if the contents of node are blocks, output them to out; else, wrap them in <p>



28
29
30
31
32
33
# File 'lib/utils/main.rb', line 28

def wrap_in_para(node, out)
  if node.blocks? then out << node.content
  else
    out.p { |p| p << node.content }
  end
end