lib/asciidoctor/doctest/asciidoc_renderer.rb in asciidoctor-doctest-1.5.0 vs lib/asciidoctor/doctest/asciidoc_renderer.rb in asciidoctor-doctest-1.5.1
- old
+ new
@@ -3,11 +3,11 @@
require 'asciidoctor/converter/template'
module Asciidoctor
module DocTest
##
- # This class is basically a wrapper for +Asciidoctor.render+ that allows to
+ # This class is basically a wrapper for +Asciidoctor.convert+ that allows to
# preset and validate some common parameters.
class AsciidocRenderer
attr_reader :backend_name, :converter, :template_dirs
@@ -31,48 +31,53 @@
# by default.
#
# @raise [ArgumentError] if some path from the +template_dirs+ doesn't
# exist or is not a directory.
#
- def initialize(backend_name: nil, converter: nil, template_dirs: [],
+ def initialize(backend_name: '', converter: nil, template_dirs: [],
templates_fallback: false)
@backend_name = backend_name.freeze
@converter = converter
- @converter ||= TemplateConverterAdapter unless template_dirs.empty? || templates_fallback
+ @converter ||= NoFallbackTemplateConverter unless template_dirs.empty? || templates_fallback
template_dirs = Array.wrap(template_dirs).freeze
-
- unless template_dirs.all? { |path| Dir.exist? path }
- fail ArgumentError, "Templates directory '#{path}' doesn't exist!"
+ 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
##
- # Renders the given +text+ in AsciiDoc syntax with Asciidoctor using the
- # tested backend.
+ # 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] rendered input.
+ # @return [String] converted input.
#
- def render(text, opts = {})
- renderer_opts = {
+ def convert(text, opts = {})
+ converter_opts = {
safe: :safe,
backend: backend_name,
converter: converter,
template_dirs: template_dirs
}.merge(opts)
- Asciidoctor.render(text.to_s, renderer_opts)
+ Asciidoctor.convert(text.to_s, converter_opts)
end
+
+ # Alias for backward compatibility.
+ alias_method :render, :convert
end
##
# @private
- # Adapter for +Asciidoctor::Converter::TemplateConverter+.
- class TemplateConverterAdapter < SimpleDelegator
+ # TemplateConverter that doesn't fallback to a built-in converter when
+ # no template for a node is found.
+ class NoFallbackTemplateConverter < SimpleDelegator
+ # NOTE: It didn't work with subclass of TemplateConverter instead of
+ # delegator, I have no idea why.
# Placeholder to be written in a rendered output in place of the node's
# content that cannot be rendered due to missing template.
NOT_FOUND_MARKER = '--TEMPLATE NOT FOUND--'