Class: Asciidoctor::DocTest::AsciidocRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/doctest/asciidoc_renderer.rb

Overview

This class is basically a wrapper for Asciidoctor.convert that allows to preset and validate some common parameters.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend_name: '', converter: nil, template_dirs: [], templates_fallback: false) ⇒ AsciidocRenderer

Returns a new instance of AsciidocRenderer.

Parameters:

  • backend_name (#to_s) (defaults to: '')

    the name of the tested backend.

  • converter (Class, Asciidoctor::Converter::Base, nil) (defaults to: nil)

    the backend’s converter class (or its instance). If not specified, the default converter for the specified backend will be used.

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

    path of the directory (or multiple directories) where to look for the backend’s templates. Relative paths are referenced from the working directory.

  • templates_fallback (Boolean) (defaults to: false)

    allow to fall back to using an appropriate built-in converter when there is no required template in the tested backend? This is actually a default behaviour in Asciidoctor, but since it’s inappropriate for testing of custom backends, it’s disabled by default.

Raises:

  • (ArgumentError)

    if some path from the template_dirs doesn’t exist or is not a directory.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/asciidoctor/doctest/asciidoc_renderer.rb', line 36

def initialize(backend_name: '', converter: nil, template_dirs: [],
               templates_fallback: false)

  @backend_name = backend_name.freeze
  @converter = converter
  @converter ||= NoFallbackTemplateConverter unless template_dirs.empty? || templates_fallback

  template_dirs = Array.wrap(template_dirs).freeze
  template_dirs.each do |path|
    fail ArgumentError, "Templates directory '#{path}' doesn't exist!" unless Dir.exist? path
  end
  @template_dirs = template_dirs unless template_dirs.empty?
end

Instance Attribute Details

#backend_nameObject (readonly)

Returns the value of attribute backend_name.



12
13
14
# File 'lib/asciidoctor/doctest/asciidoc_renderer.rb', line 12

def backend_name
  @backend_name
end

#converterObject (readonly)

Returns the value of attribute converter.



12
13
14
# File 'lib/asciidoctor/doctest/asciidoc_renderer.rb', line 12

def converter
  @converter
end

#template_dirsObject (readonly)

Returns the value of attribute template_dirs.



12
13
14
# File 'lib/asciidoctor/doctest/asciidoc_renderer.rb', line 12

def template_dirs
  @template_dirs
end

Instance Method Details

#convert(text, opts = {}) ⇒ String Also known as: render

Converts the given text into AsciiDoc syntax with Asciidoctor using the tested backend.

Parameters:

  • text (#to_s)

    the input text in AsciiDoc syntax.

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

    options to pass to Asciidoctor.

Returns:

  • (String)

    converted input.



58
59
60
61
62
63
64
65
66
67
# File 'lib/asciidoctor/doctest/asciidoc_renderer.rb', line 58

def convert(text, opts = {})
  converter_opts = {
    safe: :safe,
    backend: backend_name,
    converter: converter,
    template_dirs: template_dirs
  }.merge(opts)

  Asciidoctor.convert(text.to_s, converter_opts)
end