Adding scss support through specific command

This commit is contained in:
Pedro Menezes 2010-08-29 21:56:44 -03:00 committed by Konstantin Haase
parent 5556dbee74
commit cf3c218a6c
6 changed files with 125 additions and 1 deletions

View File

@ -203,6 +203,32 @@ and overridden on an individual basis.
sass :stylesheet, :style => :expanded # overridden
end
=== Scss Templates
The sass gem/library is required to render Scss templates:
## You'll need to require haml or sass in your app
require 'sass'
get '/stylesheet.css' do
content_type 'text/css', :charset => 'utf-8'
scss :stylesheet
end
Renders <tt>./views/stylesheet.scss</tt>.
{Scss' options}[http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#options]
can be set globally through Sinatra's configurations,
see {Options and Configurations}[http://www.sinatrarb.com/configuration.html],
and overridden on an individual basis.
set :scss, {:style => :compact } # default Scss style is :nested
get '/stylesheet.css' do
content_type 'text/css', :charset => 'utf-8'
scss :stylesheet, :style => :expanded # overridden
end
=== Less Templates
The less gem/library is required to render Less templates:

View File

@ -317,6 +317,11 @@ module Sinatra
render :sass, template, options, locals
end
def scss(template, options={}, locals={})
options[:layout] = false
render :scss, template, options, locals
end
def less(template, options={}, locals={})
options[:layout] = false
render :less, template, options, locals

View File

@ -514,6 +514,31 @@ module Tilt
register 'sass', SassTemplate
# Scss template implementation.
#
# Scss templates do not support object scopes, locals, or yield.
class ScssTemplate < Template
def initialize_engine
return if defined? ::Sass::Engine
require_template_library 'sass'
end
def prepare
@engine = ::Sass::Engine.new(data, scss_options)
end
def evaluate(scope, locals, &block)
@output ||= @engine.render
end
private
def scss_options
options.merge(:filename => eval_file, :line => line, :syntax => :scss)
end
end
register 'scss', ScssTemplate
# Lessscss template implementation. See:
# http://lesscss.org/
#

View File

@ -4,7 +4,7 @@ Gem::Specification.new do |s|
s.name = 'sinatra'
s.version = '1.0'
s.date = '2010-03-23'
s.date = '2010-08-29'
s.description = "Classy web-development dressed in a DSL"
s.summary = "Classy web-development dressed in a DSL"
@ -17,6 +17,7 @@ Gem::Specification.new do |s|
AUTHORS
CHANGES
LICENSE
README.de.rdoc
README.jp.rdoc
README.rdoc
Rakefile

64
test/scss_test.rb Normal file
View File

@ -0,0 +1,64 @@
require File.dirname(__FILE__) + '/helper'
begin
require 'sass'
class ScssTest < Test::Unit::TestCase
def scss_app(&block)
mock_app {
set :views, File.dirname(__FILE__) + '/views'
get '/', &block
}
get '/'
end
it 'renders inline Scss strings' do
scss_app { scss "#scss {\n background-color: white; }\n" }
assert ok?
assert_equal "#scss {\n background-color: white; }\n", body
end
it 'renders .scss files in views path' do
scss_app { scss :hello }
assert ok?
assert_equal "#scss {\n background-color: white; }\n", body
end
it 'ignores the layout option' do
scss_app { scss :hello, :layout => :layout2 }
assert ok?
assert_equal "#scss {\n background-color: white; }\n", body
end
it "raises error if template not found" do
mock_app {
get('/') { scss :no_such_template }
}
assert_raise(Errno::ENOENT) { get('/') }
end
it "passes scss options to the scss engine" do
scss_app {
scss "#scss {\n background-color: white;\n color: black\n}",
:style => :compact
}
assert ok?
assert_equal "#scss { background-color: white; color: black; }\n", body
end
it "passes default scss options to the scss engine" do
mock_app {
set :scss, {:style => :compact} # default scss style is :nested
get '/' do
scss "#scss {\n background-color: white;\n color: black;\n}"
end
}
get '/'
assert ok?
assert_equal "#scss { background-color: white; color: black; }\n", body
end
end
rescue
warn "#{$!.to_s}: skipping scss tests"
end

3
test/views/hello.scss Normal file
View File

@ -0,0 +1,3 @@
#scss {
background-color: white
}