class Asciidoctor::DocTest::AsciidocRenderer

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

Attributes

backend_name[R]
converter[R]
template_dirs[R]

Public Class Methods

new(backend_name: nil, converter: nil, template_dirs: [], templates_fallback: false) click to toggle source

@param backend_name [#to_s, nil] the name of the tested backend.

@param converter [Class, Asciidoctor::Converter::Base, nil]

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

@param template_dirs [Array<String>, String] path of the directory (or

multiple directories) where to look for the backend's templates.
Relative paths are referenced from the working directory.

@param templates_fallback [Boolean] 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.

@raise [ArgumentError] if some path from the template_dirs doesn’t

exist or is not a directory.
# File lib/asciidoctor/doctest/asciidoc_renderer.rb, line 39
def initialize(backend_name: nil, converter: nil, template_dirs: [],
               templates_fallback: false)

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

  template_dirs = Array(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

Public Instance Methods

convert(text, opts = {}) click to toggle source

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

@param text [#to_s] the input text in AsciiDoc syntax. @param opts [Hash] options to pass to Asciidoctor. @return [String] converted input.

# File lib/asciidoctor/doctest/asciidoc_renderer.rb, line 61
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
Also aliased as: render
render(text, opts = {})

Alias for backward compatibility.

Alias for: convert