Class: Metanorma::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/processor/processor.rb

Overview

Abstract base class for Metanorma flavor processors. Each flavor gem (metanorma-iso, metanorma-itu, etc.) defines a subclass and registers it with Registry; the registry then dispatches input documents to the appropriate processor based on the document’s declared flavor / class.

Subclasses MUST set @short, @input_format, and (if Asciidoctor is the input parser) @asciidoctor_backend in their initialize, and MAY override #output_formats, #use_presentation_xml, #options_preprocess, and #output to customise their output pipeline.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessor

Returns a new instance of Processor.

Raises:

  • (RuntimeError)

    always — concrete flavors must override.



30
31
32
# File 'lib/metanorma/processor/processor.rb', line 30

def initialize
  raise "This is an abstract class!"
end

Instance Attribute Details

#asciidoctor_backendSymbol? (readonly)

Returns Asciidoctor backend symbol used when the processor’s input is Asciidoc (e.g. :iso, :standoc); nil for non-Asciidoc inputs.

Returns:

  • (Symbol, nil)

    Asciidoctor backend symbol used when the processor’s input is Asciidoc (e.g. :iso, :standoc); nil for non-Asciidoc inputs.



27
28
29
# File 'lib/metanorma/processor/processor.rb', line 27

def asciidoctor_backend
  @asciidoctor_backend
end

#input_formatSymbol (readonly)

Returns input parser identifier (typically :asciidoc).

Returns:

  • (Symbol)

    input parser identifier (typically :asciidoc).



22
23
24
# File 'lib/metanorma/processor/processor.rb', line 22

def input_format
  @input_format
end

#shortSymbol (readonly)

Returns short name registered with the Registry (e.g. :iso, :itu).

Returns:

  • (Symbol)

    short name registered with the Registry (e.g. :iso, :itu).



19
20
21
# File 'lib/metanorma/processor/processor.rb', line 19

def short
  @short
end

Instance Method Details

#extract_metanorma_options(file) ⇒ Hash

Read metanorma-specific options from the input file’s header via Input::Asciidoc#extract_metanorma_options.

Parameters:

  • file (String)

    raw input contents.

Returns:

  • (Hash)

    metanorma-specific options (:type, :extensions, :relaton, :asciimath, :novalid).



121
122
123
# File 'lib/metanorma/processor/processor.rb', line 121

def extract_metanorma_options(file)
  Metanorma::Input::Asciidoc.new.extract_metanorma_options(file)
end

#extract_options(file) ⇒ Hash

Read processor-relevant options from the input file’s header via Input::Asciidoc#extract_options, merging in this processor’s output_formats for downstream stages.

Parameters:

  • file (String)

    raw input contents.

Returns:

  • (Hash)

    extracted options.



110
111
112
113
# File 'lib/metanorma/processor/processor.rb', line 110

def extract_options(file)
  Metanorma::Input::Asciidoc.new.extract_options(file)
    .merge(output_formats: output_formats)
end

#input_to_isodoc(file, filename, options = {}) ⇒ String

Convert an input file to Metanorma semantic XML by routing it through the Input::Asciidoc processor with this processor’s Asciidoctor backend. Override for non-Asciidoc inputs.

Parameters:

  • file (String)

    the raw input contents.

  • filename (String)

    the source filename (used for relative path resolution in includes).

  • options (Hash) (defaults to: {})

    passthrough options for the input processor; see Input::Asciidoc#process.

Returns:

  • (String)

    Metanorma semantic XML.



57
58
59
60
# File 'lib/metanorma/processor/processor.rb', line 57

def input_to_isodoc(file, filename, options = {})
  Metanorma::Input::Asciidoc.new.process(file, filename,
                                         @asciidoctor_backend, options)
end

#options_preprocess(options) ⇒ Hash

Mutate options in place to ensure :output_formats is set. Override to add other flavor-specific defaults.

Parameters:

  • options (Hash)

    processor options.

Returns:

  • (Hash)

    options with :output_formats defaulted.



86
87
88
# File 'lib/metanorma/processor/processor.rb', line 86

def options_preprocess(options)
  options[:output_formats] ||= output_formats
end

#output(isodoc_node, _inname, outname, _format, _options = {}) ⇒ Integer

Default output-writer: dump the rendered output string to a UTF-8 file. Override for binary formats (Word, PDF) that need different post-processing.

Parameters:

  • isodoc_node (String)

    rendered output content.

  • _inname (String)

    source filename (unused at this base level).

  • outname (String)

    destination path.

  • _format (Symbol)

    output format (unused at this base level).

  • _options (Hash) (defaults to: {})

    processor options (unused at this base level).

Returns:

  • (Integer)

    bytes written (Ruby’s File#write return).



100
101
102
# File 'lib/metanorma/processor/processor.rb', line 100

def output(isodoc_node, _inname, outname, _format, _options = {})
  File.open(outname, "w:UTF-8") { |f| f.write(isodoc_node) }
end

#output_formatsHash{Symbol => String}

Mapping of output-format name to file extension. Subclasses typically extend this with HTML / Word / PDF entries.

Returns:

  • (Hash{Symbol => String})

    format symbol -> file extension.



38
39
40
41
42
43
44
# File 'lib/metanorma/processor/processor.rb', line 38

def output_formats
  {
    xml: "xml",
    presentation: "presentation.xml",
    rxl: "rxl",
  }
end

#use_presentation_xml(ext) ⇒ Boolean

Whether the given output extension is downstream of the presentation XML stage (and therefore needs presentation XML generated first). Defaults to true for HTML/Word/PDF. Other formats such as RFC and STS are generated directly from semantic XML

Parameters:

  • ext (Symbol)

    output-format symbol.

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'lib/metanorma/processor/processor.rb', line 73

def use_presentation_xml(ext)
  case ext
  when :html, :doc, :pdf then true
  else
    false
  end
end