Class: Metanorma::CollectionRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/collection_renderer.rb,
lib/metanorma/collection_fileparse.rb,
lib/metanorma/collection_fileprocess.rb

Overview

XML collection renderer

Defined Under Namespace

Classes: Dummy, PdfOptionsNode

Constant Summary collapse

FORMATS =
%i[html xml doc pdf].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, folder, options = {}) ⇒ CollectionRenderer

This is only going to render the HTML collection We presuppose that the bibdata of the document is equivalent to that of the collection, and that the flavour gem can sensibly process it. We may need to enhance metadata in the flavour gems isodoc/metadata.rb with collection metadata

Parameters:

  • xml (Metanorma::Collection)

    input XML collection

  • folder (String)

    input folder

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :coverpage (String)

    cover page HTML (Liquid template)

  • :format (Array<Symbol>)

    list of formats (xml,html,doc,pdf)

  • :output_folder (String)

    output directory



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/metanorma/collection_renderer.rb', line 30

def initialize(collection, folder, options = {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  check_options options
  @xml = Nokogiri::XML collection.to_xml # @xml is the collection manifest
  @lang = @xml.at(ns("//bibdata/language"))&.text || "en"
  @script = @xml.at(ns("//bibdata/script"))&.text || "Latn"
  @locale = @xml.at(ns("//bibdata/locale"))&.text
  @doctype = doctype
  require "metanorma-#{@doctype}"

  @isodoc = isodoc_create # output processor for flavour
  @outdir = dir_name_cleanse(options[:output_folder])
  @coverpage = options[:coverpage] || collection.coverpage
  @format = Util.sort_extensions_execution(options[:format])
  @compile_options = options[:compile] || {}
  @compile_options[:no_install_fonts] = true if options[:no_install_fonts]
  @log = options[:log]
  @documents = collection.documents
  @bibdata = collection.documents
  @directives = collection.directives
  @disambig = Util::DisambigFiles.new
  @compile = Compile.new
  @c = HTMLEntities.new

  # list of files in the collection
  #@files = read_files folder
  @files = Metanorma::FileLookup.new(folder, self)
  isodoc_populate
  create_non_existing_directory(@outdir)
end

Instance Attribute Details

#compileObject (readonly)

Returns the value of attribute compile.



16
17
18
# File 'lib/metanorma/collection_renderer.rb', line 16

def compile
  @compile
end

#compile_optionsObject (readonly)

Returns the value of attribute compile_options.



16
17
18
# File 'lib/metanorma/collection_renderer.rb', line 16

def compile_options
  @compile_options
end

#documentsObject (readonly)

Returns the value of attribute documents.



16
17
18
# File 'lib/metanorma/collection_renderer.rb', line 16

def documents
  @documents
end

#isodocObject

Returns the value of attribute isodoc.



15
16
17
# File 'lib/metanorma/collection_renderer.rb', line 15

def isodoc
  @isodoc
end

#xmlObject (readonly)

Returns the value of attribute xml.



16
17
18
# File 'lib/metanorma/collection_renderer.rb', line 16

def xml
  @xml
end

Class Method Details

.render(col, options = {}) ⇒ Object

Parameters:

Options Hash (options):

  • :coverpage (String)

    cover page HTML (Liquid template)

  • :format (Array<Symbol>)

    list of formats

  • :ourput_folder (Strong)

    output directory



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/metanorma/collection_renderer.rb', line 75

def self.render(col, options = {})
  folder = File.dirname col.file
  warn "\n\n\n\n\nRender Init: #{DateTime.now.strftime('%H:%M:%S')}"
  cr = new(col, folder, options)
  warn "\n\n\n\n\nRender Files: #{DateTime.now.strftime('%H:%M:%S')}"
  cr.files
  warn "\n\n\n\n\nConcatenate: #{DateTime.now.strftime('%H:%M:%S')}"
  cr.concatenate(col, options)
  warn "\n\n\n\n\nCoverpage: #{DateTime.now.strftime('%H:%M:%S')}"
  cr.coverpage if options[:format]&.include?(:html)
  warn "\n\n\n\n\nDone: #{DateTime.now.strftime('%H:%M:%S')}"
  cr
end

Instance Method Details

#collect_erefs(docxml) ⇒ Object



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

def collect_erefs(docxml)
  docxml.xpath(ns("//eref"))
    .each_with_object({ citeas: {}, bibitemid: {} }) do |i, m|
    m[:citeas][i["citeas"]] = true
    m[:bibitemid][i["bibitemid"]] = true
  end
end

#compile_options_update(identifier) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/metanorma/collection_fileprocess.rb', line 189

def compile_options_update(identifier)
  ret = @compile_options.dup
  Array(@directives).include?("presentation-xml") ||
    #@files[identifier][:presentationxml] and
    @files.get(identifier,:presentationxml) and
    ret.merge!(passthrough_presentation_xml: true)
  #@files[identifier][:sectionsplit] == "true" and
  @files.get(identifier,:sectionsplit) == "true" and
    ret.merge!(sectionsplit: "true")
  #@files[identifier][:bare] == true and
  @files.get(identifier,:bare) == true and
    ret.merge!(bare: true)
  ret
end

#concatenate(col, options) ⇒ Object



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

def concatenate(col, options)
  options[:format] << :presentation if options[:format].include?(:pdf)
  options[:format].uniq.each do |e|
    next unless %i(presentation xml).include?(e)

    ext = e == :presentation ? "presentation.xml" : e.to_s
    File.open(File.join(@outdir, "collection.#{ext}"), "w:UTF-8") do |f|
      f.write(concatenate1(col.clone, e).to_xml)
    end
  end
  options[:format].include?(:pdf) and
    pdfconv.convert(File.join(@outdir, "collection.presentation.xml"))
end

#concatenate1(out, ext) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/metanorma/collection_renderer.rb', line 103

def concatenate1(out, ext)
  out.directives << "documents-inline"
  out.bibdatas.each_key do |ident|
    #id = @c.decode(@isodoc.docid_prefix(nil, ident.dup))
    #@files[id][:attachment] || @files[id][:outputs].nil? and next
    id = @isodoc.docid_prefix(nil, ident.dup)
    @files.get(id,:attachment) || @files.get(id,:outputs).nil? and next

    out.documents[id] =
      #Metanorma::Document.raw_file(@files[id][:outputs][ext])
      Metanorma::Document.raw_file(@files.get(id,:outputs)[ext])
  end
  out
end

#copy_file_to_dest(identifier) ⇒ Object

def copy_file_to_dest(fileref)



221
222
223
224
225
226
227
# File 'lib/metanorma/collection_fileprocess.rb', line 221

def copy_file_to_dest(identifier)
  dest = File.join(@outdir, @files.get(identifier,:out_path))
  FileUtils.mkdir_p(File.dirname(dest))
  #FileUtils.cp fileref[:ref], dest
  #require "debug"; binding.b
  FileUtils.cp @files.get(identifier,:ref), dest
end

#coverpageObject

populate liquid template of ARGV with metadata extracted from collection manifest



192
193
194
195
196
197
# File 'lib/metanorma/collection_renderer.rb', line 192

def coverpage
  @coverpage or return
  File.open(File.join(@outdir, "index.html"), "w:UTF-8") do |f|
    f.write @isodoc.populate_template(File.read(@coverpage))
  end
end

#datauri_encode(docxml) ⇒ Object



132
133
134
135
136
137
# File 'lib/metanorma/collection_fileparse.rb', line 132

def datauri_encode(docxml)
  docxml.xpath(ns("//image")).each do |i|
    i["src"] = Metanorma::Utils::datauri(i["src"])
  end
  docxml
end

#dir_name_cleanse(name) ⇒ Object



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

def dir_name_cleanse(name)
  path = Pathname.new(name)
  clean_regex = /[<>:"|?*]/
  fallback_sym = "_"
  return name.gsub(clean_regex, fallback_sym) unless path.absolute?

  File.join(path.dirname,
            path.basename.to_s.gsub(clean_regex, fallback_sym))
end

#docid_to_citeas(bib) ⇒ Object



174
175
176
177
178
179
# File 'lib/metanorma/collection_fileparse.rb', line 174

def docid_to_citeas(bib)
  docid = bib.at(ns("./docidentifier[@primary = 'true']")) ||
    bib.at(ns("./docidentifier")) or return
  @c.decode(@isodoc
      .docid_prefix(docid["type"], docid.children.to_xml))
end

#docrefs(elm, builder) ⇒ Object

Parameters:

  • elm (Nokogiri::XML::Element)
  • builder (Nokogiri::XML::Builder)


220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/metanorma/collection_renderer.rb', line 220

def docrefs(elm, builder)
  elm.xpath(ns("./docref[@index = 'true']")).each do |d|
    ident = d.at(ns("./identifier")).children.to_xml
    ident = @c.decode(@isodoc.docid_prefix(nil, ident))
    builder.li do |li|
      li.a href: index_link(d, ident) do |a|
        a << ident.split(/([<>&])/).map do |x|
          /[<>&]/.match?(x) ? x : @c.encode(x, :hexadecimal)
        end.join
      end
    end
  end
end

#doctypeObject

infer the flavour from the first document identifier; relaton does that



175
176
177
178
179
180
181
182
183
184
# File 'lib/metanorma/collection_renderer.rb', line 175

def doctype
  if (docid = @xml.at(ns("//bibdata/docidentifier/@type"))&.text)
    dt = docid.downcase
  elsif (docid = @xml.at(ns("//bibdata/docidentifier"))&.text)
    dt = docid.sub(/\s.*$/, "").lowercase
  else return "standoc"
  end
  @registry = Metanorma::Registry.instance
  @registry.alias(dt.to_sym)&.to_s || dt
end

#dup_bibitem(docid, bib) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/metanorma/collection_fileparse.rb', line 77

def dup_bibitem(docid, bib)
  #newbib = @files[docid][:bibdata].dup
  newbib = @files.get(docid,:bibdata).dup
  newbib.name = "bibitem"
  newbib["hidden"] = "true"
  newbib&.at("./*[local-name() = 'ext']")&.remove
  newbib["id"] = bib["id"]
  newbib
end

#fail_update_bibitem(docid, identifier) ⇒ Object



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

def fail_update_bibitem(docid, identifier)
  error = "[metanorma] Cannot find crossreference to document #{docid} " \
          "in document #{identifier}."
  @log&.add("Cross-References", nil, error)
  Util.log(error, :warning)
end

#file_compile(file, filename, identifier) ⇒ Object

compile and output individual file in collection warn “metanorma compile -x html #Metanorma::CollectionRenderer.ff.path”



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/metanorma/collection_fileprocess.rb', line 173

def file_compile(file, filename, identifier)
  #return if @files[identifier][:sectionsplit] == "true"
  return if @files.get(identifier,:sectionsplit) == "true"

  opts = {
    format: :asciidoc,
    extension_keys: @format,
    output_dir: @outdir,
  }.merge(compile_options_update(identifier))

  @compile.compile file, opts
  #@files[identifier][:outputs] = {}
  @files.set(identifier,:outputs, {})
  file_compile_formats(filename, identifier)
end

#file_compile_formats(filename, identifier) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/metanorma/collection_fileprocess.rb', line 204

def file_compile_formats(filename, identifier)
  #file_id = @files[identifier]
  f = @files.get(identifier, :outputs)
  @format << :presentation if @format.include?(:pdf)
  @format.each do |e|
    ext = @compile.processor.output_formats[e]
    fn = File.basename(filename).sub(/(?<=\.)[^.]+$/, ext.to_s)
    #unless /html$/.match?(ext) && file_id[:sectionsplit]
    unless /html$/.match?(ext) && @files.get(identifier, :sectionsplit)
      #file_id[:outputs][e] = File.join(@outdir, fn)
      f[e] = File.join(@outdir, fn)
    end
  end
  @files.set(identifier,:outputs, f)
end

#filesObject

process each file in the collection files are held in memory, and altered as postprocessing



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/metanorma/collection_fileprocess.rb', line 231

def files # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  warn "\n\n\n\n\nInternal Refs: #{DateTime.now.strftime('%H:%M:%S')}"
  internal_refs = locate_internal_refs
  #@files.each_with_index do |(identifier, x), i|
  @files.keys.each_with_index do |identifier, i|
    i.positive? && Array(@directives).include?("bare-after-first") and
      @compile_options.merge!(bare: true)
    #if x[:attachment] then copy_file_to_dest(x)
    if @files.get(identifier,:attachment) then copy_file_to_dest(identifier)
    else
      #file, filename = @files.targetfile(x, read: true)
      file, filename = @files.targetfile_id(identifier, read: true)
      warn "\n\n\n\n\nProcess #{filename}: #{DateTime.now.strftime('%H:%M:%S')}"
      collection_xml = update_xrefs(file, identifier, internal_refs)
      collection_filename = File.basename(filename, File.extname(filename))
      collection_xml_path = File.join(Dir.tmpdir,
                                      "#{collection_filename}.xml")
      File.write collection_xml_path, collection_xml, encoding: "UTF-8"
      file_compile(collection_xml_path, filename, identifier)
      FileUtils.rm(collection_xml_path)
    end
  end
end

#gather_internal_refsObject

gather internal bibitem references



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/metanorma/collection_fileparse.rb', line 253

def gather_internal_refs
  #@files.each.with_object({}) do |(_, x), refs|
  @files.keys.each_with_object({}) do |i, refs|
    #x[:attachment] and next
    @files.get(i,:attachment) and next
    #file, = targetfile(x, read: true)
    file, = @files.targetfile_id(i, read: true)
    Nokogiri::XML(file)
      .xpath(ns("//bibitem[@type = 'internal']/" \
                "docidentifier[@type = 'repository']")).each do |d|
      a = d.text.split(%r{/}, 2)
      a.size > 1 or next
      refs[a[0]] ||= {}
      refs[a[0]][a[1]] = true
    end
  end
end

#get_bibitem_docid(bib, identifier) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/metanorma/collection_fileparse.rb', line 55

def get_bibitem_docid(bib, identifier)
  # IDs for repo references are untyped by default
  docid = bib.at(ns("./docidentifier[not(@type)]")) ||
    bib.at(ns("./docidentifier"))
  docid &&= @c.decode(@isodoc
      .docid_prefix(docid["type"], docid.children.to_xml)).gsub(/\s/, " ")
  #if @files[docid] then docid
  if @files.get(docid) || @files.get("X #{docid}") then docid
  else
    #require "debug"; binding.b
    fail_update_bibitem(docid, identifier)
    nil
  end
end

#hide_refs(docxml) ⇒ Object



108
109
110
111
112
113
# File 'lib/metanorma/collection_fileparse.rb', line 108

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


234
235
236
237
238
239
240
# File 'lib/metanorma/collection_renderer.rb', line 234

def index_link(docref, ident)
  if docref["fileref"]
    #@files[ident][:out_path].sub(/\.xml$/, ".html")
    @files.get(ident,:out_path).sub(/\.xml$/, ".html")
  else "#{docref['id']}.html"
  end
end

#indexfile(elm) ⇒ String

single level navigation list, with hierarchical nesting if multiple lists are needed as separate HTML fragments, multiple instances of this function will be needed, and associated to different variables in the call to @isodoc.metadata_init (including possibly an array of HTML fragments)

Parameters:

  • elm (Nokogiri::XML::Element)

Returns:

  • (String)

    XML



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/metanorma/collection_renderer.rb', line 250

def indexfile(elm)
  Nokogiri::HTML::Builder.new do |b|
    b.ul do
      b.li indexfile_title(elm)
      indexfile_docref(elm, b)
      elm.xpath(ns("./manifest")).each do |d|
        b << indexfile(d)
      end
    end
  end.doc.root.to_html
end

#indexfile_docref(elm, builder) ⇒ Object

uses the identifier to label documents; other attributes (title) can be looked up in @files[:bibdata]

Parameters:

  • elm (Nokogiri::XML::Element)
  • builder (Nokogiri::XML::Builder)


212
213
214
215
216
# File 'lib/metanorma/collection_renderer.rb', line 212

def indexfile_docref(elm, builder)
  return "" unless elm.at(ns("./docref[@index = 'true']"))

  builder.ul { |b| docrefs(elm, b) }
end

#indexfile_title(elm) ⇒ String

Parameters:

  • elm (Nokogiri::XML::Element)

Returns:

  • (String)


201
202
203
204
205
# File 'lib/metanorma/collection_renderer.rb', line 201

def indexfile_title(elm)
  lvl = elm.at(ns("./level"))&.text&.capitalize
  lbl = elm.at(ns("./title"))&.text
  "#{lvl}#{lvl && lbl ? ': ' : ''}#{lbl}"
end

#isodoc_builder(isodoc, node) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/metanorma/collection_renderer.rb', line 166

def isodoc_builder(isodoc, node)
  Nokogiri::HTML::Builder.new(encoding: "UTF-8") do |b|
    b.div do |div|
      node&.children&.each { |n| isodoc.parse(n, div) }
    end
  end.doc.root.to_html
end

#isodoc_createObject



143
144
145
146
147
148
149
150
# File 'lib/metanorma/collection_renderer.rb', line 143

def isodoc_create
  x = Asciidoctor.load nil, backend: @doctype.to_sym
  isodoc = x.converter.html_converter(Dummy.new) # to obtain Isodoc class
  isodoc.i18n_init(@lang, @script, @locale) # read in internationalisation
  isodoc.(@lang, @script, @locale, isodoc.i18n)
  isodoc.info(@xml, nil)
  isodoc
end

#isodoc_populateObject



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

def isodoc_populate
  # create the @meta class of isodoc, for populating Liquid,
  # with "navigation" set to the index bar.
  # extracted from the manifest
  @isodoc.meta.set(:navigation, indexfile(@xml.at(ns("//manifest"))))
  @isodoc.meta.set(:docrefs, liquid_docrefs)
  @isodoc.meta.set(:"prefatory-content",
                  isodoc_builder(@isodoc,
                                 @xml.at(ns("//prefatory-content"))))
  @isodoc.meta.set(:"final-content",
                  isodoc_builder(isodoc, @xml.at(ns("//final-content"))))
  @isodoc.info(@xml, nil)
end

#liquid_docrefsObject



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/metanorma/collection_renderer.rb', line 262

def liquid_docrefs
  @xml.xpath(ns("//docref[@index = 'true']")).each_with_object([]) do |d, m|
    ident = d.at(ns("./identifier")).children.to_xml
    ident = @c.decode(@isodoc.docid_prefix(nil, ident))
    title = d.at(ns("./bibdata/title[@type = 'main']")) ||
      d.at(ns("./bibdata/title")) || d.at(ns("./title"))
    m << { "identifier" => ident, "file" => index_link(d, ident),
           "title" => title&.children&.to_xml,
           "level" => d.at(ns("./level"))&.text }
  end
end

#locate_internal_refsObject

resolve file location for the target of each internal reference



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/metanorma/collection_fileparse.rb', line 272

def locate_internal_refs
  #require 'debug'; binding.b
  refs = gather_internal_refs
  #@files.keys.reject { |k| @files[k][:attachment] }.each do |identifier|
  @files.keys.reject { |k| @files.get(k,:attachment) }.each do |identifier|
    #id = @c.decode(@isodoc.docid_prefix("", identifier.dup))
    #locate_internal_refs1(refs, identifier, @files[id])
    id = @isodoc.docid_prefix("", identifier.dup)
    locate_internal_refs1(refs, identifier, id)
  end
  refs.each do |schema, ids|
    ids.each do |id, key|
      key == true and refs[schema][id] = "Missing:#{schema}:#{id}"
    end
  end
  refs
end

#locate_internal_refs1(refs, identifier, ident) ⇒ Object

def locate_internal_refs1(refs, identifier, filedesc)



291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/metanorma/collection_fileparse.rb', line 291

def locate_internal_refs1(refs, identifier, ident)
  #file, _filename = targetfile(filedesc, read: true)
  file, _filename = @files.targetfile_id(ident, read: true)
  xml = Nokogiri::XML(file) { |config| config.huge }
  t = xml.xpath("//*/@id").each_with_object({}) { |i, x| x[i.text] = true }
  refs.each do |schema, ids|
    ids.keys.select { |id| t[id] }.each do |id|
      n = xml.at("//*[@id = '#{id}']") and
        n.at("./ancestor-or-self::*[@type = '#{schema}']") and
        refs[schema][id] = identifier
    end
  end
end

#ns(xpath) ⇒ Object



186
187
188
# File 'lib/metanorma/collection_renderer.rb', line 186

def ns(xpath)
  IsoDoc::Convert.new({}).ns(xpath)
end

#pdfconvObject



118
119
120
121
122
# File 'lib/metanorma/collection_renderer.rb', line 118

def pdfconv
  doctype = @doctype.to_sym
  x = Asciidoctor.load nil, backend: doctype
  x.converter.pdf_converter(PdfOptionsNode.new(doctype, @compile_options))
end

#supply_repo_ids(docxml) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/metanorma/collection_fileparse.rb', line 115

def supply_repo_ids(docxml)
  docxml.xpath(ns("//bibitem[not(ancestor::bibitem)]")).each do |b|
    next if b&.at(ns("./docidentifier[@type = 'repository']"))

    b.xpath(ns("./docidentifier")).each do |docid|
      #id = @c.decode(@isodoc
      #.docid_prefix(docid["type"], docid.children.to_xml))
      id = @isodoc
      .docid_prefix(docid["type"], docid.children.to_xml)
      #@files[id] or next
      @files.get(id) or next
      docid.next = "<docidentifier type='repository'>" \
                   "current-metanorma-collection/#{id}"
    end
  end
end

#svgmap_resolve(docxml) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/metanorma/collection_fileparse.rb', line 139

def svgmap_resolve(docxml)
  isodoc = IsoDoc::PresentationXMLConvert.new({})
  isodoc.bibitem_lookup(docxml)
  docxml.xpath(ns("//svgmap//eref")).each do |e|
    svgmap_resolve1(e, isodoc)
  end
  Metanorma::Utils::svgmap_rewrite(docxml, "")
end

#svgmap_resolve1(eref, isodoc) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/metanorma/collection_fileparse.rb', line 148

def svgmap_resolve1(eref, isodoc)
  href = isodoc.eref_target(eref)
  return if href == "##{eref['bibitemid']}" ||
    (href =~ /^#/ && !docxml.at("//*[@id = '#{href.sub(/^#/, '')}']"))

  eref["target"] = href.strip
  eref.name = "link"
  eref&.elements&.remove
end

#update_anchor_create_loc(_bib, eref, docid) ⇒ Object

if there is a crossref to another document, with no anchor, retrieve the anchor given the locality, and insert it into the crossref



241
242
243
244
245
246
247
248
249
250
# File 'lib/metanorma/collection_fileparse.rb', line 241

def update_anchor_create_loc(_bib, eref, docid)
  ins = eref.at(ns("./localityStack")) or return
  type = ins.at(ns("./locality/@type"))&.text
  type = "clause" if type == "annex"
  ref = ins.at(ns("./locality/referenceFrom"))&.text
  #anchor = @files[docid][:anchors].dig(type, ref) or return
  anchor = @files.get(docid,:anchors).dig(type, ref) or return
  ins << "<locality type='anchor'><referenceFrom>#{anchor.sub(/^_/, '')}" \
         "</referenceFrom></locality>"
end

#update_anchor_loc(bib, eref, docid) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/metanorma/collection_fileparse.rb', line 225

def update_anchor_loc(bib, eref, docid)
  loc = eref.at(ns(".//locality[@type = 'anchor']")) or
    return update_anchor_create_loc(bib, eref, docid)
  document_suffix = Metanorma::Utils::to_ncname(docid)
  ref = loc.at(ns("./referenceFrom")) or return
  anchor = "#{ref.text}_#{document_suffix}"
  #return unless @files[docid][:anchors].inject([]) do |m, (_, x)|
  return unless @files.get(docid,:anchors).inject([]) do |m, (_, x)|
    m += x.values
  end.include?(anchor)

  ref.content = anchor
end

#update_anchors(bib, docxml, docid) ⇒ Object

update crossrefences to other documents, to include disambiguating document suffix on id



214
215
216
217
218
219
220
221
222
223
# File 'lib/metanorma/collection_fileparse.rb', line 214

def update_anchors(bib, docxml, docid) # rubocop:disable Metrics/AbcSize
  docxml.xpath("//xmlns:eref[@citeas = '#{docid}']").each do |e|
    #if @files[docid] then update_anchor_loc(bib, e, docid)
    if @files.get(docid) then update_anchor_loc(bib, e, docid)
    else
      e << "<strong>** Unresolved reference to document #{docid} " \
           "from eref</strong>"
    end
  end
end

#update_bibitem(bib, identifier) ⇒ Object

Parameters:

  • bib (Nokogiri::XML::Element)
  • identifier (String)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/metanorma/collection_fileparse.rb', line 43

def update_bibitem(bib, identifier)
  docid = get_bibitem_docid(bib, identifier) or return
  newbib = dup_bibitem(docid, bib)
  bib.replace(newbib)
  #_file, url = targetfile(@files[docid], relative: true, read: false, doc: !@files[docid][:attachment])
  _file, url = @files.targetfile_id(docid, relative: true, read: false, doc: !@files.get(docid,:attachment))
  uri_node = Nokogiri::XML::Node.new "uri", newbib.document
  uri_node[:type] = "citation"
  uri_node.content = url
  newbib.at(ns("./docidentifier")).previous = uri_node
end

#update_direct_refs_to_docs(docxml, identifier) ⇒ Object

repo(current-metanorma-collection/ISO 17301-1:2016) replaced by bibdata of “ISO 17301-1:2016” in situ as bibitem. Any erefs to that bibitem id are replaced with relative URL Preferably with anchor, and is a job to realise dynamic lookup of localities.



163
164
165
166
167
168
169
170
171
172
# File 'lib/metanorma/collection_fileparse.rb', line 163

def update_direct_refs_to_docs(docxml, identifier)
  erefs = collect_erefs(docxml)
  docxml.xpath(ns("//bibitem[not(ancestor::bibitem)]")).each do |b|
    docid = b.at(ns("./docidentifier[@type = 'repository']"))
    (docid && %r{^current-metanorma-collection/}.match(docid.text)) or next
    update_bibitem(b, identifier)
    docid = docid_to_citeas(b) or next
    erefs[:citeas][docid] and update_anchors(b, docxml, docid)
  end
end

#update_indirect_refs_to_docs(docxml, internal_refs) ⇒ Object

Resolve erefs to a container of ids in another doc, to an anchor eref (direct link)



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

def update_indirect_refs_to_docs(docxml, internal_refs)
  internal_refs.each do |schema, ids|
    ids.each do |id, file|
      update_indirect_refs_to_docs1(docxml, schema, id, file)
    end
  end
end

#update_indirect_refs_to_docs1(docxml, schema, id, file) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/metanorma/collection_fileparse.rb', line 199

def update_indirect_refs_to_docs1(docxml, schema, id, file)
  docxml.xpath(ns("//eref[@bibitemid = '#{schema}_#{id}']")).each do |e|
    e["citeas"] = file
    if a = e.at(ns(".//locality[@type = 'anchor']/referenceFrom"))
      a.children = "#{a.text}_#{Metanorma::Utils::to_ncname(file)}"
    end
  end
  docid = docxml.at(ns("//bibitem[@id = '#{schema}_#{id}']/" \
                       "docidentifier[@type = 'repository']")) or return
  docid.children = "current-metanorma-collection/#{file}"
  docid.previous = "<docidentifier type='X'>#{file}</docidentifier>"
end

#update_xrefs(file, identifier, internal_refs) ⇒ String

Resolves direct links to other files in collection (repo(current-metanorma-collection/x), and indirect links to other files in collection (bibitem[@type = ‘internal’] pointing to a file anchor in another file in the collection)

Parameters:

  • file (String)

    XML content

  • identifier (String)

    docid

  • internal_refs (Hash{String=>Hash{String=>String}] schema name to anchor to filename)

    nternal_refs [HashString=>Hash{String=>String] schema name to anchor to filename

Returns:

  • (String)

    XML content



97
98
99
100
101
102
103
104
105
106
# File 'lib/metanorma/collection_fileparse.rb', line 97

def update_xrefs(file, identifier, internal_refs)
  docxml = Nokogiri::XML(file) { |config| config.huge }
  supply_repo_ids(docxml)
  update_indirect_refs_to_docs(docxml, internal_refs)
  @files.add_document_suffix(identifier, docxml)
  update_direct_refs_to_docs(docxml, identifier)
  svgmap_resolve(datauri_encode(docxml))
  hide_refs(docxml)
  docxml.to_xml
end