Sha256: 2511906dbf3be6db8b766f306ca251b7e9bd22e14be038eb0bc2d4b9fe8c251d

Contents?: true

Size: 1.19 KB

Versions: 15

Compression:

Stored size: 1.19 KB

Contents

# encoding: UTF-8

require 'sass'

module Spontaneous::Rack
  class CSS
    CSS_EXTENSION = /\.css$/o

    def initialize(app, options = {})
      @roots = options[:root]
      @app = app
    end

    def call(env)
      try_sass(env[S::PATH_INFO]) or @app.call(env)
    end

    def try_sass(request_path)
      return nil unless request_path =~ CSS_EXTENSION
      try_paths = @roots.map { |root| File.join(root, request_path) }
      # if the css file itself exists then we want to use that
      return nil if try_paths.detect { |file| ::File.exists?(file) }

      try_paths.each do |path|
        template = template_path(path)
        return render_sass_template(template) if File.exists?(template)
      end
      nil
    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_path(path)
      path.gsub(CSS_EXTENSION, ".scss")
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
spontaneous-0.2.0.beta10 lib/spontaneous/rack/css.rb
spontaneous-0.2.0.beta9 lib/spontaneous/rack/css.rb
spontaneous-0.2.0.beta8 lib/spontaneous/rack/css.rb
spontaneous-0.2.0.beta7 lib/spontaneous/rack/css.rb
spontaneous-0.2.0.beta6 lib/spontaneous/rack/css.rb
spontaneous-0.2.0.beta5 lib/spontaneous/rack/css.rb
spontaneous-0.2.0.beta4 lib/spontaneous/rack/css.rb
spontaneous-0.2.0.beta3 lib/spontaneous/rack/css.rb
spontaneous-0.2.0.beta2 lib/spontaneous/rack/css.rb
spontaneous-0.2.0.beta1 lib/spontaneous/rack/css.rb
spontaneous-0.2.0.alpha7 lib/spontaneous/rack/css.rb
spontaneous-0.2.0.alpha6 lib/spontaneous/rack/css.rb
spontaneous-0.2.0.alpha5 lib/spontaneous/rack/css.rb
spontaneous-0.2.0.alpha4 lib/spontaneous/rack/css.rb
spontaneous-0.2.0.alpha3 lib/spontaneous/rack/css.rb