From b18310f73635c063410f53885c74c08f3b5bb273 Mon Sep 17 00:00:00 2001 From: Juan David Pastas Date: Mon, 30 Jul 2012 10:05:06 -0500 Subject: [PATCH] add stylus template support --- README.es.rdoc | 6 +++ README.rdoc | 6 +++ lib/sinatra/base.rb | 5 +++ test/stylus_test.rb | 90 +++++++++++++++++++++++++++++++++++++++++++ test/views/hello.styl | 2 + 5 files changed, 109 insertions(+) create mode 100644 test/stylus_test.rb create mode 100644 test/views/hello.styl diff --git a/README.es.rdoc b/README.es.rdoc index 3ac03910e1..c53a3803b4 100644 --- a/README.es.rdoc +++ b/README.es.rdoc @@ -536,6 +536,12 @@ Dependencias:: {coffee-script}[https://github.com/josh/ruby-coffee-s Extensiones de Archivo:: .coffee Ejemplo:: coffee :index +=== Plantillas Stylus + +Dependencias:: {ruby-stylus}[https://github.com/lucasmazza/ruby-stylus] +Extensiones de Archivo:: .styl +Ejemplo:: stylus :index + === Plantillas Yajl Dependencias:: {yajl-ruby}[https://github.com/brianmario/yajl-ruby] diff --git a/README.rdoc b/README.rdoc index 0e16e2419c..2066325a18 100644 --- a/README.rdoc +++ b/README.rdoc @@ -520,6 +520,12 @@ Dependency:: {coffee-script}[https://github.com/josh/ruby-coffee-script] File Extensions:: .coffee Example:: coffee :index +=== Stylus Templates + +Dependency:: {ruby-stylus}[https://github.com/lucasmazza/ruby-stylus] +File Extensions:: .styl +Example:: stylus :index + === Yajl Templates Dependency:: {yajl-ruby}[https://github.com/brianmario/yajl-ruby] diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 33648b28e3..86cb4f338e 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -598,6 +598,11 @@ def less(template, options={}, locals={}) render :less, template, options, locals end + def stylus(template, options={}, locals={}) + options.merge! :layout => false, :default_content_type => :css + render :styl, template, options, locals + end + def builder(template=nil, options={}, locals={}, &block) options[:default_content_type] = :xml render_ruby(:builder, template, options, locals, &block) diff --git a/test/stylus_test.rb b/test/stylus_test.rb new file mode 100644 index 0000000000..612f4d418e --- /dev/null +++ b/test/stylus_test.rb @@ -0,0 +1,90 @@ +require File.expand_path('../helper', __FILE__) + +begin + require 'stylus' + + begin + Stylus.compile '1' + rescue Exception + raise LoadError, 'unable to find Stylus compiler' + end + + class StylusTest < Test::Unit::TestCase + def stylus_app(options = {}, &block) + mock_app do + set :views, File.dirname(__FILE__) + '/views' + set(options) + get('/', &block) + end + get '/' + end + + it 'renders inline Stylus strings' do + assert false, 'waiting response on how stylus can be rendered inline' + stylus_app { stylus "a margin auto\n" } + assert ok? + assert body.include?("a {\n margin: auto;\n}\n") + end + + it 'defaults content type to css' do + stylus_app { stylus :hello } + assert ok? + assert_equal "text/css;charset=utf-8", response['Content-Type'] + end + + it 'defaults allows setting content type per route' do + stylus_app do + content_type :html + stylus :hello + end + assert ok? + assert_equal "text/html;charset=utf-8", response['Content-Type'] + end + + it 'defaults allows setting content type globally' do + stylus_app(:styl => { :content_type => 'html' }) do + stylus :hello + end + assert ok? + assert_equal "text/html;charset=utf-8", response['Content-Type'] + end + + it 'renders .styl files in views path' do + stylus_app { stylus :hello } + assert ok? + assert_include body, "a {\n margin: auto;\n}\n" + end + + it 'ignores the layout option' do + stylus_app { stylus :hello, :layout => :layout2 } + assert ok? + assert_include body, "a {\n margin: auto;\n}\n" + end + + it "raises error if template not found" do + mock_app { + get('/') { stylus :no_such_template } + } + assert_raise(Errno::ENOENT) { get('/') } + end + + it "passes stylus options to the stylus engine" do + stylus_app { stylus :hello, :no_wrap => true } + assert ok? + assert_body "a {\n margin: auto;\n}\n" + end + + it "passes default stylus options to the stylus engine" do + mock_app do + set :stylus, :no_wrap => true # default stylus style is :nested + get('/') { stylus :hello } + end + get '/' + assert ok? + assert_body "a {\n margin: auto;\n}\n" + end + end + +rescue LoadError + warn "#{$!.to_s}: skipping stylus tests" +end diff --git a/test/views/hello.styl b/test/views/hello.styl new file mode 100644 index 0000000000..3be6744e3b --- /dev/null +++ b/test/views/hello.styl @@ -0,0 +1,2 @@ +a + margin auto