Browse files

Add markdown helper method. Tilt supports markdown for quite some tim…

…e now, but it was not as easy to use as haml or erb, and not documented. Tests and documentation (English and German) included.
  • Loading branch information...
1 parent 4db91f3 commit 970169b1fb001fbd9245c48f5d83c4321fb58387 @rkh rkh committed Sep 11, 2010
Showing with 94 additions and 0 deletions.
  1. +26 −0 README.de.rdoc
  2. +23 −0 README.rdoc
  3. +9 −0 lib/sinatra/base.rb
  4. +1 −0 sinatra.gemspec
  5. +34 −0 test/markdown_test.rb
  6. +1 −0 test/views/hello.md
View
26 README.de.rdoc
@@ -317,6 +317,32 @@ aufrufen kann, will man nahezu in allen Fällen +locals+ übergeben:
liquid :index, :locals => { :key => 'value' }
+=== Markdown-Templates
+
+Das rdiscount gem wird benötigt um Markdown-Templates rendern zu können:
+
+ ## rdiscount muss eingebunden werden
+ require "rdiscount"
+
+ get '/' do
+ markdown :index
+ end
+
+Dieser Code rendert <tt>./views/index.markdown</tt> (+md+ und +mkd+ sind
+ebenfalls zulässige Dateiendungen).
+
+Da es weder möglich ist Methoden aufzurufen, noch +locals+ zu übergeben, ist
+es am sinnvollsten Markdown in Kombination mit einer anderen Template-Engine
+zu nutzen:
+
+ erb :overview, :locals => { :text => markdown(:introduction) }
+
+Es ist auch möglich die +markdown+ Methode aus anderen Templates heraus
+aufzurufen:
+
+ %h1 Hallo von Haml!
+ %p= markdown(:greetings)
+
=== Inline-Templates
get '/' do
View
23 README.rdoc
@@ -314,6 +314,29 @@ template, you almost always want to pass locals to it:
liquid :index, :locals => { :key => 'value' }
+=== Markdown Templates
+
+The rdiscount gem/library is required to render Markdown templates:
+
+ ## You'll need to require rdiscount in your app
+ require "rdiscount"
+
+ get '/' do
+ markdown :index
+ end
+
+Renders <tt>./views/index.markdown</tt> (+md+ and +mkd+ are also valid file
+extensions).
+
+It is not possible to call methods from markdown, nor to pass locals to it. You therefore will usually use it in combination with another rendering engine:
+
+ erb :overview, :locals => { :text => markdown(:introduction) }
+
+Note that you may also call the markdown method from within other templates:
+
+ %h1 Hello From Haml!
+ %p= markdown(:greetings)
+
=== Inline Templates
get '/' do
View
9 lib/sinatra/base.rb
@@ -339,6 +339,10 @@ def liquid(template, options={}, locals={})
render :liquid, template, options, locals
end
+ def markdown(template, options={}, locals={})
+ render :markdown, template, options, locals
+ end
+
private
def render(engine, data, options={}, locals={}, &block)
# merge app-level options
@@ -379,6 +383,11 @@ def compile_template(engine, data, options, views)
template.new(path, line.to_i, options) { body }
else
path = ::File.join(views, "#{data}.#{engine}")
+ Tilt.mappings.each do |ext, klass|
+ break if File.exists?(path)
+ next unless klass == template
+ path = ::File.join(views, "#{data}.#{ext}")
+ end
template.new(path, 1, options)
end
when data.is_a?(Proc) || data.is_a?(String)
View
1 sinatra.gemspec
@@ -87,6 +87,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'erubis'
s.add_development_dependency 'less'
s.add_development_dependency 'liquid'
+ s.add_development_dependency 'rdiscount'
s.has_rdoc = true
s.homepage = "http://sinatra.rubyforge.org"
View
34 test/markdown_test.rb
@@ -0,0 +1,34 @@
+require File.dirname(__FILE__) + '/helper'
+
+begin
+require 'rdiscount'
+
+class MarkdownTest < Test::Unit::TestCase
+ def markdown_app(&block)
+ mock_app do
+ set :views, File.dirname(__FILE__) + '/views'
+ get '/', &block
+ end
+ get '/'
+ end
+
+ it 'renders inline markdown strings' do
+ markdown_app { markdown '# Hiya' }
+ assert ok?
+ assert_equal "<h1>Hiya</h1>\n", body
+ end
+
+ it 'renders .markdown files in views path' do
+ markdown_app { markdown :hello }
+ assert ok?
+ assert_equal "<h1>Hello From Markdown</h1>\n", body
+ end
+
+ it "raises error if template not found" do
+ mock_app { get('/') { markdown :no_such_template } }
+ assert_raise(Errno::ENOENT) { get('/') }
+ end
+end
+rescue
+ warn "#{$!.to_s}: skipping markdown tests"
+end
View
1 test/views/hello.md
@@ -0,0 +1 @@
+# Hello From Markdown

0 comments on commit 970169b

Please sign in to comment.