Module: Metanorma::Collection::XrefProcess

Defined in:
lib/metanorma/collection/xrefprocess/xrefprocess.rb

Class Method Summary collapse

Class Method Details

.copy_repo_items_biblio(ins, section, xml) ⇒ Object



195
196
197
198
199
200
201
202
203
204
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 195

def copy_repo_items_biblio(ins, section, xml)
  bibitems = ::Metanorma::Collection::Util::gather_bibitems(section)
  xml.xpath(ns("//references/bibitem[docidentifier/@type = 'repository']"))
    .each_with_object([]) do |b, m|
      bibitems[b["id"]] or next
      # section.at("//*[@bibitemid = '#{b['id']}']") or next
      ins << b.dup
      m << b["id"]
    end
end

.eref_to_internal_eref(section, xml, key, presxml) ⇒ Object



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

def eref_to_internal_eref(section, xml, key, presxml)
  bibitems, indirect, bibids, lang =
    eref_to_internal_eref_prep(section, xml, presxml)
  eref_to_internal_eref_select(section, xml, bibitems, presxml)
    .each_with_object([]) do |x, m|
      url = select_citation_uri(bibitems[x], lang)
      bibids[x]&.each do |e|
        e.at(ns("./localityStack | ./locality")) and next
        id = eref_to_internal_eref1(e, key, url, indirect, presxml) and m << id
      end
    end
end

.eref_to_internal_eref1(elem, key, url, indirect, presxml) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 144

def eref_to_internal_eref1(elem, key, url, indirect, presxml)
  if url
    elem.name = presxml ? "fmt-link" : "link"
    elem["target"] = url
    nil
  elsif !indirect[elem["bibitemid"]]
    nil
  else
    eref_to_internal_eref1_internal(elem, key, indirect)
  end
end

.eref_to_internal_eref1_internal(elem, key, indirect) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 156

