1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

Add markdown helper method. Tilt supports markdown 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.

This commit is contained in:
Konstantin Haase 2010-09-11 14:34:41 +02:00
parent 4db91f3c3c
commit 970169b1fb
6 changed files with 94 additions and 0 deletions

View file

@ -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 file

@ -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 file

@ -339,6 +339,10 @@ module Sinatra
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 @@ module Sinatra
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 file

@ -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"

34
test/markdown_test.rb Normal file
View file

@ -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

1
test/views/hello.md Normal file
View file

@ -0,0 +1 @@
# Hello From Markdown