From c248dbac9db09f726f11d4877b49c80025961da0 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Sun, 12 Sep 2010 13:59:00 +0200 Subject: [PATCH] Add rdoc helper method. Tilt supports RDoc for quite some time now, but it was not as easy to use as haml or erb, and not documented. Tests and documentation (English and German) included. --- README.de.rdoc | 25 +++++++++++++++++++++++++ README.rdoc | 22 ++++++++++++++++++++++ lib/sinatra/base.rb | 4 ++++ test/rdoc_test.rb | 34 ++++++++++++++++++++++++++++++++++ test/views/hello.rdoc | 1 + 5 files changed, 86 insertions(+) create mode 100644 test/rdoc_test.rb create mode 100644 test/views/hello.rdoc diff --git a/README.de.rdoc b/README.de.rdoc index 96ff7ab0c3..980fdf4174 100644 --- a/README.de.rdoc +++ b/README.de.rdoc @@ -368,6 +368,31 @@ aufzurufen: %h1 Hallo von Haml! %p= textile(:greetings) +=== RDoc-Templates + +Das rdoc gem wird benötigt um RDoc-Templates rendern zu können: + + ## redcloth muss eingebunden werden + require "rdoc" + + get '/' do + rdoc :index + end + +Dieser Code rendert ./views/index.rdoc. + +Da es weder möglich ist Methoden aufzurufen, noch +locals+ zu übergeben, ist +es am sinnvollsten RDoc in Kombination mit einer anderen Template-Engine +zu nutzen: + + erb :overview, :locals => { :text => rdoc(:introduction) } + +Es ist auch möglich die +rdoc+ Methode aus anderen Templates heraus +aufzurufen: + + %h1 Hallo von Haml! + %p= rdoc(:greetings) + === Inline-Templates get '/' do diff --git a/README.rdoc b/README.rdoc index f79dbac5ee..3b8e62069e 100644 --- a/README.rdoc +++ b/README.rdoc @@ -359,6 +359,28 @@ Note that you may also call the textile method from within other templates: %h1 Hello From Haml! %p= textile(:greetings) +=== RDoc Templates + +The RDoc gem/library is required to render RDoc templates: + + ## You'll need to require rdiscount in your app + require "rdoc" + + get '/' do + rdoc :index + end + +Renders ./views/index.rdoc. + +It is not possible to call methods from rdoc, nor to pass locals to it. You therefore will usually use it in combination with another rendering engine: + + erb :overview, :locals => { :text => rdoc(:introduction) } + +Note that you may also call the rdoc method from within other templates: + + %h1 Hello From Haml! + %p= rdoc(:greetings) + === Inline Templates get '/' do diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index ff66bfebc6..807bc0e31b 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -347,6 +347,10 @@ def textile(template, options={}, locals={}) render :textile, template, options, locals end + def rdoc(template, options={}, locals={}) + render :rdoc, template, options, locals + end + private def render(engine, data, options={}, locals={}, &block) # merge app-level options diff --git a/test/rdoc_test.rb b/test/rdoc_test.rb new file mode 100644 index 0000000000..8762e3dba2 --- /dev/null +++ b/test/rdoc_test.rb @@ -0,0 +1,34 @@ +require File.dirname(__FILE__) + '/helper' + +begin +require 'rdoc' + +class RdocTest < Test::Unit::TestCase + def rdoc_app(&block) + mock_app do + set :views, File.dirname(__FILE__) + '/views' + get '/', &block + end + get '/' + end + + it 'renders inline rdoc strings' do + rdoc_app { rdoc '= Hiya' } + assert ok? + assert_equal "

Hiya

\n", body + end + + it 'renders .rdoc files in views path' do + rdoc_app { rdoc :hello } + assert ok? + assert_equal "

Hello From RDoc

\n", body + end + + it "raises error if template not found" do + mock_app { get('/') { rdoc :no_such_template } } + assert_raise(Errno::ENOENT) { get('/') } + end +end +rescue + warn "#{$!.to_s}: skipping rdoc tests" +end diff --git a/test/views/hello.rdoc b/test/views/hello.rdoc new file mode 100644 index 0000000000..cfba71486e --- /dev/null +++ b/test/views/hello.rdoc @@ -0,0 +1 @@ += Hello From RDoc