def eref_to_internal_eref1_internal(elem, key, indirect)
  t = elem["bibitemid"]
  if key
    t = "#{key}_#{t}"
    elem["type"] = key
  elsif d = indirect[t]&.at(ns("./docidentifier[@type = 'repository']"))
    m = %r{^([^/]+)}.match(d.text) and
      t.sub!(%r(#{m[0]}_), "")
  end
  make_anchor(elem, t)
  elem["bibitemid"]
end

.eref_to_internal_eref_prep(section, xml, presxml) ⇒ Object



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

def eref_to_internal_eref_prep(section, xml, presxml)
  bibitems = ::Metanorma::Collection::Util::gather_bibitems(xml)
    .delete_if { |_, v| internal_bib?(v) }
  indirect = ::Metanorma::Collection::Util::gather_bibitems(xml)
    .select { |_, v| indirect_bib?(v) }
  bibitemids = ::Metanorma::Collection::Util::gather_bibitemids(section, presxml)
  lang = xml.at(ns("//bibdata/language"))&.text || "en"
  [bibitems, indirect, bibitemids, lang]
end

.eref_to_internal_eref_select(section, _xml, bibitems, presxml) ⇒ Object



169
170
171
172
173
174
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 169

def eref_to_internal_eref_select(section, _xml, bibitems, presxml)
  refs = ::Metanorma::Collection::Util::gather_bibitemids(section, presxml).keys
  refs.uniq.reject do |x|
    b = bibitems[x] and (indirect_bib?(b) || internal_bib?(b))
  end
end

.indirect_bib?(bibitem) ⇒ Boolean

Returns:

  • (Boolean)


181
182
183
184
185
186
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 181

def indirect_bib?(bibitem)
  a = bibitem.at(ns("./docidentifier[@type = 'repository']")) or
    return false
  %r{^current-metanorma-collection/}.match?(a.text) and return false
  a.text.include?("/")
end

.insert_indirect_biblio(ins, refs, key, xml) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 206

def insert_indirect_biblio(ins, refs, key, xml)
  refs.empty? and return
  internal_bibitems, = insert_indirect_biblio_prep(xml)
  refs.compact.reject do |x|
    # external_bibitems[x.sub(/^#{key}_/, "")]
  end.each do |x|
    ins << if b = internal_bibitems[x.sub(/^#{key}_/, "")]
             b.dup.tap { |m| m["id"] = x }
           else new_indirect_bibitem(x, key)
           end
  end
end

.insert_indirect_biblio_prep(xml) ⇒ Object



219
220
221
222
223
224
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 219

def insert_indirect_biblio_prep(xml)
  bibitems = ::Metanorma::Collection::Util::gather_bibitems(xml)
  internal_bibitems = bibitems.select { |_, v| internal_bib?(v) }
  external_bibitems = bibitems.reject { |_, v| internal_bib?(v) }
  [internal_bibitems, external_bibitems]
end

.internal_bib?(bibitem) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
179
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 176

def internal_bib?(bibitem)
  bibitem["type"] == "internal" ||
    bibitem.at(ns("./docidentifier[@type = 'repository']"))
end

.make_anchor(elem, anchor) ⇒ Object



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

def make_anchor(elem, anchor)
  elem.at(ns("./localityStack | ./locality")) and return
  elem.text.strip.empty? && elem.elements.empty? and elem << anchor
  elem <<
    "<localityStack><locality type='anchor'><referenceFrom>" \
      "#{anchor}</referenceFrom></locality></localityStack>"
end

.new_hidden_ref(xmldoc) ⇒ Object

from standoc



189
190
191
192
193
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 189

def new_hidden_ref(xmldoc)
  ins = xmldoc.at("bibliography") or
    xmldoc.root << "<bibliography/>" and ins = xmldoc.at("bibliography")
  ins.add_child("<references hidden='true' normative='false'/>").first
end

.new_indirect_bibitem(ident, prefix) ⇒ Object



226
227
228
229
230
231
232
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 226

def new_indirect_bibitem(ident, prefix)
  <<~BIBENTRY
    <bibitem id="#{ident}" type="internal">
    <docidentifier type="repository">#{ident.sub(/^#{prefix}_/, "#{prefix}/")}</docidentifier>
    </bibitem>
  BIBENTRY
end

.ns(xpath) ⇒ Object



23
24
25
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 23

def ns(xpath)
  @isodoc.ns(xpath)
end

.select_citation_uri(bib, lang) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 113

def select_citation_uri(bib, lang)
  bib or return
  url = bib.at(ns("./uri[@type = 'citation']" \
                  "[@language = '#{lang}']")) ||
    bib.at(ns("./uri[@type = 'citation']"))
  url&.text
end

.svg_preprocess(xml, doc_suffix, presxml) ⇒ Object



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

def svg_preprocess(xml, doc_suffix, presxml)
  suffix = doc_suffix.nil? || doc_suffix.blank? ? "" : "_#{doc_suffix}"
  xml.xpath("//m:svg", "m" => "http://www.w3.org/2000/svg").each do |s|
    m = svgmap_wrap(s)
    svg_xrefs(s, m, suffix, presxml)
  end
  xml
end

.svg_xrefs(svg, svgmap, suffix, presxml) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 53

def svg_xrefs(svg, svgmap, suffix, presxml)
  tag = presxml ? "fmt-xref" : "xref"
  svg.xpath(".//m:a", "m" => "http://www.w3.org/2000/svg").each do |a|
    /^#/.match? a["href"] or next
    a["href"] = a["href"].sub(/^#/, "")
    svgmap << "<target href='#{a['href']}'>" \
      "<#{tag} target='#{a['href']}#{suffix}'/></target>"
  end
end

.svgmap_wrap(svg) ⇒ Object



46
47
48
49
50
51
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 46

def svgmap_wrap(svg)
  ret = svg.at("./ancestor::xmlns:svgmap") and return ret
  ret = svg.at("./ancestor::xmlns:figure")
  ret.wrap("<svgmap/>")
  svg.at("./ancestor::xmlns:svgmap")
end

.xref_prefix_key(xref, key, indirect) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 103

def xref_prefix_key(xref, key, indirect)
  if b = indirect[xref["target"]]
    t = b.at(ns("./docidentifier[@type = 'repository']"))
    xref["type"] = t.text.sub(%r{/.*$}, "")
  elsif key
    xref["target"] = "#{key}_#{xref['target']}"
    xref["type"] = key
  end
end

.xref_preprocess(xml, isodoc) ⇒ Object

xml.root = key # to force recognition of internal refs bookmarks etc as new id elements introduced in Presentation XML: add doc suffix Run on Semantic XML



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 9

def xref_preprocess(xml, isodoc)
  @isodoc = isodoc
  key = (0...8).map { rand(65..90).chr }.join # random string
  xml.root["type"] = key
  Metanorma::Utils::anchor_attributes
    .each do |(tag_name, attr_name)|
      #tag_name == "xref" and tag_name = "fmt-xref"
    ::Metanorma::Collection::Util::add_suffix_to_attrs(
      xml, xml.root["document_suffix"], tag_name, attr_name, isodoc
    )
  end
  key
end

.xref_process(section, xml, key, ident, isodoc, presxml) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 27

def xref_process(section, xml, key, ident, isodoc, presxml)
  @isodoc ||= isodoc
  svg_preprocess(section, Metanorma::Utils::to_ncname(ident), presxml)
  refs = eref_to_internal_eref(section, xml, key, presxml)
  refs += xref_to_internal_eref(section, xml, key, presxml)
  ins = new_hidden_ref(section)
  copied_refs = copy_repo_items_biblio(ins, section, xml)
  insert_indirect_biblio(ins, refs - copied_refs, key, xml)
end

.xref_to_internal_eref(section, xml, key, presxml) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 71

def xref_to_internal_eref(section, xml, key, presxml)
  key or return [] # no sectionsplit, no playing with xrefs
  bibitems, indirect = xref_to_internal_eref_prep(section, xml)
  tag = presxml ? "fmt-xref" : "xref"
  section.xpath(ns("//#{tag}")).each_with_object({}) do |x, m|
    xref_prefix_key(x, key, indirect)
    x["bibitemid"] = x["target"]
    m[x["bibitemid"]] = true
    xref_to_internal_eref_anchor(x, key, bibitems,
                                 xml.root["document_suffix"], presxml)
  end.keys
end

.xref_to_internal_eref_anchor(xref, key, bibitems, document_suffix, presxml) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/metanorma/collection/xrefprocess/xrefprocess.rb', line 91

def xref_to_internal_eref_anchor(xref, key, bibitems, document_suffix, presxml)
  t = xref["target"]
  if d = bibitems[t]&.at(ns("./docidentifier[@type = 'repository']"))
    m = %r{^([^/]+)}.match(d.text) and
      t.sub!(%r(#{m[0]}_), "")
  end
  key and t.sub!(%r{^#{key}_}, "")
  make_anchor(xref, t.sub(%r(_#{document_suffix}$), ""))
  xref.delete("target")
  xref.name = presxml ? "fmt-eref" : "eref"
end

.xref_to_internal_eref_prep(section, xml) ⇒ Object



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

def xref_to_internal_eref_prep(section, xml)
  bibitems = ::Metanorma::Collection::Util::gather_bibitems(section)
  indirect_bibitems = ::Metanorma::Collection::Util::gather_bibitems(xml)
    .select { |_, v| indirect_bib?(v) }
  [bibitems, indirect_bibitems]
end