lib/metanorma/collection_fileprocess.rb in metanorma-1.3.1 vs lib/metanorma/collection_fileprocess.rb in metanorma-1.3.2

- old
+ new

@@ -15,17 +15,20 @@ def read_files(path) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength files = {} @xml.xpath(ns("//docref")).each do |d| identifier = d.at(ns("./identifier")).text files[identifier] = file_entry(d, path) - next if files[identifier][:attachment] - - file, _filename = targetfile(files[identifier], true) - xml = Nokogiri::XML(file) - add_document_suffix(identifier, xml) - files[identifier][:anchors] = read_anchors(xml) - files[identifier][:bibdata] = xml.at(ns("//bibdata")) + if files[identifier][:attachment] + files[identifier][:bibdata] = Metanorma::Document + .attachment_bibitem(identifier).root + else + file, _filename = targetfile(files[identifier], read: true) + xml = Nokogiri::XML(file) + add_document_suffix(identifier, xml) + files[identifier][:anchors] = read_anchors(xml) + files[identifier][:bibdata] = xml.at(ns("//bibdata")) + end end files end def file_entry(docref, path) @@ -56,16 +59,24 @@ end end # return file contents + output filename for each file in the collection, # given a docref entry - # @param data [Hash] - # @param read [Boolean] + # @param data [Hash] docref entry + # @param read [Boolean] read the file in and return it + # @param doc [Boolean] I am a Metanorma document, + # so my URL should end with html or pdf or whatever + # @param relative [Boolean] Return path relative to YAML file, + # not relative to calling function # @return [Array<String, nil>] - def targetfile(data, read = false, doc = true) - if data[:type] == "fileref" then ref_file data[:ref], read, doc - else xml_file data[:id], read + def targetfile(data, options) + options = { read: false, doc: true, relative: false }.merge(options) + path = options[:relative] ? data[:rel_path] : data[:ref] + if data[:type] == "fileref" + ref_file path, options[:read], options[:doc] + else + xml_file data[:id], options[:read] end end # @param ref [String] # @param read [Boolean] @@ -92,11 +103,11 @@ @files[identifier][:outputs][e] = File.join(@outdir, fn) end end def copy_file_to_dest(fileref) - _file, filename = targetfile(fileref, true, false) + _file, filename = targetfile(fileref, read: true, doc: false) dest = File.join(@outdir, fileref[:rel_path]) FileUtils.mkdir_p(File.dirname(dest)) FileUtils.cp filename, dest end @@ -105,10 +116,10 @@ def files # rubocop:disable Metrics/AbcSize, Metrics/MethodLength internal_refs = locate_internal_refs @files.each do |identifier, x| if x[:attachment] then copy_file_to_dest(x) else - file, filename = targetfile(x, true) + file, filename = targetfile(x, read: true) file = update_xrefs(file, identifier, internal_refs) Tempfile.open(["collection", ".xml"], encoding: "utf-8") do |f| f.write(file) f.close file_compile(f, filename, identifier)