Class: Metanorma::Collection::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/collection/renderer/svg.rb,
lib/metanorma/collection/renderer/utils.rb,
lib/metanorma/collection/renderer/renderer.rb,
lib/metanorma/collection/renderer/fileparse.rb,
lib/metanorma/collection/renderer/navigation.rb,
lib/metanorma/collection/renderer/fileprocess.rb,
lib/metanorma/collection/renderer/render_word.rb

Defined Under Namespace

Classes: DocOptionsNode, PdfOptionsNode

Constant Summary collapse

FORMATS =
%i[html xml doc pdf].freeze
ANCHOR_XPATH =
"xmlns:locality[@type = 'anchor']/xmlns:referenceFrom"
.freeze
BIBITEM_NOT_REPO_XPATH =
<<~XPATH.strip
//bibitem[not(ancestor::bibitem)][not(ancestor::bibdata)][not(./docidentifier[@type = 'repository'])]
XPATH
SECTION_BREAK =
'<p class="MsoNormal"><br clear="all" class="section"/></p>'.freeze
DIV1 =
'<div class="WordSection1">&#xa0;</div>'.freeze
DIV2 =
'<div class="WordSection2">&#xa0;</div>'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/metanorma/collection/renderer/renderer.rb', line 35

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
  @xml.root.default_namespace = "http://metanorma.org"
  @lang = collection.bibdata.language.first || "en"
  @script = collection.bibdata.script.first || "Latn"
  @locale = @xml.at("//xmlns:bibdata/xmlns:locale")&.text
  @registry = Metanorma::Registry.instance
  @flavor = options[:flavor] || flavor
  @compile = Compile.new
  @compile.load_flavor(@flavor)

  # output processor for flavour
  @isodoc = Util::isodoc_create(@flavor, @lang, @script, @xml)
  @isodoc_presxml = Util::isodoc_create(@flavor, @lang, @script, @xml,
                                        presxml: true)
  @outdir = dir_name_cleanse(options[:output_folder])
  @coverpage = options[:coverpage] || collection.coverpage
  @format = ::Metanorma::Util.sort_extensions_execution(options[:format])
  @compile_options = options[:compile] || {}
  @compile_options[:install_fonts] = true if options[:install_fonts]
  @log = options[:log]
  @log&.add_msg(METANORMA_LOG_MESSAGES)
  @bibdata = collection.bibdata
  @documents = collection.documents
  @bibdatas = collection.documents
  @directives = collection.directives
  @dirname = collection.dirname
  @manifest = collection.manifest.config
  @disambig = Util::DisambigFiles.new
  @prefatory = collection.prefatory
  @final = collection.final
  @c = HTMLEntities.new
  @files_to_delete = []
  @nested = options[:nested]
  # if false, this is the root instance of Renderer
  # if true, then this is not the last time Renderer will be run
  # (e.g. this is sectionsplit)

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

Instance Attribute Details

#compileObject (readonly)

Returns the value of attribute compile.



20
21
22
# File 'lib/metanorma/collection/renderer/renderer.rb', line 20

def compile
  @compile
end

#compile_optionsObject (readonly)

Returns the value of attribute compile_options.



20
21
22
# File 'lib/metanorma/collection/renderer/renderer.rb', line 20

def compile_options
  @compile_options
end

#documentsObject (readonly)

Returns the value of attribute documents.



20
21
22
# File 'lib/metanorma/collection/renderer/renderer.rb', line 20

def documents
  @documents
end

#isodocObject

Returns the value of attribute isodoc.



19
20
21
# File 'lib/metanorma/collection/renderer/renderer.rb', line 19

def isodoc
  @isodoc
end

#isodoc_presxmlObject

Returns the value of attribute isodoc_presxml.



19
20
21
# File 'lib/metanorma/collection/renderer/renderer.rb', line 19

def isodoc_presxml
  @isodoc_presxml
end

#manifestObject (readonly)

Returns the value of attribute manifest.



20
21
22
# File 'lib/metanorma/collection/renderer/renderer.rb', line 20

def manifest
  @manifest
end

#nestedObject

Returns the value of attribute nested.



19
20
21
# File 'lib/metanorma/collection/renderer/renderer.rb', line 19

def nested
  @nested
end

#outdirObject (readonly)

Returns the value of attribute outdir.



20
21
22
# File 'lib/metanorma/collection/renderer/renderer.rb', line 20

def outdir
  @outdir
end

#xmlObject (readonly)

Returns the value of attribute xml.



20
21
22
# File 'lib/metanorma/collection/renderer/renderer.rb', line 20

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



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

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

Instance Method Details

#add_hidden_bibliography(xmldoc, refs) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/metanorma/collection/renderer/utils.rb', line 73

def add_hidden_bibliography(xmldoc, refs)
  ins = new_hidden_ref(xmldoc)
  refs.each do |k, v|
    url = @files.url(v, {})
    ins << <<~XML
      <bibitem id="#{k}" anchor="#{k}">#{docid_xml(v)}<uri type='citation'>#{url}</uri></bibitem>
    XML
  end
end

#build_indexfile1(mnf) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/metanorma/collection/renderer/navigation.rb', line 66

def build_indexfile1(mnf)
  Nokogiri::HTML::Builder.new do |b|
    if mnf.file then docrefs(mnf, b)
    else
      b.li do |l|
        l << indexfile_title(mnf)
        l.ul do |u|
          Array(mnf.entry).each { |e| u << indexfile1(e) }
        end
      end
    end
  end
