Sha256: 9882ddf58c225dd255005cfb452810b5f3f7be1487643a57980e47097af336bd

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

# encoding: UTF-8

require 'sass'

module Spontaneous::Rack
  class CSS
    def initialize(app, options = {})
      @options = options
      @root = options.delete(:root)
      @app = app
    end

    def call(env)
      path = env[S::PATH_INFO]
      if path =~ /\.css$/ and !File.exists?(File.join(@root, path))
        template = template_root_path(path) + ".scss"
        if File.exists?(template)
          render_sass_template(template)
        else
          raise Sinatra::NotFound
        end
      else
        @app.call(env)
      end
    end

    def render_sass_template(template)
      load_paths = [Spontaneous.css_dir, File.dirname(template), File.join(File.dirname(template), "sass")]
      engine = Sass::Engine.for_file(template, {
        :load_paths => load_paths,
        :filename => template,
        :cache => false,
        :style => :expanded
      })
      [200, {'Content-type' => 'text/css'}, [engine.render]]
    end

    def template_root_path(path)
      basename = File.basename(path, '.css')
      File.join(@root, File.dirname(path), basename)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spontaneous-0.2.0.alpha2 lib/spontaneous/rack/css.rb
spontaneous-0.2.0.alpha1 lib/spontaneous/rack/css.rb