lib/metanorma/collection_fileprocess.rb in metanorma-1.3.7 vs lib/metanorma/collection_fileprocess.rb in metanorma-1.3.8

- old
+ new

@@ -36,32 +36,62 @@ 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? + s, manifest = sectionsplit(files[k][:ref]) + s.each_with_index do |f1, i| + add_section_split_instance(f1, m, k, i, files) end + m["#{k}:index.html"] = add_section_split_cover(files, manifest, k) end m[k] = files[k] end end + def add_section_split_cover(files, manifest, ident) + cover = section_split_cover(manifest, ident) + files[ident][:out_path] = cover + { attachment: true, index: false, out_path: cover, + ref: File.join(File.dirname(manifest.file), cover) } + end + + def section_split_cover(col, ident) + dir = File.dirname(col.file) + @compile.collection_setup(nil, dir) + CollectionRenderer.new(col, dir, + output_folder: "#{ident}_collection", + format: %i(html), + coverpage: File.join(dir, "cover.html")).coverpage + FileUtils.mv "#{ident}_collection/index.html", + File.join(dir, "#{ident}_index.html") + FileUtils.rm_rf "#{ident}_collection" + "#{ident}_index.html" + end + + def add_section_split_instance(file, manifest, key, idx, files) + dir = File.dirname(files[key][:ref]) + presfile = File.join(dir, File.basename(file[:url])) + manifest["#{key} #{file[:title]}"] = + { parentid: key, presentationxml: true, type: "fileref", + rel_path: file[:url], out_path: File.basename(file[:url]), + anchors: read_anchors(Nokogiri::XML(File.read(presfile))), + bibdata: files[key][:bibdata], ref: presfile } + manifest["#{key} #{file[:title]}"][:bare] = true unless idx.zero? + end + def sectionsplit(file) - Compile.new.compile( + @compile.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] } + xml = Nokogiri::XML(File.read(r)) + s = @compile.sectionsplit(xml, File.basename(r), File.dirname(r)) + .sort_by { |f| f[:order] } + [s, @compile.collection_manifest(File.basename(r), s, xml, nil, + File.dirname(r))] 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 @@ -72,12 +102,13 @@ rel_path: ref["fileref"], out_path: out } else { type: "id", ref: ref["id"] } end - ret[:attachment] = ref["attachment"] if ref["attachment"] - ret[:sectionsplit] = ref["sectionsplit"] if ref["sectionsplit"] + %i(attachment sectionsplit index).each do |s| + ret[s] = ref[s.to_s] if ref[s.to_s] + end ret[:presentationxml] = ref["presentation-xml"] if ref["presentation-xml"] ret[:bareafterfirst] = ref["bare-after-first"] if ref["bare-after-first"] ret end @@ -129,15 +160,16 @@ end # compile and output individual file in collection # warn "metanorma compile -x html #{f.path}" def file_compile(file, filename, identifier) - c = Compile.new - c.compile file.path, { format: :asciidoc, extension_keys: @format } + return if @files[identifier][:sectionsplit] == "true" + + @compile.compile file.path, { format: :asciidoc, extension_keys: @format } .merge(compile_options(identifier)) @files[identifier][:outputs] = {} - file_compile_formats(file, filename, identifier, c) + file_compile_formats(file, filename, identifier) end def compile_options(identifier) ret = @compile_options.dup Array(@directives).include?("presentation-xml") || @@ -148,13 +180,13 @@ @files[identifier][:bare] == true and ret.merge!(bare: true) ret end - def file_compile_formats(file, filename, identifier, compile) + def file_compile_formats(file, filename, identifier) @format.each do |e| - ext = compile.processor.output_formats[e] + 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}"), @@ -180,16 +212,18 @@ end # process each file in the collection # files are held in memory, and altered as postprocessing 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| i.positive? && Array(@directives).include?("bare-after-first") and @compile_options.merge!(bare: true) if x[:attachment] then copy_file_to_dest(x) else file, filename = targetfile(x, read: true) + warn "\n\n\n\n\nProcess #{filename}: #{DateTime.now.strftime('%H:%M:%S')}" 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)