end

#cleanup_indexfile1(ret) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/metanorma/collection/renderer/navigation.rb', line 80

def cleanup_indexfile1(ret)
  ret = ret.doc.root
  ret.xpath("/ul").each do |u|
    if u.at("./li/ul") && !u.at("./li[text()]")
      u.replace(u.xpath("./li/ul"))
    end
  end
  ret.to_html
end

#collect_erefs(docxml, presxml) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/metanorma/collection/renderer/utils.rb', line 59

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

#collection_coverpages(conv, docs) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/metanorma/collection/renderer/render_word.rb', line 72

def collection_coverpages(conv, docs)
  conv.wordintropage and [DIV2, SECTION_BREAK].reverse_each do |s|
    docs.unshift(Nokogiri::XML(s).root)
  end
  conv.wordcoverpage and [DIV1, SECTION_BREAK].reverse_each do |s|
    docs.unshift(Nokogiri::XML(s).root)
  end
  docs
end

#compile_options_update(identifier) ⇒ Object



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

def compile_options_update(identifier)
  ret = @compile_options.dup
  @directives.detect { |d| d.key == "presentation-xml" } ||
    @files.get(identifier, :presentationxml) and
    ret.merge!(passthrough_presentation_xml: true)
  @files.get(identifier, :sectionsplit) == true and
    ret.merge!(sectionsplit: true)
  @files.get(identifier, :bare) == true and
    ret.merge!(bare: true)
  ret
end

#concat_extract_files(filename) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/metanorma/collection/renderer/render_word.rb', line 11

def concat_extract_files(filename)
  xml = Nokogiri::XML(File.read(filename, encoding: "UTF-8"), &:huge)
  docs = xml.xpath(ns("//doc-container")).each_with_object([]) do |x, m|
    n = Nokogiri::XML::Document.new
    n.add_child(x.elements.first.remove)
    m << n
  end
  [wrapping_doc(docs.first.dup, xml), docs]
end

#concatenate(col, options) ⇒ Object



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

def concatenate(col, options)
  warn "\n\n\n\n\nConcatenate: #{DateTime.now.strftime('%H:%M:%S')}"
  concatenate_presentation?(options) and
    options[:format] << :presentation
  concatenate_prep(col, options)
  concatenate_outputs(options)
end

#concatenate1(out, ext) ⇒ Object



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

def concatenate1(out, ext)
  out.directives << ::Metanorma::Collection::Config::Directive
    .new(key: "documents-inline")
  out.bibdatas.each_key do |ident|
    id = @isodoc.docid_prefix(nil, ident.dup)
    @files.get(id, :attachment) || @files.get(id, :outputs).nil? and next
    out.documents[Util::key id] =
      Metanorma::Collection::Document
        .raw_file(@files.get(id, :outputs)[ext])
  end
  out
end

#concatenate_outputs(options) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/metanorma/collection/renderer/renderer.rb', line 150

def concatenate_outputs(options)
  pres = File.join(@outdir, "collection.presentation.xml")
  options[:format].include?(:pdf) and pdfconv.convert(pres)
  options[:format].include?(:doc) and docconv_convert(pres)
  @directives.detect { |d| d.key == "bilingual" } &&
    options[:format].include?(:html) and
    Metanorma::Collection::Multilingual.new(
      { flavor: flavor.to_sym,
        converter_options: PdfOptionsNode.new(flavor, @compile_options),
        outdir: @outdir },
    ).to_html(pres)
end

#concatenate_prep(col, options) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/metanorma/collection/renderer/renderer.rb', line 128

def concatenate_prep(col, options)
  warn options[:format]
  %i(xml presentation).each do |e|
    options[:format].include?(e) or next
    ext = e == :presentation ? "presentation.xml" : e.to_s
    File.open(File.join(@outdir, "collection.#{ext}"), "w:UTF-8") do |f|
      b = concatenate1(col.clone, e).to_xml
      e == :presentation and b = concatenate_presentation(b)
      f.write(b)
    end
  end
end

#concatenate_presentation(xml) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/metanorma/collection/renderer/renderer.rb', line 141

def concatenate_presentation(xml)
  xml.sub!("<metanorma-collection>", "<metanorma-collection xmlns='http://metanorma.org'>")
  # TODO BEING FORCED TO DO THAT BECAUSE SHALE IS NOT DEALING WITH DEFAULT NAMESPACES
  @directives.detect { |d| d.key == "bilingual" } and
    xml = Metanorma::Collection::Multilingual
      .new({ align_cross_elements: %w(p note) }).to_bilingual(xml)
  xml
end

#concatenate_presentation?(options) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
126
# File 'lib/metanorma/collection/renderer/renderer.rb', line 122

def concatenate_presentation?(options)
  !(options[:format] & %i(pdf doc)).empty? ||
    (@directives.detect { |d| d.key == "bilingual" } &&
    options[:format].include?(:html))
end

#copy_file_to_dest(identifier) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/metanorma/collection/renderer/fileprocess.rb', line 63

def copy_file_to_dest(identifier)
  out = Pathname.new(@files.get(identifier, :out_path)).cleanpath
  out.absolute? and
    out = out.relative_path_from(File.expand_path(FileUtils.pwd))
  dest = File.join(@outdir, @disambig.source2dest_filename(out.to_s))
  FileUtils.mkdir_p(File.dirname(dest))
  source = @files.get(identifier, :ref)
  source != dest and FileUtils.cp_r source, dest, remove_destination: true
end

