Sha256: 58d39866cef1d2b417c4872d1ee622665ed4135e58cef595ded7557dd48a2352

Contents?: true

Size: 802 Bytes

Versions: 5

Compression:

Stored size: 802 Bytes

Contents

require 'erubis'
require 'rdiscount'

require 'sinatra/base'

module Sinatra
  module RDiscount

    def rdiscount(template, options={}, locals={})
      render :rdiscount, template, options, locals
    end
    
  private
    
    def render_rdiscount(data, options, locals, &block)
      if block_given?
        # render layout
        instance = ::Erubis::Eruby.new(data)
      else
        # render template
        markdown = ::RDiscount.new(data)
        html = markdown.to_html
        instance = ::Erubis::Eruby.new(html, :pattern => '\{% %\}')
      end
      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
    end

  end

  helpers RDiscount
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
wbzyl-sinatra-rdiscount-0.1.5 lib/sinatra/rdiscount.rb
wbzyl-sinatra-rdiscount-0.10.1 lib/sinatra/rdiscount.rb
sinatra-rdiscount-1.0.0.1 lib/sinatra/rdiscount.rb
sinatra-rdiscount-1.0.0.0 lib/sinatra/rdiscount.rb
sinatra-rdiscount-0.10.2 lib/sinatra/rdiscount.rb