Module: Metanorma::Collection::Util

Defined in:
lib/metanorma/collection/util/util.rb,
lib/metanorma/collection/util/disambig_files.rb

Defined Under Namespace

Classes: DisambigFiles, Dummy

Constant Summary collapse

SVG_NS =
"http://www.w3.org/2000/svg".freeze

Class Method Summary collapse

Class Method Details

.add_suffix_to_attrs(doc, suffix, tag, attr, isodoc) ⇒ Object

Skip SVGs, they are processed by Vectory::SvgDocument#suffix_ids



48
49
50
51
52
53
54
55
56
# File 'lib/metanorma/collection/util/util.rb', line 48

def add_suffix_to_attrs(doc, suffix, tag, attr, isodoc)
  (suffix.nil? || suffix.empty?) and return
  doc.xpath(isodoc.ns("//#{tag}[@#{attr}][not(ancestor-or-self::*[local-name()='svg'])]")).each do |elem|
    # doc.xpath(isodoc.ns("//#{tag}[@#{attr}]")).each do |elem|
    a = elem.attributes[attr].value
    /_#{suffix}$/.match?(a) or
      elem.attributes[attr].value = "#{a}_#{suffix}"
  end
end

.anchor_id_attributesObject



12
13
14
15
# File 'lib/metanorma/collection/util/util.rb', line 12

def anchor_id_attributes
  Metanorma::Utils::anchor_attributes(presxml: true) +
    [%w(* id), %w(* anchor), %w(link bibitemid), %w(fmt-link bibitemid)]
end

.asciidoc_dummy_headerObject



183
184
185
186
187
188
189
# File 'lib/metanorma/collection/util/util.rb', line 183

def asciidoc_dummy_header
  <<~DUMMY
    = X
    A

  DUMMY
end

.gather_bibitemids(xml, presxml) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/metanorma/collection/util/util.rb', line 30

def gather_bibitemids(xml, presxml)
  xml.xpath("//*[@bibitemid]").each_with_object({}) do |e, m|
    presxml && %w(xref eref link).include?(e.name) and next
    m[e["bibitemid"]] ||= []
    m[e["bibitemid"]] << e
  end
end

.gather_bibitems(xml) ⇒ Object



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

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

.gather_citeases(xml, presxml) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/metanorma/collection/util/util.rb', line 38

def gather_citeases(xml, presxml)
  xml.xpath("//*[@citeas]").each_with_object({}) do |e, m|
    presxml && %w(xref eref link).include?(e.name) and next
    k = key(e["citeas"])
    m[k] ||= []
    m[k] << e
  end
end

.hash_key_detect(directives, key, variable) ⇒ Object



58
59
60
61
62
# File 'lib/metanorma/collection/util/util.rb', line 58

def hash_key_detect(directives, key, variable)
  c = directives.detect { |x| x.key == key } or
    return variable
  c.value
end

.hide_refs(docxml) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/metanorma/collection/util/util.rb', line 71

def hide_refs(docxml)
  p = "//xmlns:references[xmlns:bibitem]"\
    "[not(./xmlns:bibitem[not(@hidden) or @hidden = 'false'])]"
  docxml.xpath(p).each do |f|
    f["hidden"] = "true"
  end
end

.isodoc_create(flavor, lang, script, xml, presxml: false) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/metanorma/collection/util/util.rb', line 172

def isodoc_create(flavor, lang, script, xml, presxml: false)
  isodoc = Util::load_isodoc(flavor, presxml: presxml)
  isodoc.i18n_init(lang, script, nil) # read in internationalisation
  # TODO locale?
  isodoc.(lang, script, nil, isodoc.i18n)
  isodoc.xref_init(lang, script, nil, isodoc.i18n, {})
  isodoc.xrefs.klass.meta = isodoc.meta
  isodoc.info(xml, nil)
  isodoc
end

.key(ident) ⇒ Object



79
80
81
82
# File 'lib/metanorma/collection/util/util.rb', line 79

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

.load_isodoc(flavor, presxml: false) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/metanorma/collection/util/util.rb', line 163