#coverpageObject

populate liquid template of ARGV with metadata extracted from collection manifest



184
185
186
187
188
189
190
191
192
193
# File 'lib/metanorma/collection/renderer/renderer.rb', line 184

def coverpage
  @coverpage or return

  @coverpage_path = Util::rel_path_resolve(@dirname, @coverpage)
  warn "\n\n\n\n\nCoverpage: #{DateTime.now.strftime('%H:%M:%S')}"

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

#deep_detached_clone(node) ⇒ Object



24
25
26
# File 'lib/metanorma/collection/renderer/utils.rb', line 24

def deep_detached_clone(node)
  Nokogiri::XML(node.to_xml).root
end

#dir_name_cleanse(name) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/metanorma/collection/renderer/utils.rb', line 4

def dir_name_cleanse(name)
  path = Pathname.new(name)
  clean_regex = /[<>:"|?*\p{Zs}]/
  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

#docconvObject



4
5
6
7
8
9
# File 'lib/metanorma/collection/renderer/render_word.rb', line 4

def docconv
  @tempfile_cache ||= []
  flavor = @flavor.to_sym
  x = Asciidoctor.load nil, backend: flavor
  x.converter.doc_converter(DocOptionsNode.new(@directives, @dirname))
end

#docconv_convert(filename) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/metanorma/collection/renderer/render_word.rb', line 82

def docconv_convert(filename)
  pref_file, docs = concat_extract_files(filename)
  body = docconv_convert1(docs)
  collection_conv = overall_docconv_converter(body)
  collection_coverpages(collection_conv, body)
  collection_conv.convert(filename, pref_file.to_xml, false)
end

#docconv_convert1(docs) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/metanorma/collection/renderer/render_word.rb', line 59

def docconv_convert1(docs)
  docs.each_with_index.with_object([]) do |(d, i), m|
    conv = docconv
    conv.convert_init(d.to_xml(encoding: "UTF-8"), "xxxx", false)
    html = conv.postprocess_cleanup(conv.convert1(d, "xxx", "."))
    @tempfile_cache += conv.tempfile_cache # hold on to the temp img files
    b = Nokogiri::XML(html).at("//body")
    i == docs.size - 1 or
      b << '<p class="MsoNormal"><br clear="all" class="section"/></p>'
    m << b.children
  end
end

#docid_to_citeas(bib) ⇒ Object



53
54
55
56
57
# File 'lib/metanorma/collection/renderer/utils.rb', line 53

def docid_to_citeas(bib)
  docid = bib.at(ns("./docidentifier[@primary = 'true']")) ||
    bib.at(ns("./docidentifier")) or return
  ::Metanorma::Collection::Util::key(docid_prefix(docid))
end

#docid_xml(val) ⇒ Object



68
69
70
71
# File 'lib/metanorma/collection/renderer/utils.rb', line 68

def docid_xml(val)
  "<docidentifier type='repository'>current-metanorma-collection/" \
    "#{val}</docidentifier>"
end

#docref_ident(docref) ⇒ Object



37
38
39
40
# File 'lib/metanorma/collection/renderer/navigation.rb', line 37

def docref_ident(docref)
  ident = docref.identifier.dup
  @c.decode(@isodoc.docid_prefix(nil, ident))
end

#docrefs(mnf, builder) ⇒ Object



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

def docrefs(mnf, builder)
  ident = docref_ident(mnf)
  builder.li do |li|
    li.a href: index_link(mnf, ident) do |a|
      a << ident.split(/([<>&])/).map do |x|
        /[<>&]/.match?(x) ? x : @c.encode(x, :hexadecimal)
      end.join
    end
  end
end

#dup_bibitem(docid, bib) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/metanorma/collection/renderer/utils.rb', line 14

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

#file_compile(file, filename, identifier) ⇒ Object

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



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/metanorma/collection/renderer/fileprocess.rb', line 12

def file_compile(file, filename, identifier)
  @files.get(identifier, :sectionsplit) and return
  opts = {
    format: :asciidoc,
    extension_keys: @files.get(identifier, :format),
    output_dir: @outdir,
    type: @flavor,
  }.merge(compile_options_update(identifier))
  @compile.compile file, opts
  @files.set(identifier, :outputs, {})
  file_compile_formats(filename, identifier, opts)
end

#file_compile_format(filename, identifier, format, output_formats) ⇒ Object



56
57
58
59
60
61
# File 'lib/metanorma/collection/renderer/fileprocess.rb', line 56

def file_compile_format(filename, identifier, format, output_formats)
  ext = @compile.processor.output_formats[format]
  fn = File.basename(filename).sub(/(?<=\.)[^.]+$/, ext.to_s)
  (/html$/.match?(ext) && @files.get(identifier, :sectionsplit)) or
    output_formats[format] = File.join(@outdir, fn)
end

#file_compile_formats(filename, identifier, opts) ⇒ Object

def allowed_extension_keys

  ret = @format.dup
  @directives.detect { |d| d.key == "individual-pdf" } or
    ret.delete(:pdf)
  @directives.detect { |d| d.key == "individual-doc" } or
    ret.delete(:doc)
  ret
end


46
47
48
49
50
51
52
53
54
# File 'lib/metanorma/collection/renderer/fileprocess.rb', line 46

def file_compile_formats(filename, identifier, opts)
  f = @files.get(identifier, :outputs)
  format = opts[:extension_keys]
  concatenate_presentation?({ format: format }) and format << :presentation
  format.each do |e|
    file_compile_format(filename, identifier, e, f)
  end
  @files.set(identifier, :outputs, f)
end

#filesObject

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/metanorma/collection/renderer/fileprocess.rb', line 75

def files # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  warn "\n\n\n\n\nRender Files: #{DateTime.now.strftime('%H:%M:%S')}"
  internal_refs = locate_internal_refs
  @files.keys.each_with_index do |ident, i|
    i.positive? && @directives.detect do |d|
      d.key == "bare-after-first"
    end and
      @compile_options.merge!(bare: true)
    if @files.get(ident, :attachment) then copy_file_to_dest(ident)
    else
      file, fname = @files.targetfile_id(ident, read: true)
      warn "\n\n\n\n\nProcess #{fname}: #{DateTime.now.strftime('%H:%M:%S')}"
      collection_xml = update_xrefs(file, ident, internal_refs)
      collection_filename = File.basename(fname, File.extname(fname))
      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, fname, ident)
      FileUtils.rm(collection_xml_path)
    end
  end
