Class: Asciidoctor::TemplatesCompiler::ConverterGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/templates_compiler/converter_generator.rb

Overview

Source-code generator of Asciidoctor converter classes.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name:, transforms_code:, helpers_code: nil, register_for: [], backend_info: {}, delegate_backend: nil, ignore_convert_opts: false) ⇒ ConverterGenerator

Returns a new instance of ConverterGenerator.

Parameters:

  • class_name (String)

    full name of the converter class to generate (e.g. My::HTML::Converter).

  • transforms_code (#each)

    enumerable that yields pair: transform name ()

  • helpers_code (String, nil) (defaults to: nil)

    source code to include in the generated class. It must contain a module named Helpers.

  • register_for (Array<String>) (defaults to: [])

    an array of backend names that the generated converter should be registered for to handle. Default is empty.

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

    a hash of parameters for backend_info: basebackend, outfilesuffix, filetype, htmlsyntax, supports_templates. Default is empty.

  • delegate_backend (String, nil) (defaults to: nil)

    name of the backend (converter) to use as a fallback for AST nodes not supported by the generated converter. If not specified, no fallback will be used and converter will raise NoMethodError when it try to convert unsupported node.

  • ignore_convert_opts (Boolean) (defaults to: false)

    ignore (i.e. do not set as local variables) options passed to the #convert method (i.e. to the templates). This is needed only for Opal.

Raises:

  • (ArgumentError)

    if helpers_code is not blank and does not contain module Helpers.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/asciidoctor/templates_compiler/converter_generator.rb', line 48

def initialize(class_name:, transforms_code:, helpers_code: nil,
               register_for: [], backend_info: {}, delegate_backend: nil,
               ignore_convert_opts: false, **)
  @class_name = class_name
  @transforms_code = transforms_code
  @helpers_code = helpers_code
  @register_for = Array(register_for)
  @backend_info = backend_info
  @delegate_backend = delegate_backend
  @ignore_convert_opts = ignore_convert_opts

  if !helpers_code.blank? && helpers_code !~ /\bmodule Helpers[\s#]/
    raise ArgumentError, 'The helpers_code does not contain module Helpers'
  end
end

Class Method Details

.callObject

An alias for generate.

Parameters:

  • output (#<<)

    output stream where to write the generated class. Defaults to StringIO.

Returns:

  • the given output stream.



27
28
29
# File 'lib/asciidoctor/templates_compiler/converter_generator.rb', line 27

def generate(output: StringIO.new, **opts)
  new(**opts).call(output)
end

.generate(output: StringIO.new, **opts) ⇒ Object

Returns the given output stream.

Parameters:

  • class_name (String)

    full name of the converter class to generate (e.g. My::HTML::Converter).

  • transforms_code (#each)

    enumerable that yields pair: transform name ()

  • helpers_code (String, nil)

    source code to include in the generated class. It must contain a module named Helpers.

  • register_for (Array<String>)

    an array of backend names that the generated converter should be registered for to handle. Default is empty.

  • backend_info (Hash)

    a hash of parameters for backend_info: basebackend, outfilesuffix, filetype, htmlsyntax, supports_templates. Default is empty.

  • delegate_backend (String, nil)

    name of the backend (converter) to use as a fallback for AST nodes not supported by the generated converter. If not specified, no fallback will be used and converter will raise NoMethodError when it try to convert unsupported node.

  • ignore_convert_opts (Boolean)

    ignore (i.e. do not set as local variables) options passed to the #convert method (i.e. to the templates). This is needed only for Opal.

  • output (#<<) (defaults to: StringIO.new)

    output stream where to write the generated class. Defaults to StringIO.

Returns:

  • the given output stream.

Raises:

  • (ArgumentError)

    if helpers_code is not blank and does not contain module Helpers.



22
23
24
# File 'lib/asciidoctor/templates_compiler/converter_generator.rb', line 22

def generate(output: StringIO.new, **opts)
  new(**opts).call(output)
end

Instance Method Details

#generate(out = StringIO.new) ⇒ Object Also known as: call

Generates source code of a converter class for Asciidoctor.

Parameters:

  • out (#<<) (defaults to: StringIO.new)

    output stream where to write the generated class.

Returns:

  • the given out stream.



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/asciidoctor/templates_compiler/converter_generator.rb', line 70

def generate(out = StringIO.new)
  out << head_code << "\n"
  out << helpers_code << "\n" unless @helpers_code.blank?
  out << initialization_code << "\n"
  out << convert_method_code << "\n"
  out << handles_method_code << "\n"
  transform_methods_code(out)
  out << support_methods_code << "\n"
  out << tail_code
  out
end