# File lib/asciidoctor/converter/template.rb, line 99
    def scan
      path_resolver = PathResolver.new
      backend = @backend
      engine = @engine
      @template_dirs.each do |template_dir|
        # FIXME need to think about safe mode restrictions here
        next unless ::File.directory?(template_dir = (path_resolver.system_path template_dir, nil))

        # NOTE last matching template wins for template name if no engine is given
        file_pattern = '*'
        if engine
          file_pattern = %(*.#{engine})
          # example: templates/haml
          if ::File.directory?(engine_dir = (::File.join template_dir, engine))
            template_dir = engine_dir
          end
        end

        # example: templates/html5 or templates/haml/html5
        if ::File.directory?(backend_dir = (::File.join template_dir, backend))
          template_dir = backend_dir
        end

        pattern = ::File.join template_dir, file_pattern

        if (scan_cache = @caches[:scans])
          template_cache = @caches[:templates]
          unless (templates = scan_cache[pattern])
            templates = (scan_cache[pattern] = (scan_dir template_dir, pattern, template_cache))
          end
          templates.each do |name, template|
            @templates[name] = template_cache[template.file] = template
          end
        else
          @templates.update scan_dir(template_dir, pattern, @caches[:templates])
        end
        nil
      end
    end