From dd81da11bb7deaa5af5714e415f072b9a1622d2b Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Sun, 10 Oct 2010 13:53:43 +0200 Subject: [PATCH] Add nokogiri helper method. Tilt supports Nokogiri for quite some time now, but it was not as easy to use as builder, and not documented. Tests and documentation included. --- README.de.rdoc | 13 +++++++ README.es.rdoc | 14 ++++++++ README.fr.rdoc | 14 ++++++++ README.jp.rdoc | 13 +++++++ README.rdoc | 13 +++++++ lib/sinatra/base.rb | 18 +++++++--- test/nokogiri_test.rb | 69 +++++++++++++++++++++++++++++++++++++ test/views/hello.nokogiri | 1 + test/views/layout2.nokogiri | 3 ++ 9 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 test/nokogiri_test.rb create mode 100644 test/views/hello.nokogiri create mode 100644 test/views/layout2.nokogiri diff --git a/README.de.rdoc b/README.de.rdoc index bd4dd2eb..6fd216b0 100644 --- a/README.de.rdoc +++ b/README.de.rdoc @@ -234,6 +234,19 @@ Das buidler gem wird benötigt, um Builder-Templates rendern zu können: Dieser Code rendert ./views/index.builder. +=== Nokogiri-Templates + +Das nokogiri gem wird benötigt, um Nokogiri-Templates rendern zu können: + + ## nokogiri muss eingebunden werden + require 'nokogiri' + + get '/' do + nokogiri :index + end + +Dieser Code rendert ./views/index.nokogiri. + === Sass-Templates Das haml gem wird benötigt, um SASS-Templates rendern zu können: diff --git a/README.es.rdoc b/README.es.rdoc index 0940616f..3eb1bd27 100644 --- a/README.es.rdoc +++ b/README.es.rdoc @@ -233,6 +233,20 @@ La gem/librería builder es necesaria para renderizar plantillas builder: Renderiza ./views/index.builder. +=== Plantillas Nokogiri + +La gem/librería nokogiri es necesaria para renderizar plantillas nokogiri: + + ## Vas a necesitar requerir nokogiri en tu app + require 'nokogiri' + + get '/' do + content_type 'application/xml', :charset => 'utf-8' + nokogiri :index + end + +Renderiza ./views/index.nokogiri. + === Plantillas Sass La gem/librería sass es necesaria para renderizar plantillas Sass: diff --git a/README.fr.rdoc b/README.fr.rdoc index 44990339..28db8c69 100644 --- a/README.fr.rdoc +++ b/README.fr.rdoc @@ -230,6 +230,20 @@ Le gem builder est nécessaire pour utiliser la fonction de rendu builder: Utilisera le template: ./views/index.builder. +=== Templates Nokogiri + +Le gem nokogiri est nécessaire pour utiliser la fonction de rendu nokogiri: + + ## Chargez la bibliothèque nokogiri dans votre application + require 'nokogiri' + + get '/' do + content_type 'application/xml', :charset => 'utf-8' + nokogiri :index + end + +Utilisera le template: ./views/index.nokogiri. + === Templates Sass Le gem sass est nécessaire pour utiliser la fonction de rendu Sass: diff --git a/README.jp.rdoc b/README.jp.rdoc index 7cf6bbe3..6230428c 100644 --- a/README.jp.rdoc +++ b/README.jp.rdoc @@ -159,6 +159,19 @@ builderを使うにはbuilderライブラリが必要です: ./views/index.builderを表示します。 +=== 鋸 テンプレート + +鋸を使うには鋸ライブラリが必要です: + + ## 鋸を読み込みます + require 'nokogiri' + + get '/' do + nokogiri :index + end + +./views/index.nokogiriを表示します。 + === Sass テンプレート Sassテンプレートを使うにはsassライブラリが必要です: diff --git a/README.rdoc b/README.rdoc index 0117c3d4..4da4d1c6 100644 --- a/README.rdoc +++ b/README.rdoc @@ -230,6 +230,19 @@ The builder gem/library is required to render builder templates: Renders ./views/index.builder. +=== Nokogiri Templates + +The nokogiri gem/library is required to render nokogiri templates: + + ## You'll need to require nokogiri in your app + require 'nokogiri' + + get '/' do + nokogiri :index + end + +Renders ./views/index.nokogiri. + === Sass Templates The sass gem/library is required to render Sass templates: diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index adaac09d..70b62e9d 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -350,10 +350,7 @@ module Sinatra end def builder(template=nil, options={}, locals={}, &block) - options[:default_content_type] = :xml - options, template = template, nil if template.is_a?(Hash) - template = Proc.new { block } if template.nil? - render :builder, template, options, locals + render_xml(:builder, template, options, locals, &block) end def liquid(template, options={}, locals={}) @@ -385,7 +382,20 @@ module Sinatra render :coffee, template, options, locals end + def nokogiri(template=nil, options={}, locals={}, &block) + options[:layout] = false if Tilt::VERSION <= "1.1" + render_xml(:nokogiri, template, options, locals, &block) + end + private + # logic shared between builder and nokogiri + def render_xml(engine, template, options={}, locals={}, &block) + options[:default_content_type] = :xml + options, template = template, nil if template.is_a?(Hash) + template = Proc.new { block } if template.nil? + render engine, template, options, locals + end + def render(engine, data, options={}, locals={}, &block) # merge app-level options options = settings.send(engine).merge(options) if settings.respond_to?(engine) diff --git a/test/nokogiri_test.rb b/test/nokogiri_test.rb new file mode 100644 index 00000000..7675cd96 --- /dev/null +++ b/test/nokogiri_test.rb @@ -0,0 +1,69 @@ +require File.dirname(__FILE__) + '/helper' + +begin +require 'nokogiri' + +class NokogiriTest < Test::Unit::TestCase + def nokogiri_app(&block) + mock_app do + set :views, File.dirname(__FILE__) + '/views' + get '/', &block + end + get '/' + end + + it 'renders inline Nokogiri strings' do + nokogiri_app { nokogiri 'xml' } + assert ok? + assert_equal %{\n}, body + end + + it 'renders inline blocks' do + nokogiri_app do + @name = "Frank & Mary" + nokogiri do |xml| + xml.couple @name + end + end + assert ok? + assert_equal "\nFrank & Mary\n", body + end + + it 'renders .nokogiri files in views path' do + nokogiri_app do + @name = "Blue" + nokogiri :hello + end + assert ok? + assert_equal %(\nYou're my boy, Blue!\n), body + end + + it "renders with inline layouts" do + next if Tilt::VERSION <= "1.1" + mock_app do + layout { %(xml.layout { xml << yield }) } + get('/') { nokogiri %(xml.em 'Hello World') } + end + get '/' + assert ok? + assert_equal "\n\n Hello World\n\n", body + end + + it "renders with file layouts" do + next if Tilt::VERSION <= "1.1" + nokogiri_app do + @name = "Blue" + nokogiri %(xml.em 'Hello World'), :layout => :layout2 + end + assert ok? + assert_equal "\n\n Hello World\n\n", body + end + + it "raises error if template not found" do + mock_app { get('/') { nokogiri :no_such_template } } + assert_raise(Errno::ENOENT) { get('/') } + end +end +rescue + warn "#{$!.to_s}: skipping nokogiri tests" +end diff --git a/test/views/hello.nokogiri b/test/views/hello.nokogiri new file mode 100644 index 00000000..16b86d03 --- /dev/null +++ b/test/views/hello.nokogiri @@ -0,0 +1 @@ +xml.exclaim "You're my boy, #{@name}!" diff --git a/test/views/layout2.nokogiri b/test/views/layout2.nokogiri new file mode 100644 index 00000000..9491f574 --- /dev/null +++ b/test/views/layout2.nokogiri @@ -0,0 +1,3 @@ +xml.layout do + xml << yield +end