end

#flavorObject

TODO: infer flavor from publisher when available



177
178
179
180
# File 'lib/metanorma/collection/renderer/renderer.rb', line 177

def flavor
  dt = @xml.at("//bibdata/ext/flavor")&.text or return "standoc"
  @registry.alias(dt.to_sym)&.to_s || dt
end

#flush_filesObject



81
82
83
84
85
86
87
# File 'lib/metanorma/collection/renderer/renderer.rb', line 81

def flush_files
  warn "\n\n\n\n\nDone: #{DateTime.now.strftime('%H:%M:%S')}"
  warn "\nFiles to delete:\n"
  warn @files.files_to_delete
  @files.files_to_delete.each { |f| FileUtils.rm_f(f) }
  @files_to_delete.each { |f| FileUtils.rm_f(f) }
end

#gather_internal_refsObject

gather internal bibitem references



99
100
101
102
103
104
105
106
# File 'lib/metanorma/collection/renderer/fileprocess.rb', line 99

def gather_internal_refs
  @files.keys.each_with_object({}) do |i, refs|
    @files.get(i, :attachment) and next
    @files.get(i, :sectionsplit) and next
    file, = @files.targetfile_id(i, read: true)
    gather_internal_refs1(file, i, refs)
  end
end

#gather_internal_refs1(file, ident, refs) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/metanorma/collection/renderer/fileprocess.rb', line 108

def gather_internal_refs1(file, ident, refs)
  f = Nokogiri::XML(file, &:huge)
  !@files.get(ident, :sectionsplit) and
    gather_internal_refs_indirect(f, refs)
  key = @files.get(ident, :indirect_key) and
    gather_internal_refs_sectionsplit(f, ident, key, refs)
end

#gather_internal_refs_indirect(doc, refs) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/metanorma/collection/renderer/fileprocess.rb', line 116

def gather_internal_refs_indirect(doc, refs)
  doc.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]] = false
               end
end

#gather_internal_refs_sectionsplit(_doc, ident, key, refs) ⇒ Object



126
127
128
129
130
131
# File 'lib/metanorma/collection/renderer/fileprocess.rb', line 126

def gather_internal_refs_sectionsplit(_doc, ident, key, refs)
  refs[key] ||= {}
  @files.get(ident, :ids).each_key do |k|
    refs[key][k] = false
  end
end

#get_bibitem_docid(bib, identifier) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/metanorma/collection/renderer/utils.rb', line 28

def get_bibitem_docid(bib, identifier)
  docid =
    bib.at(ns("./docidentifier[@type = 'metanorma-collection']")) ||
    bib.at(ns("./docidentifier[not(@type)]")) ||
    bib.at(ns("./docidentifier"))
  docid &&= docid_prefix(docid)
  if @files.get(docid) then docid
  else
    fail_update_bibitem(docid, identifier)
    nil
  end
end

#index?(mnf) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/metanorma/collection/renderer/navigation.rb', line 56

def index?(mnf)
  mnf.index and return true
  mnf.entry&.detect { |e| index?(e) }
end


42
43
44
45
46
47
# File 'lib/metanorma/collection/renderer/navigation.rb', line 42

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

#index_object(mnf) ⇒ Object

object to construct navigation out of in Liquid



91
92
93
94
95
96
97
98
# File 'lib/metanorma/collection/renderer/navigation.rb', line 91

def index_object(mnf)
  mnf = Array(mnf).first
  ret = { title: indexfile_title(mnf), level: mnf.type,
          docrefs: index_object_docrefs(mnf),
          children: index_object_children(mnf) }.compact
  ret.keys == [:children] and ret = ret[:children]
  ret
end

#index_object_children(mnf) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/metanorma/collection/renderer/navigation.rb', line 100

def index_object_children(mnf)
  nonfiles = Array(mnf.entry).reject(&:file)
  c = nonfiles.each_with_object([]) do |d, b|
    b << index_object(d)
  end.flatten
  c.empty? and c = nil
  c
end

#index_object_docrefs(mnf) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/metanorma/collection/renderer/navigation.rb', line 109

def index_object_docrefs(mnf)
  files = Array(mnf.entry).select(&:file)
  files.empty? and return nil
  r = Nokogiri::HTML::Builder.new do |b|
    b.ul do |u|
      files.each { |f| docrefs(f, u) }
    end
  end
  r.doc.root&.to_html&.tr("\n", " ")
end

#indexfile(mnf) ⇒ Object

single level navigation list, with hierarchical nesting



