lib/metanorma/collection_fileprocess.rb in metanorma-1.3.5 vs lib/metanorma/collection_fileprocess.rb in metanorma-1.3.6

- old
+ new

@@ -25,14 +25,47 @@ xml = Nokogiri::XML(file) add_document_suffix(identifier, xml) files[identifier][:anchors] = read_anchors(xml) files[identifier][:bibdata] = xml.at(ns("//bibdata")) end + files[identifier][:bibitem] = files[identifier][:bibdata].dup + files[identifier][:bibitem].name = "bibitem" + files[identifier][:bibitem]["hidden"] = "true" + files[identifier][:bibitem]&.at("./*[local-name() = 'ext']")&.remove end - files + add_section_split(files) end + def add_section_split(files) + files.keys.each_with_object({}) do |k, m| + if files[k][:sectionsplit] == "true" && !files[k]["attachment"] + sectionsplit(files[k][:rel_path]).each_with_index do |f1, i| + m[k + f1[:title]] = + { parentid: k, presentationxml: true, type: "fileref", + rel_path: f1[:url], out_path: File.basename(f1[:url]), + anchors: read_anchors(Nokogiri::XML(File.read(f1[:url]))), + bibdata: files[k][:bibdata], ref: f1[:url] } + m[k + f1[:title]][:bare] = true unless i.zero? + end + m[k] = files[k] + else + m[k] = files[k] + end + end + end + + def sectionsplit(file) + Compile.new.compile( + file, { format: :asciidoc, extension_keys: [:presentation] } + .merge(@compile_options) + ) + r = file.sub(/\.xml$/, ".presentation.xml") + @isodoc.sectionsplit( + Nokogiri::XML(File.read(r)), File.basename(r), File.dirname(r) + ).sort_by { |f| f[:order] } + end + # rel_path is the source file address, determined relative to the YAML. # out_path is the destination file address, with any references outside # the working directory (../../...) truncated def file_entry(ref, identifier, _path) out = ref["attachment"] ? ref["fileref"] : File.basename(ref["fileref"]) @@ -42,10 +75,13 @@ out_path: out } else { type: "id", ref: ref["id"] } end ret[:attachment] = ref["attachment"] if ref["attachment"] + ret[:sectionsplit] = ref["sectionsplit"] if ref["sectionsplit"] + ret[:presentationxml] = ref["presentation-xml"] if ref["presentation-xml"] + ret[:bareafterfirst] = ref["bare-after-first"] if ref["bare-after-first"] ret end def add_suffix_to_attributes(doc, suffix, tag_name, attribute_name) doc.xpath(ns("//#{tag_name}[@#{attribute_name}]")).each do |elem| @@ -93,23 +129,51 @@ filename.sub!(/\.xml$/, ".html") if doc [file, filename] end # compile and output individual file in collection + # warn "metanorma compile -x html #{f.path}" def file_compile(file, filename, identifier) - # warn "metanorma compile -x html #{f.path}" - Array(@directives).include?("presentation-xml") and - @compile_options.merge!(passthrough_presentation_xml: true) c = Compile.new - c.compile file.path, { format: :asciidoc, - extension_keys: @format }.merge(@compile_options) + c.compile file.path, { format: :asciidoc, extension_keys: @format } + .merge(compile_options(identifier)) @files[identifier][:outputs] = {} + file_compile_formats(file, filename, identifier, c) + end + + def compile_options(identifier) + ret = @compile_options.dup + Array(@directives).include?("presentation-xml") || + @files[identifier][:presentationxml] and + ret.merge!(passthrough_presentation_xml: true) + @files[identifier][:sectionsplit] == "true" and + ret.merge!(sectionsplit: "true") + @files[identifier][:bare] == true and + ret.merge!(bare: true) + ret + end + + def file_compile_formats(file, filename, identifier, compile) @format.each do |e| - ext = c.processor.output_formats[e] - fn = File.basename(filename).sub(/(?<=\.)[^\.]+$/, ext.to_s) - FileUtils.cp file.path.sub(/\.xml$/, ".#{ext}"), File.join(@outdir, fn) - @files[identifier][:outputs][e] = File.join(@outdir, fn) + ext = compile.processor.output_formats[e] + fn = File.basename(filename).sub(/(?<=\.)[^.]+$/, ext.to_s) + if /html$/.match?(ext) && @files[identifier][:sectionsplit] + # file_sectionsplit_copy(file, fn, identifier, ext, e) + else + FileUtils.cp file.path.sub(/\.xml$/, ".#{ext}"), + File.join(@outdir, fn) + @files[identifier][:outputs][e] = File.join(@outdir, fn) + end end + end + + def file_sectionsplit_copy(file, base, identifier, ext, format) + dir = file.path.sub(/\.xml$/, ".#{ext}_collection") + files = Dir.glob("#{dir}/*.#{ext}") + FileUtils.cp files, @outdir + cover = File.join(@outdir, base.sub(/\.html$/, ".index.html")) + FileUtils.cp File.join(dir, "index.html"), cover + @files[identifier][:outputs][format] = cover end def copy_file_to_dest(fileref) dest = File.join(@outdir, fileref[:out_path]) FileUtils.mkdir_p(File.dirname(dest))