# File lib/asciidoctor/converter/template.rb, line 233
    def scan_dir template_dir, pattern, template_cache = nil
      result = {}
      eruby_loaded = nil
      # Grab the files in the top level of the directory (do not recurse)
      ::Dir.glob(pattern).select {|match| ::File.file? match }.each do |file|
        if (basename = ::File.basename file) == 'helpers.rb' || (path_segments = basename.split '.').size < 2
          next
        end
        # TODO we could derive the basebackend from the minor extension of the template file
        #name, *rest, ext_name = *path_segments # this form only works in Ruby >= 1.9
        name = path_segments[0]
        if name == 'block_ruler'
          name = 'thematic_break'
        elsif name.start_with? 'block_'
          name = name[6..-1]
        end
        ext_name = path_segments[-1]
        template_class = ::Tilt
        extra_engine_options = {}
        if ext_name == 'slim'
          # slim doesn't get loaded by Tilt, so we have to load it explicitly
          Helpers.require_library 'slim' unless defined? ::Slim
          # align safe mode of AsciiDoc embedded in Slim template with safe mode of current document
          (@engine_options[:slim][:asciidoc] ||= {})[:safe] ||= @safe if @safe && ::Slim::VERSION >= '3.0'
          # load include plugin when using Slim >= 2.1
          require 'slim/include' unless (defined? ::Slim::Include) || ::Slim::VERSION < '2.1'
        elsif ext_name == 'erb'
          template_class, extra_engine_options = (eruby_loaded ||= load_eruby(@eruby))
        end
        next unless ::Tilt.registered? ext_name
        unless template_cache && (template = template_cache[file])
          template = template_class.new file, 1, (@engine_options[ext_name.to_sym] || {}).merge(extra_engine_options)
        end
        result[name] = template
      end
      if ::File.file?(helpers = (::File.join template_dir, 'helpers.rb'))
        require helpers
      end
      result
    end