50
51
52
53
54
# File 'lib/metanorma/collection/renderer/navigation.rb', line 50

def indexfile(mnf)
  mnfs = Array(mnf)
  mnfs.empty? and return ""
  mnfs.map { |m| "<ul>#{indexfile1(m)}</ul>" }.join("\n")
end

#indexfile1(mnf) ⇒ Object



61
62
63
64
# File 'lib/metanorma/collection/renderer/navigation.rb', line 61

def indexfile1(mnf)
  index?(mnf) or return ""
  cleanup_indexfile1(build_indexfile1(mnf))
end

#indexfile_docref(mnf, builder) ⇒ Object

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

Parameters:



21
22
23
24
# File 'lib/metanorma/collection/renderer/navigation.rb', line 21

def indexfile_docref(mnf, builder)
  Array(mnf.entry).detect(&:index) or return ""
  builder.ul { |b| docrefs(mnf, b) }
end

#indexfile_title(entry) ⇒ String

Parameters:

  • elm (Nokogiri::XML::Element)

Returns:

  • (String)


6
7
8
9
10
11
12
13
14
# File 'lib/metanorma/collection/renderer/navigation.rb', line 6

def indexfile_title(entry)
  if entry.bibdata &&
      x = entry.bibdata.title.detect { |t| t.type == "main" } ||
          entry.bibdata.title.first
    x.title.content
  else
    entry.title
  end
end

#indirect_ref_key(schema, id, doc_suffix, add_suffix) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 152

def indirect_ref_key(schema, id, doc_suffix, add_suffix)
  /^#{schema}_/.match?(id) and return id
  x = @indirect_keys.dig(schema, id) and return x
  @indirect_keys[schema] ||= {}
  @indirect_keys[schema][id] = if add_suffix
                                 "#{schema}_#{id}_#{doc_suffix}"
                               else
                                 "#{schema}_#{id}"
                               end
end

#liquid_docrefs(mnfs) ⇒ Object



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

def liquid_docrefs(mnfs)
  Array(mnfs).select(&:index).each_with_object([]) do |d, m|
    if d.file
      ident = @c.decode(@isodoc.docid_prefix(nil, d.identifier.dup))
      m << { "identifier" => ident, "file" => index_link(d, ident),
             "title" => indexfile_title(d), "level" => d.type }
    else
      liquid_docrefs(d.entry).each { |m1| m << m1 }
    end
  end
end

#locate_internal_refsObject

resolve file location for the target of each internal reference



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

def locate_internal_refs
  warn "\n\n\n\n\nInternal Refs: #{DateTime.now.strftime('%H:%M:%S')}"
  refs = populate_internal_refs(gather_internal_refs)
  refs.each do |schema, ids|
    ids.each do |id, key|
      key and next
      refs[schema][id] = "Missing:#{schema}:#{id}"
      @log&.add("METANORMA_1", nil, params: [refs[schema][id]])
    end
  end
  refs
end

#locate_internal_refs1(refs, identifier, ident) ⇒ Object



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

def locate_internal_refs1(refs, identifier, ident)
  file, = @files.targetfile_id(ident, read: true)
  t = locate_internal_refs1_prep(file)
  refs.each do |schema, ids|
    ids.keys.select { |id| t[id] }.each do |id|
      t[id].at("./ancestor-or-self::*[@type = '#{schema}']") and
        refs[schema][id] = identifier
    end
  end
end

#locate_internal_refs1_prep(file) ⇒ Object



168
169
170
171
172
173
174
175
176
# File 'lib/metanorma/collection/renderer/fileprocess.rb', line 168