def load_isodoc(flavor, presxml: false)
  x = Asciidoctor.load nil, backend: flavor.to_sym
  if presxml
    x.converter.presentation_xml_converter(Dummy.new)
  else
    x.converter.html_converter(Dummy.new) # to obtain Isodoc class
  end
end

.mime_file_recognised?(filename) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
# File 'lib/metanorma/collection/util/util.rb', line 7

def mime_file_recognised?(filename)
  mime_type = Marcel::MimeType.for name: filename
  mime_type != "application/octet-stream"
end

.nokogiri_to_temp(xml, filename, suffix) ⇒ Object



191
192
193
194
195
196
# File 'lib/metanorma/collection/util/util.rb', line 191

def nokogiri_to_temp(xml, filename, suffix)
  file = Tempfile.new([filename, suffix])
  file.write(xml.to_xml(indent: 0))
  file.close
  [file, file.path]
end

.rel_path_resolve(dir, path) ⇒ Object



64
65
66
67
68
69
# File 'lib/metanorma/collection/util/util.rb', line 64

def 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

.taste2coverpage_pdf_portfolio(taste) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/metanorma/collection/util/util.rb', line 96

def taste2coverpage_pdf_portfolio(taste)
  tastes = Metanorma::TasteRegister.instance.aliases
  tastes[taste.to_sym] or return nil
  taste = Metanorma::TasteRegister.instance.get(taste.to_sym)
  ret = taste.config.base_override&.filename_attributes
    &.coverpage_pdf_portfolio or return
  File.join(taste.directory, ret)
end

.taste2flavor(taste) ⇒ Object



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

def taste2flavor(taste)
  tastes = Metanorma::TasteRegister.instance.aliases
  tastes[taste.to_sym] and taste = tastes[taste.to_sym]
  taste
end

.taste2isodoc_attrs(taste, format) ⇒ Object



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

def taste2isodoc_attrs(taste, format)
  tastes = Metanorma::TasteRegister.instance.aliases
  tastes[taste.to_sym] or return {}
  Metanorma::TasteRegister.isodoc_attrs(taste.to_sym, format)
end

.update_ids_css(document, ids, suffix) ⇒ Object

replicates Vectory update_ids_css, but skips over svgs, which are processed separately by Vectory::SvgDocument#suffix_ids



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/metanorma/collection/util/util.rb', line 120

def update_ids_css(document, ids, suffix)
  suffix = suffix.is_a?(Integer) ? sprintf("%09d", suffix) : suffix
  document.xpath(".//m:style[not(ancestor::i:svg)]",
                 "m" => SVG_NS, "i" => SVG_NS).each do |s|
    s.children = update_ids_css_string(s.children.to_xml, ids, suffix)
  end
  document.xpath(".//*[@style][not(ancestor::i:svg)]",
                 "i" => SVG_NS).each do |s|
    s["style"] = update_ids_css_string(s["style"], ids, suffix)
  end
end

.update_ids_css_string(style, ids, suffix) ⇒ Object

Updates ID references in a CSS style string. Replicates Vectory update_ids_css_string



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/metanorma/collection/util/util.rb', line 134

def update_ids_css_string(style, ids, suffix)
  ids.each do |i|
    style = style.gsub(%r[##{i}\b],
                       sprintf("#%<id>s_%<suffix>s", id: i,
                                                     suffix: suffix))
      .gsub(%r(\[id\s*=\s*['"]?#{i}['"]?\]),
            sprintf("[id='%<id>s_%<suffix>s']", id: i,
                                                suffix: suffix))
  end
  style
end

.url_in_css_styles(doc, ids, document_suffix) ⇒ Object

update relative URLs, url(#…), in CSS in @style attrs (incl. SVG), and in include SVG url(#..) attrs, not processed already by add_suffix_to_attrs



110
111
112
113
114
115
116
# File 'lib/metanorma/collection/util/util.rb', line 110

def url_in_css_styles(doc, ids, document_suffix)
  update_ids_css(doc.root, ids, document_suffix)
  doc.xpath("//i:svg", "i" => SVG_NS).each do |s|
    svg = Vectory::SvgDocument.new(s.to_xml)
    svg.suffix_ids(document_suffix)
  end
end