# File lib/sinatra/respond_to.rb, line 19
    def self.registered(app)
      app.helpers RespondTo::Helpers

      app.set :default_content, :html
      app.set :assume_xhr_is_js, true

      # We remove the trailing extension so routes
      # don't have to be of the style
      #
      #   get '/resouce.:format'
      #
      # They can instead be of the style
      #
      #   get '/resource'
      #
      # and the format will automatically be available in <tt>format</tt>
      app.before do
        # Let through sinatra image urls in development
        next if self.class.development? && request.path_info =~ %r{/__sinatra__/.*?.png}

        unless settings.static? && settings.public_folder? && (request.get? || request.head?) && static_file?(request.path_info)
          if request.params.has_key? 'format'
            format params['format']
          else
            # Sinatra relies on a side-effect from path_info= to
            # determine its routes. A direct string change (e.g., sub!)
            # would bypass that -- and fail to have the effect we're looking
            # for.
            request.path_info = request.path_info.sub %r{\.([^\./]+)$}, ''

            format $1 || (request.xhr? && settings.assume_xhr_is_js? ? :js : settings.default_content)
          end
        end
      end

      app.configure :development do |dev|
        dev.error UnhandledFormat do
          content_type :html, :charset => 'utf-8'

          ("<!DOCTYPE html>\n<html>\n<head>\n<style type=\"text/css\">\nbody { text-align:center;font-family:helvetica,arial;font-size:22px;\ncolor:#888;margin:20px}\n#c {margin:0 auto;width:500px;text-align:left}\n</style>\n</head>\n<body>\n<h2>Sinatra doesn't know this ditty.</h2>\n<img src='/__sinatra__/404.png'>\n<div id=\"c\">\nTry this:\n<pre>\#{request.request_method.downcase} '\#{request.path_info}' do\\n  respond_to do |wants|\\n    wants.\#{format} { \"Hello World\" }\\n  end\\nend</pre>\n</div>\n</body>\n</html>\n").gsub(/^ {10}/, '')
        end

        dev.error MissingTemplate do
          content_type :html, :charset => 'utf-8'
          response.status = 500

          engine = request.env['sinatra.error'].message.split('.').last
          engine = 'haml' unless ['haml', 'builder', 'erb'].include? engine

          path = File.basename(request.path_info)
          path = "root" if path.nil? || path.empty?

          format = engine == 'builder' ? 'xml' : 'html'

          layout = case engine
                   when 'haml' then "!!!\n%html\n  %body= yield"
                   when 'erb' then "<html>\n  <body>\n    <%= yield %>\n  </body>\n</html>"
                   when 'builder' then "xml << yield"
                   end

          layout = "<small>app.#{format}.#{engine}</small>\n<pre>#{escape_html(layout)}</pre>"

          ("<!DOCTYPE html>\n<html>\n<head>\n<style type=\"text/css\">\nbody { text-align:center;font-family:helvetica,arial;font-size:22px;\ncolor:#888;margin:20px}\n#c {margin:0 auto;width:500px;text-align:left;}\nsmall {float:right;clear:both;}\npre {clear:both;}\n</style>\n</head>\n<body>\n<h2>Sinatra can't find \#{request.env['sinatra.error'].message}</h2>\n<img src='/__sinatra__/500.png'>\n<div id=\"c\">\nTry this:<br />\n\#{layout}\n<small>\#{path}.\#{format}.\#{engine}</small>\n<pre>Hello World!</pre>\n<small>application.rb</small>\n<pre>\#{request.request_method.downcase} '\#{request.path_info}' do\\n  respond_to do |wants|\\n    wants.\#{engine == 'builder' ? 'xml' : 'html'} { \#{engine} :\#{path}\#{\",\\n\#{' '*32}layout => :app\" if layout} }\\n  end\\nend</pre>\n</div>\n</body>\n</html>\n").gsub(/^ {10}/, '')
        end
      end
    end