def locate_internal_refs1_prep(file)
  xml = Nokogiri::XML(file, &:huge)
  r = xml.root["document_suffix"]
  xml.xpath("//*[@id]").each_with_object({}) do |i, x|
    /^semantic_/.match?(i.name) and next
    x[i["id"]] = i
    r and x[i["id"].sub(/_#{r}$/, "")] = i
  end
end

#new_hidden_ref(xmldoc) ⇒ Object



41
42
43
44
45
46
# File 'lib/metanorma/collection/renderer/utils.rb', line 41

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

#overall_docconv_converter(body) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/metanorma/collection/renderer/render_word.rb', line 99

def overall_docconv_converter(body)
  collection_conv = docconv
  collection_conv.options[:collection_doc] = body.map(&:to_xml).join
  overall_docconv_cover(collection_conv)

  def collection_conv.postprocess_cleanup(result)
    ret = to_xhtml(super)
    b = ret.at("//div[@id = '_collection_placeholder']")
    b.replace(@options[:collection_doc])
    from_xhtml(ret)
  end

  collection_conv
end

#overall_docconv_cover(collection_conv) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/metanorma/collection/renderer/render_word.rb', line 90

def overall_docconv_cover(collection_conv)
  p = Util::hash_key_detect(@directives, "collection-word-coverpage", nil)
  collection_conv.wordcoverpage =
    Util::rel_path_resolve(@dirname, p)
  p = Util::hash_key_detect(@directives, "collection-word-intropage", nil)
  collection_conv.wordintropage =
    Util::rel_path_resolve(@dirname, p)
end

#populate_internal_refs(refs) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/metanorma/collection/renderer/fileprocess.rb', line 133

def populate_internal_refs(refs)
  @files.keys.reject do |k|
    @files.get(k, :attachment) || @files.get(k, :sectionsplit)
  end.each do |ident|
    locate_internal_refs1(refs, ident,
                          @isodoc.docid_prefix("", ident.dup))
  end
  refs
end

#rxl(options) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/metanorma/collection/renderer/renderer.rb', line 105

def rxl(options)
  @bibdata or return
  options[:site_generate] and options[:format] << :xml
  options[:format].include?(:rxl) or return
  File.open(File.join(@outdir, "collection.rxl"), "w:UTF-8") do |f|
    f.write(@bibdata.to_xml)
  end
end

#set_displayorder_wrapping_doc(doc) ⇒ Object



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

def set_displayorder_wrapping_doc(doc)
  doc.xpath(ns("//preface/* | //sections/* | //annex"))
    .each_with_index do |x, i|
    x["displayorder"] = i + 1
  end
  doc
end

#strip_eref(eref) ⇒ Object



48
49
50
51
# File 'lib/metanorma/collection/renderer/utils.rb', line 48

def strip_eref(eref)
  eref.xpath(ns("./locality | ./localityStack")).each(&:remove)
  eref.replace(eref.children)
end

#strip_unresolved_repo_erefs(_document_id, bib_docid, erefs, bibitem) ⇒ Object

strip erefs if they are repository erefs, but do not point to a document within the current collection. This can happen if a collection consists of many documents, but not all are included in the current collection. Do not do this if this is a sectionsplit collection or a nested manifest. Return false if bibitem is not to be further processed



120
121
122
123
124
125
126
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 120

def strip_unresolved_repo_erefs(_document_id, bib_docid, erefs, bibitem)
  %r{^current-metanorma-collection/(?!Missing:)}.match?(bib_docid.text) and
    return true
  @nested and return false
  erefs[bibitem["id"]]&.each { |x| x.parent and strip_eref(x) }
  false
end

#suffix_anchor_indirect(prefix, suffix) ⇒ Object

encode both prefix and suffix to NCName



219
220
221
222
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 219

def suffix_anchor_indirect(prefix, suffix)
  k = "#{prefix}_#{suffix}"
  @ncnames[k] ||= Metanorma::Utils::to_ncname(k)
end

#supply_repo_ids(doc) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 70

def supply_repo_ids(doc)
  doc.xpath(ns(BIBITEM_NOT_REPO_XPATH)).each do |b|
    b.xpath(ns("./docidentifier")).each do |docid|
      id = @isodoc.docid_prefix(docid["type"], docid.children.to_xml)
      @files.get(id) or next
      @files.get(id, :indirect_key) and next # will resolve as indirect key
      docid.next = docid_xml(id)
    end
  end
end

#svg_datauri(docxml, docid) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/metanorma/collection/renderer/svg.rb', line 4

def svg_datauri(docxml, docid)
  rel = @files.get(docid, :rel_path)
  parent = @files.get(docid, :parentid) and
    rel = @files.get(parent, :rel_path)
  # if sectionsplit, use orig file dir
  dir = File.join(@dirname, File.dirname(rel))
  datauri_encode(docxml, dir)
end

#svg_unnest(docxml) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/metanorma/collection/renderer/svg.rb', line 41

def svg_unnest(docxml)
  docxml.xpath(ns("//svgmap//image[.//*[name() = 'image']]")).each do |i|
    s = i.elements.detect { |e| e.name == "svg" } and
      i.replace(s)
  end
  docxml
end

#svgmap_fmt_prefix_remove(docxml) ⇒ Object

undo Presentation XML update: Vectory takes eref not fmt-eref



33
34
35
36
37
38
39
# File 'lib/metanorma/collection/renderer/svg.rb', line 33

def svgmap_fmt_prefix_remove(docxml)
  docxml.xpath(ns("//svgmap/target")).each do |t|
    n = t.at(ns(".//fmt-link | .//fmt-xref | .//fmt-eref")) or next
    n.name = n.name.sub(/^fmt-/, "")
    t.children = n
  end
end

#svgmap_resolve(docxml, docid, presxml) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/metanorma/collection/renderer/svg.rb', line 13

def svgmap_resolve(docxml, docid, presxml)
  ids, docxml, isodoc, tag = svgmap_resolve_prep(docxml, docid, presxml)
  docxml.xpath(ns("//svgmap//#{tag}")).each do |e|
    svgmap_resolve_eref(e, isodoc, docxml, ids, presxml)
  end
  svgmap_fmt_prefix_remove(docxml)
  Vectory::SvgMapping.new(docxml, "").call
  docxml.xpath(ns("//svgmap")).each { |s| isodoc.svgmap_extract(s) }
end

#svgmap_resolve_eref(eref, isodoc, _docxml, ids, presxml) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/metanorma/collection/renderer/svg.rb', line 49

def svgmap_resolve_eref(eref, isodoc, _docxml, ids, presxml)
  href = isodoc.eref_target(eref) or return
  href = href[:link]
  href == "##{eref['bibitemid']}" ||
    (href =~ /^#/ && !ids[href.sub(/^#/, "")]) and return
  eref["target"] = href.strip
  eref.name = presxml ? "fmt-link" : "link"
  eref.elements&.remove
end

#svgmap_resolve_prep(docxml, docid, presxml) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/metanorma/collection/renderer/svg.rb', line 23

def svgmap_resolve_prep(docxml, docid, presxml)
  ids = @files.get(docid, :ids)
  docxml = svg_unnest(svg_datauri(docxml, docid))
  isodoc = IsoDoc::PresentationXMLConvert.new({})
  isodoc.bibitem_lookup(docxml)
  tag = presxml ? "fmt-eref" : "eref"
  [ids, docxml, isodoc, tag]
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



226
227
228
229
230
231
232
233
234
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 226

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
  a = @files.get(docid, :anchors).dig(type, ref) or return
  ins << "<locality type='anchor'><referenceFrom>#{a.sub(/^_/, '')}" \
    "</referenceFrom></locality>"
end

#update_anchors(bib, docid, erefs, erefs_no_anchor, erefs_anchors) ⇒ Object

bottleneck



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 193

def update_anchors(bib, docid, erefs, erefs_no_anchor, erefs_anchors)
  @files.get(docid) or error_anchor(erefs, docid)
  has_anchors, url, ncn_docid = update_anchors_prep(docid)
  erefs_no_anchor.each do |e|
    update_anchor_create_loc(bib, e, docid)
  end
  !url && has_anchors or return
  erefs_anchors.each do |e|
    update_anchors1(docid, ncn_docid, e)
  end
end

#update_anchors1(docid, ncn_docid, anchor) ⇒ Object



205
206
207
208
209
210
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 205

def update_anchors1(docid, ncn_docid, anchor)
  @concat_anchors[anchor.text] ||= "#{ncn_docid}_#{anchor.text}"
  if @files.get(docid).dig(:anchors_lookup, @concat_anchors[anchor.text])
    anchor.content = @concat_anchors[anchor.text]
  end
end

#update_anchors_prep(docid) ⇒ Object



212
213
214
215
216
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 212

def update_anchors_prep(docid)
  @concat_anchors = {}
  [@files.get(docid)&.key?(:anchors_lookup), @files.url?(docid),
   Metanorma::Utils::to_ncname(docid)]
end

#update_bibitem(bib, identifier) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/metanorma/collection/renderer/fileprocess.rb', line 178

def update_bibitem(bib, identifier)
  docid = get_bibitem_docid(bib, identifier) or return
  newbib = dup_bibitem(docid, bib)
  url = @files.url(docid, relative: true,
                          doc: !@files.get(docid, :attachment))
  dest = newbib.at("./docidentifier") || newbib.at(ns("./docidentifier"))
  dest or dest = newbib.elements[-1]
  dest.previous = "<uri type='citation'>#{url}</uri>"
  bib.replace(newbib)
end

#update_direct_refs_to_docs(xml, identifier, presxml) ⇒ 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.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 86

def update_direct_refs_to_docs(xml, identifier, presxml)
  erefs, erefs_no_anchor, anchors, erefs1 =
    update_direct_refs_to_docs_prep(xml, presxml)
  x = xml.xpath(ns("//bibitem")) - xml.xpath(ns("//bibdata//bibitem"))
  x.each do |b|
    docid = b.at(ns("./docidentifier[@type = 'repository']")) or next
    strip_unresolved_repo_erefs(identifier, docid, erefs1, b) or next
    update_bibitem(b, identifier)
    docid = docid_to_citeas(b) or next
    erefs[docid] and
      update_anchors(b, docid, erefs[docid], erefs_no_anchor[docid],
                     anchors[docid])
  end
end

#update_direct_refs_to_docs_prep(docxml, presxml) ⇒ Object

Hash(docid) of arrays



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 102

def update_direct_refs_to_docs_prep(docxml, presxml)
  erefs = Util::gather_citeases(docxml, presxml)
  no_anchor = erefs.keys.each_with_object({}) { |k, m| m[k] = [] }
  anchors = erefs.keys.each_with_object({}) { |k, m| m[k] = [] }
  erefs.each do |k, v|
    v.each do |e|
      if loc = e.at(".//#{ANCHOR_XPATH}") then anchors[k] << loc
      else no_anchor[k] << e end
    end
  end
  [erefs, no_anchor, anchors, Util::gather_bibitemids(docxml, presxml)]
end

#update_indirect_refs_prep(docxml, presxml) ⇒ Object



145
146
147
148
149
150
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 145

def update_indirect_refs_prep(docxml, presxml)
  @updated_anchors = {}
  @indirect_keys = {}
  [Util::gather_bibitems(docxml), Util::gather_bibitemids(docxml, presxml),
   docxml.root["document_suffix"], docxml.root["type"], {}]
end

#update_indirect_refs_to_docs(docxml, _docid, internal_refs, presxml) ⇒ Object

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 130

def update_indirect_refs_to_docs(docxml, _docid, internal_refs, presxml)
  bib, erefs, doc_suffix, doc_type, f = update_indirect_refs_prep(docxml,
                                                                  presxml)
  internal_refs.each do |schema, ids|
    add_suffix = doc_suffix && doc_type && doc_type != schema
    ids.each do |id, file|
      f[file] ||= { url: @files.url?(file),
                    parentid: @files.get(file) && @files.get(file,
                                                             :parentid) }
      k = indirect_ref_key(schema, id, doc_suffix, add_suffix)
      update_indirect_refs_to_docs1(f[file], k, file, bib, erefs)
    end
  end
end

#update_indirect_refs_to_docs1(filec, key, file, bibitems, erefs) ⇒ Object



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

def update_indirect_refs_to_docs1(filec, key, file, bibitems, erefs)
  erefs[key]&.each do |e|
    e["citeas"] = file
    update_indirect_refs_to_docs_anchor(e, file, filec[:url],
                                        filec[:parentid])
  end
  update_indirect_refs_to_docs_docid(bibitems[key], file)
end

#update_indirect_refs_to_docs_anchor(eref, file, url, parentid) ⇒ Object



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

def update_indirect_refs_to_docs_anchor(eref, file, url, parentid)
  a = eref.at(".//#{ANCHOR_XPATH}") or return
  parentid and file = "#{parentid}_#{file}"
  existing = a.text
  anchor = if url then existing
           else
             @indirect_keys[existing] ||= {}
             @indirect_keys[existing][file] ||= Metanorma::Utils::to_ncname("#{existing}_#{file}")
           end
  @updated_anchors[existing] or a.children = anchor
  @updated_anchors[anchor] = true
end

#update_indirect_refs_to_docs_docid(bib, file) ⇒ Object



185
186
187
188
189
190
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 185

def update_indirect_refs_to_docs_docid(bib, file)
  docid = bib&.at(ns("./docidentifier[@type = 'repository']")) or return
  docid.children = "current-metanorma-collection/#{file}"
  docid.previous =
    "<docidentifier type='metanorma-collection'>#{file}</docidentifier>"
end

#update_sectionsplit_eref_to_doc(eref, internal_refs, doclist, opts) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 57

def update_sectionsplit_eref_to_doc(eref, internal_refs, doclist, opts)
  a = eref.at("./xmlns:localityStack/#{ANCHOR_XPATH}") or return
  doc = internal_refs[opts[:key]]["#{a.text}_#{opts[:target_suffix]}"]
  bibitemid = Metanorma::Utils::to_ncname("#{doc}_#{opts[:source_suffix]}")
  eref["bibitemid"] = bibitemid
  doclist[bibitemid] ||= doc
  doclist
end

#update_sectionsplit_refs_to_docs(docxml, internal_refs, presxml) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 41

def update_sectionsplit_refs_to_docs(docxml, internal_refs, presxml)
  Util::gather_citeases(docxml, presxml).each do |k, v|
    (@files.get(k) && @files.get(k, :sectionsplit)) or next
    opts = { key: @files.get(k, :indirect_key),
             source_suffix: docxml.root["document_suffix"],
             target_suffix: @files.get(k, :document_suffix) }
    refs = v.each_with_object({}) do |eref, m|
      update_sectionsplit_eref_to_doc(eref, internal_refs, m, opts)
    end
    add_hidden_bibliography(docxml, refs)
  end
end

#update_xrefs(file, docid, internal_refs) ⇒ String

Resolves references to other files in the collection. Three routines:

  1. Eref to a document that has been split into multiple documents

(sectionsplit) are resolved to direct eref to the split document

  1. Indirect erefs to a file anchor in an unknown file in the collection

(bibitem[@type = ‘internal’] ) are resolved to direct eref to the containing document

  1. Direct erefs to other files in collection

(repo(current-metanorma-collection/x) are resolved to hyperlinks

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 17

def update_xrefs(file, docid, internal_refs)
  xml, sso = update_xrefs_prep(file, docid)
  @nested || sso or
    Metanorma::Collection::XrefProcess::xref_process(xml, xml, nil, docid,
                                                     @isodoc, sso)
  @ncnames = {}
  @nested or update_indirect_refs_to_docs(xml, docid, internal_refs, sso)
  @files.add_document_suffix(docid, xml)
  @nested or update_sectionsplit_refs_to_docs(xml, internal_refs, sso)
  update_direct_refs_to_docs(xml, docid, sso)
  ::Metanorma::Collection::Util::hide_refs(xml)
  sso and eref2link(xml, sso)
  @nested or svgmap_resolve(xml, docid, sso)
  xml.to_xml
end

#update_xrefs_prep(file, docid) ⇒ Object

sso files are Presentation XML; otherwise, Semantic XML



34
35
36
37
38
39
# File 'lib/metanorma/collection/renderer/fileparse.rb', line 34

def update_xrefs_prep(file, docid)
  docxml = file.is_a?(String) ? Nokogiri::XML(file, &:huge) : file
  supply_repo_ids(docxml)
  sso = @files.get(docid, :sectionsplit_output)
  [docxml, sso]
end

#wrapping_doc(doc, xml) ⇒ Object



21
22
23
24
25
26
# File 'lib/metanorma/collection/renderer/render_word.rb', line 21

def wrapping_doc(doc, xml)
  doc.at(ns("//bibdata")).replace(xml.at(ns("//bibdata")).to_xml)
  sections = wrapping_doc_body(doc)
  wrapping_doc_intro_outro(xml, sections)
  set_displayorder_wrapping_doc(doc)
end

#wrapping_doc_body(doc) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/metanorma/collection/renderer/render_word.rb', line 35

def wrapping_doc_body(doc)
  doc.xpath(ns("//annex | //preface | //bibliography")).each(&:remove)
  s = doc.at(ns("//sections")) ||
    doc.root.elements[-1].after("<sections/>").next
  repl = <<~BODY
    <sections><clause id='_collection_placeholder'><p>PLACEHOLDER</p></clause></sections>
  BODY
  s.replace(repl)
  doc.at(ns("//sections"))
end

#wrapping_doc_intro_outro(xml, sections) ⇒ Object



28
29
30
31
32
33
# File 'lib/metanorma/collection/renderer/render_word.rb', line 28

def wrapping_doc_intro_outro(xml, sections)
  p = xml.at(ns("//prefatory-content")) and
    sections.previous = "<preface>#{p.children.to_xml}</preface>"
  p = xml.at(ns("//final-content")) and
    sections.next = "<annex>#{p.children.to_xml}</annex>"
end