Skip to content

Commit

Permalink
template for sinatra extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Wlodek Bzyl committed Apr 14, 2009
0 parents commit dc30d1f
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.gem
Empty file added LICENSE
Empty file.
13 changes: 13 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Sinatra Extensions

[Typo](http://www.sinatrarb.com/extensions.html):

require 'sinatra/base'
require 'rack/utils' # added
module Sinatra
module HTMLEscapeHelper
def h(text)
Rake::Utils.escape_html(text) #<-- Rack::Utils.escape_html(text)
end

Empty file added Rakefile
Empty file.
7 changes: 7 additions & 0 deletions example/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rubygems'
require 'sinatra'
require 'sinatra/rdiscount'

get "/hello" do
h "1 < 2" # => "1 &lt; 2"
end
39 changes: 39 additions & 0 deletions lib/sinatra/rdiscount.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'sinatra/base'
require 'rack/utils'

module Sinatra
module HTMLEscapeHelper
def h(text)
Rack::Utils.escape_html(text)
end
end

helpers HTMLEscapeHelper
end

module Sinatra
module RDiscountTemplate

def rdiscount(template, options={}, locals={})
require 'rdiscount' unless defined? ::RDiscount
render :rdiscount, template, options, locals
end

private

def render_rdiscount(template, data, options, locals, &block)
original_out_buf = @_out_buf
data = data.call if data.kind_of? Proc
instance = ::ERB.new(data, nil, nil, '@_out_buf')
locals_assigns = locals.to_a.collect { |k,v| "#{k} = locals[:#{k}]" }

src = "#{locals_assigns.join("\n")}\n#{instance.src}"
eval src, binding, '(__ERB__)', locals_assigns.length + 1
@_out_buf, result = original_out_buf, @_out_buf
result
end

end

helpers RDiscountTemplate
end
34 changes: 34 additions & 0 deletions sinatra-rdiscount.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-

Gem::Specification.new do |s|
s.specification_version = 2 if s.respond_to? :specification_version=
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=

s.name = 'sinatra-rdiscount'
s.version = '0.0.2'
s.date = '2009-03-31'

s.description = "RDiscount templates for Sinatra applications"
s.summary = "RDiscount..."

s.authors = ["Włodek Bzyl"]
s.email = "matwb@univ.gda.pl"

s.files = %w[
LICENSE
README.markdown
Rakefile
lib/sinatra/rdiscount.rb
sinatra-rdiscount.gemspec
test/spec_sinatra_rdiscount.rb
]
s.require_paths = %w[lib]

s.add_dependency 'rdiscount', '>= 1.3.4'

s.has_rdoc = false
s.homepage = "http://sinatra.rubyforge.org"

s.rubyforge_project = 'sinatra-rdiscount'
s.rubygems_version = '1.3.1'
end
Empty file added test/spec_sinatra_rdiscount.rb
Empty file.

0 comments on commit dc30d1f

Please sign in to comment.