Class: Asciidoctor::TemplatesCompiler::Base

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

Overview

Base class for templates compilers.

Direct Known Subclasses

Slim

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.callObject

An “alias” for #compile_converter.



18
19
20
# File 'lib/asciidoctor/templates_compiler/base.rb', line 18

def compile_converter(**opts)
  new.compile_converter(**opts)
end

.compile_converter(**opts) ⇒ Object

An “alias” for #compile_converter.



14
15
16
# File 'lib/asciidoctor/templates_compiler/base.rb', line 14

def compile_converter(**opts)
  new.compile_converter(**opts)
end

Instance Method Details

#compile_converter(templates_dir:, engine_opts: {}, pretty: false, **opts) ⇒ Object Also known as: call

Compiles templates found in templates_dir to Ruby and generates an Asciidoctor converter class from them.

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 (#<<)

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

  • templates_dir (String)

    path of the directory where to look for templates and (optional) helpers.rb.

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

    a hash of options to pass into the templating engine. Default is empty.

  • pretty (Boolean) (defaults to: false)

    enable pretty-formatting of the generated Ruby code?

Returns:

  • the given output stream.

Raises:

  • (ArgumentError)

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

  • (ArgumentError)

    if the given templates_dir does not exist.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/asciidoctor/templates_compiler/base.rb', line 35

def compile_converter(templates_dir:, engine_opts: {}, pretty: false, **opts)
  unless Dir.exist? templates_dir
    raise ArgumentError, "Templates directory '#{templates_dir}' does not exist"
  end

  templates = find_templates(templates_dir)
  transforms_code = compile_templates(templates, engine_opts, pretty: pretty)

  generate_class(transforms_code: transforms_code,
                 helpers_code: read_helpers(templates_dir), **opts)
end