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

- old
+ new

@@ -4,10 +4,11 @@ require "yaml" require "fontist" require "fontist/manifest/install" require_relative "compile_validate" require_relative "fontist_utils" +require_relative "util" module Metanorma class Compile # @return [Array<String>] attr_reader :errors, :processor @@ -33,11 +34,11 @@ def require_libraries(options) options&.dig(:require)&.each { |r| require r } end def xml_options_extract(file) - xml = Nokogiri::XML(file) + xml = Nokogiri::XML(file) { |config| config.huge } if xml.root @registry.root_tags.each do |k, v| return { type: k } if v == xml.root.name end end @@ -109,11 +110,11 @@ end def relaton_export(isodoc, options) return unless options[:relaton] - xml = Nokogiri::XML(isodoc) + xml = Nokogiri::XML(isodoc) { |config| config.huge } bibdata = xml.at("//bibdata") || xml.at("//xmlns:bibdata") # docid = bibdata&.at("./xmlns:docidentifier")&.text || options[:filename] # outname = docid.sub(/^\s+/, "").sub(/\s+$/, "").gsub(/\s+/, "-") + ".xml" File.open(options[:relaton], "w:UTF-8") { |f| f.write bibdata.to_xml } end @@ -133,11 +134,11 @@ if extract_types.nil? || extract_types.empty? extract_types = [:sourcecode, :image, :requirement] end FileUtils.rm_rf dirname FileUtils.mkdir_p dirname - xml = Nokogiri::XML(isodoc) + xml = Nokogiri::XML(isodoc) { |config| config.huge } sourcecode_export(xml, dirname) if extract_types.include? :sourcecode image_export(xml, dirname) if extract_types.include? :image requirement_export(xml, dirname) if extract_types.include? :requirement end @@ -179,21 +180,10 @@ f.write s end end end - # dependency ordering - def sort_extensions_execution(ext) - case ext - when :xml then 0 - when :rxl then 1 - when :presentation then 2 - else - 99 - end - end - def wrap_html(options, file_extension, outfilename) if options[:wrapper] && /html$/.match(file_extension) outfilename = outfilename.sub(/\.html$/, "") FileUtils.mkdir_p outfilename FileUtils.mv "#{outfilename}.html", outfilename @@ -204,24 +194,14 @@ # isodoc is Raw Metanorma XML def process_extensions(extensions, file, isodoc, options) f = change_output_dir options xml_name = f.sub(/\.[^.]+$/, ".xml") presentationxml_name = f.sub(/\.[^.]+$/, ".presentation.xml") - extensions.sort do |a, b| - sort_extensions_execution(a) <=> sort_extensions_execution(b) - end.each do |ext| - isodoc_options = @processor.extract_options(file) - isodoc_options[:datauriimage] = true if options[:datauriimage] - isodoc_options[:sourcefilename] = options[:filename] - isodoc_options[:bare] = options[:bare] + Util.sort_extensions_execution(extensions).each do |ext| file_extension = @processor.output_formats[ext] outfilename = f.sub(/\.[^.]+$/, ".#{file_extension}") - if ext == :pdf - font_locations = FontistUtils.fontist_font_locations(@processor, options) - font_locations and - isodoc_options[:mn2pdf] = { font_manifest_file: font_locations.path } - end + isodoc_options = get_isodoc_options(file, options, ext) if ext == :rxl options[:relaton] = outfilename relaton_export(isodoc, options) elsif options[:passthrough_presentation_xml] && ext == :presentation FileUtils.cp f, presentationxml_name @@ -229,22 +209,39 @@ begin @processor.use_presentation_xml(ext) ? @processor.output(nil, presentationxml_name, outfilename, ext, isodoc_options) : @processor.output(isodoc, xml_name, outfilename, ext, isodoc_options) rescue StandardError => e - if e.message.include? "Fatal:" - @errors << e.message - else - puts e.message - puts e.backtrace.join("\n") - end + isodoc_error_process(e) end end wrap_html(options, file_extension, outfilename) end end private + + def isodoc_error_process(err) + if err.message.include? "Fatal:" + @errors << err.message + else + puts err.message + puts err.backtrace.join("\n") + end + end + + def get_isodoc_options(file, options, ext) + isodoc_options = @processor.extract_options(file) + isodoc_options[:datauriimage] = true if options[:datauriimage] + isodoc_options[:sourcefilename] = options[:filename] + isodoc_options[:bare] = options[:bare] + isodoc_options[:sectionsplit] = options[:sectionsplit] + if ext == :pdf + floc = FontistUtils.fontist_font_locations(@processor, options) and + isodoc_options[:mn2pdf] = { font_manifest_file: floc.path } + end + isodoc_options + end # @param options [Hash] # @return [String] def change_output_dir(options) if options[:output_dir]