module Metanorma # XML collection renderer class CollectionRenderer def update_bibitem(bib, identifier) docid = get_bibitem_docid(bib, identifier) or return newbib = dup_bibitem(docid, bib) _file, url = @files .targetfile_id(docid, relative: true, read: false, doc: !@files.get(docid, :attachment)) dest = newbib.at("./docidentifier") || newbib.at(ns("./docidentifier")) dest.previous = "#{url}" bib.replace(newbib) end # 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 # 2. Indirect erefs to a file anchor in an unknwon file in the collection # (bibitem[@type = 'internal'] ) are resolved to direct eref to the # containing document # 3. Direct erefs to other files in collection # (repo(current-metanorma-collection/x) are resolved to hyperlinks # @param file [String] XML content # @param identifier [String] docid # @param internal_refs [Hash{String=>Hash{String=>String}] schema name to # anchor to filename # @return [String] XML content def update_xrefs(file, docid, internal_refs) docxml = file.is_a?(String) ? Nokogiri::XML(file, &:huge) : file supply_repo_ids(docxml) @nested or update_indirect_refs_to_docs(docxml, docid, internal_refs) @files.add_document_suffix(docid, docxml) @nested or update_sectionsplit_refs_to_docs(docxml, internal_refs) update_direct_refs_to_docs(docxml, docid) hide_refs(docxml) @files.get(docid, :sectionsplit_output) and eref2link(docxml) svgmap_resolve(datauri_encode(docxml), @files.get(docid, :ids)) docxml.to_xml end def update_sectionsplit_refs_to_docs(docxml, internal_refs) Util::gather_citeases(docxml).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 def update_sectionsplit_eref_to_doc(eref, internal_refs, doclist, opts) a = eref.at(ns("./localityStack/locality[@type = 'anchor']/" \ "referenceFrom")) 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 def new_hidden_ref(xmldoc) ins = xmldoc.at(ns("bibliography")) or xmldoc.root << "" and ins = xmldoc.at(ns("bibliography")) ins.at(ns("./referenced[@hidden = 'true']")) or ins.add_child("