Class: Metanorma::Compile
- Inherits:
-
Object
- Object
- Metanorma::Compile
- Includes:
- CompileOptions, Flavor, Validator, Writeable
- Defined in:
- lib/metanorma/compile/flavor.rb,
lib/metanorma/compile/render.rb,
lib/metanorma/compile/compile.rb,
lib/metanorma/compile/extract.rb,
lib/metanorma/compile/validator.rb,
lib/metanorma/compile/writeable.rb,
lib/metanorma/compile/relaton_drop.rb,
lib/metanorma/compile/compile_options.rb,
lib/metanorma/compile/output_filename.rb,
lib/metanorma/compile/output_filename_config.rb
Defined Under Namespace
Modules: CompileOptions, Extract, Flavor, Validator, Writeable Classes: OutputFilename, OutputFilenameConfig, RelatonDrop
Constant Summary collapse
- DEFAULT_NUM_WORKERS =
3
Instance Attribute Summary collapse
- #errors ⇒ Array<String> readonly
-
#processor ⇒ Object
readonly
Returns the value of attribute processor.
Instance Method Summary collapse
- #clean_exit(options) ⇒ Object
-
#compile(filename, options = {}) ⇒ Object
Main compile method that orchestrates the document conversion process.
-
#export_relaton_from_bibdata(bibdata, options) ⇒ Object
Export given bibliographic data to Relaton XML on disk.
-
#extract_information(semantic_xml, bibdata, options) ⇒ Object
Step 4: Extract information from Semantic XML.
-
#extract_relaton_metadata(xml) ⇒ Nokogiri::XML::Element
The bibliographic data element.
-
#gather_and_install_fonts(source_file, options, extensions) ⇒ Object
isodoc is Raw Metanorma XML.
-
#generate_outputs(source_file, semantic_xml, bibdata, extensions, output_paths, options) ⇒ Object
Step 5: Generate output formats from Semantic XML.
-
#generate_outputs_parallel(source_file, semantic_xml, bibdata, extensions, output_paths, options) ⇒ Object
Generate multiple output formats with parallel processing.
-
#generate_presentation_xml(source_file, xml, bibdata, output_paths, opt) ⇒ Object
Generate presentation XML from semantic XML.
-
#generate_semantic_xml(filename, options) ⇒ Array?
Step 1: Generate Semantic XML from input file.
-
#initialize ⇒ Compile
constructor
A new instance of Compile.
-
#prepare_output_paths(filename, bibdata, options) ⇒ Hash
Step 2: Prepare output paths for generated files Use default filename template if empty string is provided.
-
#process_ext(ext, source_file, semantic_xml, bibdata, output_paths, options) ⇒ Object
Process a single extension (output format).
-
#process_ext_special(ext, sem_xml, bibdata, output_paths, options, isodoc_options) ⇒ Object
Process special extensions with custom handling.
- #process_extensions_in_order(source_file, semantic_xml, bibdata, extensions, output_paths, options) ⇒ Object
-
#process_from_semantic_xml(ext, output_paths, sem_xml, isodoc_options) ⇒ Object
Process format directly from semantic XML.
- #process_input_adoc(filename, options) ⇒ Object
- #process_input_adoc_hdr(file, options) ⇒ Object
- #process_input_adoc_includes(file, filename) ⇒ Object
- #process_input_adoc_overrides(attrs, options) ⇒ Object
- #process_input_xml(filename, _options) ⇒ Object
- #process_options!(filename, options) ⇒ Object
-
#process_output_from_presentation_xml(ext, output_paths, options, isodoc_options) ⇒ Object
Generate output format from presentation XML.
-
#process_via_presentation_xml(ext, output_paths, options, isodoc_options) ⇒ Object
Process format that requires presentation XML.
- #read_file(filename) ⇒ Object
-
#sectionsplit_convert(input_filename, file, output_filename = nil, opts = {}) ⇒ Object
assume we pass in Presentation XML, but we want to recover Semantic XML.
- #wrap_html(options, file_extension, outfilename) ⇒ Object
Methods included from Writeable
Methods included from Flavor
#load_flavor, #stdtype2flavor_gem
Methods included from CompileOptions
#extract_dir_options, #extract_extensions, #extract_flavor_options, #extract_options, #extract_xml_options, #font_install, #get_extensions, #options_in_file, #require_libraries
Methods included from Validator
#validate_format!, #validate_options!, #validate_type!
Constructor Details
#initialize ⇒ Compile
Returns a new instance of Compile.
37 38 39 40 41 42 43 |
# File 'lib/metanorma/compile/compile.rb', line 37 def initialize @registry = Metanorma::Registry.instance @errors = [] @isodoc = IsoDoc::Convert.new({}) @fontist_installed = false @log = Metanorma::Utils::Log.new end |
Instance Attribute Details
#errors ⇒ Array<String> (readonly)
34 35 36 |
# File 'lib/metanorma/compile/compile.rb', line 34 def errors @errors end |
#processor ⇒ Object (readonly)
Returns the value of attribute processor.
35 36 37 |
# File 'lib/metanorma/compile/compile.rb', line 35 def processor @processor end |
Instance Method Details
#clean_exit(options) ⇒ Object
91 92 93 94 |
# File 'lib/metanorma/compile/compile.rb', line 91 def clean_exit() [:novalid] and return @log.write end |
#compile(filename, options = {}) ⇒ Object
Main compile method that orchestrates the document conversion process
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/metanorma/compile/compile.rb', line 48 def compile(filename, = {}) (filename, ) @processor = @registry.find_processor([:type].to_sym) # Step 1: Generate Semantic XML semantic_result = generate_semantic_xml(filename, ) return nil unless semantic_result source_file, semantic_xml = semantic_result # Step 2: Prepare output paths xml = Nokogiri::XML(semantic_xml, &:huge) bibdata = (xml) output_paths = prepare_output_paths(filename, bibdata, ) # Step 3: Determine which output formats to generate extensions = get_extensions() return nil unless extensions # Step 4: Extract information from Semantic XML if requested extract_information(semantic_xml, bibdata, ) # Step 5: Generate output formats from Semantic XML generate_outputs( source_file, semantic_xml, bibdata, extensions, output_paths, , ) ensure clean_exit() end |
#export_relaton_from_bibdata(bibdata, options) ⇒ Object
Export given bibliographic data to Relaton XML on disk
37 38 39 40 |
# File 'lib/metanorma/compile/render.rb', line 37 def export_relaton_from_bibdata(bibdata, ) [:relaton] or return export_output([:relaton], bibdata.to_xml) end |
#extract_information(semantic_xml, bibdata, options) ⇒ Object
Step 4: Extract information from Semantic XML
141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/metanorma/compile/compile.rb', line 141 def extract_information(semantic_xml, bibdata, ) # Extract Relaton bibliographic data export_relaton_from_bibdata(bibdata, ) if [:relaton] # Extract other components (sourcecode, images, requirements) if [:extract] Extract.extract( semantic_xml, [:extract], [:extract_type], ) end end |
#extract_relaton_metadata(xml) ⇒ Nokogiri::XML::Element
Returns the bibliographic data element.
44 45 46 |
# File 'lib/metanorma/compile/render.rb', line 44 def (xml) xml.at("//bibdata") || xml.at("//xmlns:bibdata") end |
#gather_and_install_fonts(source_file, options, extensions) ⇒ Object
isodoc is Raw Metanorma XML
58 59 60 61 62 63 |
# File 'lib/metanorma/compile/render.rb', line 58 def gather_and_install_fonts(source_file, , extensions) Util.sort_extensions_execution(extensions).each do |ext| = (source_file, , ext) font_install(.merge()) end end |
#generate_outputs(source_file, semantic_xml, bibdata, extensions, output_paths, options) ⇒ Object
Step 5: Generate output formats from Semantic XML
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/metanorma/compile/compile.rb', line 162 def generate_outputs( source_file, semantic_xml, bibdata, extensions, output_paths, ) if extensions == i(presentation) # Just generate presentation XML generate_presentation_xml( source_file, semantic_xml, bibdata, output_paths, ) else # Generate multiple output formats with parallel processing generate_outputs_parallel( source_file, semantic_xml, bibdata, extensions, output_paths, ) end end |
#generate_outputs_parallel(source_file, semantic_xml, bibdata, extensions, output_paths, options) ⇒ Object
Generate multiple output formats with parallel processing
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/metanorma/compile/render.rb', line 9 def generate_outputs_parallel( source_file, semantic_xml, bibdata, extensions, output_paths, ) @queue = ::Metanorma::Util::WorkersPool.new( ENV["METANORMA_PARALLEL"]&.to_i || DEFAULT_NUM_WORKERS, ) # Install required fonts for all extensions gather_and_install_fonts(source_file, .dup, extensions) # Process each extension in order process_extensions_in_order( source_file, semantic_xml, bibdata, extensions, output_paths, ) @queue.shutdown end |
#generate_presentation_xml(source_file, xml, bibdata, output_paths, opt) ⇒ Object
Generate presentation XML from semantic XML
4 5 6 |
# File 'lib/metanorma/compile/render.rb', line 4 def generate_presentation_xml(source_file, xml, bibdata, output_paths, opt) process_ext(:presentation, source_file, xml, bibdata, output_paths, opt) end |
#generate_semantic_xml(filename, options) ⇒ Array?
Step 1: Generate Semantic XML from input file
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/metanorma/compile/compile.rb', line 100 def generate_semantic_xml(filename, ) case extname = File.extname(filename) when ".adoc" then process_input_adoc(filename, ) when ".xml" then process_input_xml(filename, ) else Util.log("[metanorma] Error: file extension #{extname} " \ "is not supported.", :error) nil end end |
#prepare_output_paths(filename, bibdata, options) ⇒ Hash
Step 2: Prepare output paths for generated files Use default filename template if empty string is provided.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/metanorma/compile/compile.rb', line 118 def prepare_output_paths(filename, bibdata, ) basename = if [:filename_template].nil? filename.sub(/\.[^.]+$/, "") else drop = RelatonDrop.new(bibdata) config = OutputFilenameConfig.new([:filename_template]) config.generate_filename(drop) end @output_filename = OutputFilename.new( basename, [:output_dir], @processor, ) { xml: @output_filename.semantic_xml, orig_filename: filename, presentationxml: @output_filename.presentation_xml, } end |
#process_ext(ext, source_file, semantic_xml, bibdata, output_paths, options) ⇒ Object
Process a single extension (output format)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/metanorma/compile/render.rb', line 66 def process_ext(ext, source_file, semantic_xml, bibdata, output_paths, ) output_paths[:ext] = @processor.output_formats[ext] output_paths[:out] = @output_filename.for_format(ext) || output_paths[:xml].sub(/\.[^.]+$/, ".#{output_paths[:ext]}") = (source_file, , ext) # Handle special cases first return true if process_ext_special( ext, semantic_xml, bibdata, output_paths, , ) # Otherwise, determine if it uses presentation XML if @processor.use_presentation_xml(ext) # Format requires presentation XML first, then convert to final format process_via_presentation_xml(ext, output_paths, , ) else # Format can be generated directly from semantic XML process_from_semantic_xml( ext, output_paths, semantic_xml, ) end end |
#process_ext_special(ext, sem_xml, bibdata, output_paths, options, isodoc_options) ⇒ Object
Process special extensions with custom handling
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/metanorma/compile/render.rb', line 91 def process_ext_special( ext, sem_xml, bibdata, output_paths, , ) if ext == :rxl # Special case: Relaton export export_relaton_from_bibdata( bibdata, .merge(relaton: output_paths[:out]), ) true elsif ext == :presentation && [:passthrough_presentation_xml] # Special case: Pass through presentation XML f = if File.exist?(output_paths[:orig_filename]) output_paths[:orig_filename] else output_paths[:xml] end FileUtils.cp f, output_paths[:presentationxml] true elsif ext == :html && [:sectionsplit] # Special case: Split HTML into sections sectionsplit_convert( output_paths[:xml], sem_xml, output_paths[:out], ) true else false end end |
#process_extensions_in_order(source_file, semantic_xml, bibdata, extensions, output_paths, options) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/metanorma/compile/render.rb', line 24 def process_extensions_in_order( source_file, semantic_xml, bibdata, extensions, output_paths, ) Util.sort_extensions_execution(extensions).each do |ext| process_ext( ext, source_file, semantic_xml, bibdata, output_paths, ) or break end end |
#process_from_semantic_xml(ext, output_paths, sem_xml, isodoc_options) ⇒ Object
Process format directly from semantic XML
141 142 143 144 145 146 147 148 149 |
# File 'lib/metanorma/compile/render.rb', line 141 def process_from_semantic_xml(ext, output_paths, sem_xml, ) @processor.output(sem_xml, output_paths[:xml], output_paths[:out], ext, ) true # Return as Thread equivalent rescue StandardError => e strict = ext == :presentation || [:strict] == "true" isodoc_error_process(e, strict, true) ext != :presentation end |
#process_input_adoc(filename, options) ⇒ Object
201 202 203 204 205 206 207 |
# File 'lib/metanorma/compile/compile.rb', line 201 def process_input_adoc(filename, ) Util.log("[metanorma] Processing: AsciiDoc input.", :info) file = read_file(filename) file = process_input_adoc_hdr(file, ) file = process_input_adoc_includes(file, filename) [file, @processor.input_to_isodoc(file, filename, )] end |
#process_input_adoc_hdr(file, options) ⇒ Object
178 179 180 181 182 183 184 |
# File 'lib/metanorma/compile/compile.rb', line 178 def process_input_adoc_hdr(file, ) hdr, rest = Metanorma::Input::Asciidoc.new.header(file) attrs = hdr.split("\n") [:asciimath] and attrs << ":mn-keep-asciimath:" process_input_adoc_overrides(attrs, ) "#{attrs.join("\n")}\n\n#{rest}" end |
#process_input_adoc_includes(file, filename) ⇒ Object
193 194 195 196 197 198 199 |
# File 'lib/metanorma/compile/compile.rb', line 193 def process_input_adoc_includes(file, filename) dir = File.dirname(filename) dir != "." and file = file.gsub(/^include::/, "include::#{dir}/") .gsub(/^embed::/, "embed::#{dir}/") file end |
#process_input_adoc_overrides(attrs, options) ⇒ Object
186 187 188 189 190 191 |
# File 'lib/metanorma/compile/compile.rb', line 186 def process_input_adoc_overrides(attrs, ) Metanorma::TasteRegister.instance.available_tastes.include?([:supplied_type]) or return c = Metanorma::TasteRegister.get([:supplied_type]) c.process_input_adoc_overrides(attrs, ) end |
#process_input_xml(filename, _options) ⇒ Object
209 210 211 212 213 214 |
# File 'lib/metanorma/compile/compile.rb', line 209 def process_input_xml(filename, ) Util.log("[metanorma] Processing: Metanorma XML input.", :info) # TODO NN: this is a hack -- we should provide/bridge the # document attributes in Metanorma XML ["", read_file(filename)] end |
#process_options!(filename, options) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/metanorma/compile/compile.rb', line 83 def (filename, ) require_libraries() = (filename, ) () @log.save_to(filename, [:output_dir]) [:log] = @log end |
#process_output_from_presentation_xml(ext, output_paths, options, isodoc_options) ⇒ Object
Generate output format from presentation XML
130 131 132 133 134 135 136 137 138 |
# File 'lib/metanorma/compile/render.rb', line 130 def process_output_from_presentation_xml(ext, output_paths, , ) @processor.output(nil, output_paths[:presentationxml], output_paths[:out], ext, ) wrap_html(, output_paths[:ext], output_paths[:out]) rescue StandardError => e strict = ext == :presentation || [:strict] == true isodoc_error_process(e, strict, false) end |
#process_via_presentation_xml(ext, output_paths, options, isodoc_options) ⇒ Object
Process format that requires presentation XML
122 123 124 125 126 127 |
# File 'lib/metanorma/compile/render.rb', line 122 def process_via_presentation_xml(ext, output_paths, , ) @queue.schedule(ext, output_paths.dup, .dup, .dup) do |a, b, c, d| process_output_from_presentation_xml(a, b, c, d) end end |
#read_file(filename) ⇒ Object
216 217 218 |
# File 'lib/metanorma/compile/compile.rb', line 216 def read_file(filename) File.read(filename, encoding: "utf-8").gsub("\r\n", "\n") end |
#sectionsplit_convert(input_filename, file, output_filename = nil, opts = {}) ⇒ Object
assume we pass in Presentation XML, but we want to recover Semantic XML
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/metanorma/compile/render.rb', line 152 def sectionsplit_convert(input_filename, file, output_filename = nil, opts = {}) @isodoc ||= IsoDoc::PresentationXMLConvert.new({}) input_filename += ".xml" unless input_filename.match?(/\.xml$/) File.exist?(input_filename) or export_output(input_filename, file) presxml = File.read(input_filename, encoding: "utf-8") _xml, filename, dir = @isodoc.convert_init(presxml, input_filename, false) ::Metanorma::Collection::Sectionsplit.new( input: input_filename, isodoc: @isodoc, xml: presxml, base: File.basename(output_filename || filename), output: output_filename || filename, dir: dir, compile_opts: opts ).build_collection end |
#wrap_html(options, file_extension, outfilename) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/metanorma/compile/render.rb', line 48 def wrap_html(, file_extension, outfilename) if [:wrapper] && /html$/.match(file_extension) outfilename = outfilename.sub(/\.html$/, "") FileUtils.mkdir_p outfilename FileUtils.mv "#{outfilename}.html", outfilename FileUtils.mv "#{outfilename}_images", outfilename, force: true end end |