Class: Asciidoctor::DocTest::AsciidocConverter

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

Overview

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

Direct Known Subclasses

HTML::Converter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ AsciidocConverter

Returns a new instance of AsciidocConverter

Parameters:

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

    the default options to be passed to Asciidoctor. For a complete list of all available options see the Asciidoctor's documentation

Options Hash (opts):

  • :backend (#to_s, nil)

    the name of the tested backend.

  • :backend_name (#to_s, nil)

    alias for the :backend.

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

    the backend's converter class (or its instance). When not specified, the default converter for the specified backend is used.

  • :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.

  • :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.

  • :safe (Symbol)

    the safe mode, one of :unsafe, :safe, :server, or :secure. Default is :safe.

Raises:

  • (ArgumentError)

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


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/asciidoctor/doctest/asciidoc_converter.rb', line 50

def initialize(opts = {})
  opts = opts.rekey(&:to_sym).rekey(:backend_name => :backend)

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

  unless template_dirs.empty?
    opts[:template_dirs] = template_dirs
    opts[:converter] ||= NoFallbackTemplateConverter unless opts[:templates_fallback]
  end

  opts[:safe] ||= :safe
  opts.delete(:backend) if opts[:backend].blank?

  @default_opts = opts
end

Instance Attribute Details

#default_optsHash (readonly) Also known as: opts

Returns the default options to be passed to Asciidoctor.

Returns:

  • (Hash)

    the default options to be passed to Asciidoctor.


17
18
19
# File 'lib/asciidoctor/doctest/asciidoc_converter.rb', line 17

def default_opts
  @default_opts
end

Instance Method Details

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

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. This will be merged with #default_opts with precedence of opts.

Returns:

  • (String)

    converted input.


78
79
80
# File 'lib/asciidoctor/doctest/asciidoc_converter.rb', line 78

def convert(text, opts = {})
  Asciidoctor.convert(text.to_s, @default_opts + opts)
end