module Middleman::Sass

def self.included(base)

def self.included(base)
  base.supported_formats << "sass"
end

def render_path(path)

def render_path(path)
  if template_exists?(path, :sass)
    begin
      static_version = options.public + request.path_info
      send_file(static_version) if File.exists? static_version
      location_of_sass_file = options.environment == "build" ? File.join(options.build_dir, options.css_dir) : "public"
      css_filename = File.join(Dir.pwd, location_of_sass_file) + request.path_info
      sass(path.to_sym, Compass.sass_engine_options.merge({ :css_filename => css_filename }))
    rescue Exception => e
      sass_exception_string(e)
    end
  else
    super
  end
end

def sass_exception_string(e)

Handle Sass errors
def sass_exception_string(e)
  e_string = "#{e.class}: #{e.message}"
  if e.is_a? ::Sass::SyntaxError
    e_string << "\non line #{e.sass_line}"
    if e.sass_filename
      e_string << " of #{e.sass_filename}"
      if File.exists?(e.sass_filename)
        e_string << "\n\n"
        min = [e.sass_line - 5, 0].max
        begin
          File.read(e.sass_filename).rstrip.split("\n")[
            min .. e.sass_line + 5
          ].each_with_index do |line, i|
            e_string << "#{min + i + 1}: #{line}\n"
          end
        rescue
          e_string << "Couldn't read sass file: #{e.sass_filename}"
        end
      end
    end
  end
  <<END
string}
trace:\n#{e.backtrace.join("\n")}
:before {
ite-space: pre;
nt-family: monospace;
ntent: "#{e_string.gsub('"', '\"').gsub("\n", '\\A ')}"; }
end