public
Description: The core of Ramaze
Homepage: http://ramaze.net
Clone URL: git://github.com/manveru/innate.git
Click here to lend your support to: innate and make a donation at www.pledgie.com !
innate / lib / innate / view / etanni.rb
100644 34 lines (29 sloc) 0.842 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Innate
  module View
    module Etanni
      def self.call(action, string)
        etanni = View.compile(string){|str| Innate::Etanni.new(str) }
        html = etanni.result(action.binding, (action.view || action.method))
        return html, 'text/html'
      end
    end
  end
 
  class Etanni
    SEPARATOR = "E69t116A65n110N78i105S83e101P80a97R82a97T84o111R82"
    START = "\n<<#{SEPARATOR}.chomp\n"
    STOP = "\n#{SEPARATOR}\n"
    ADD = "_out_ << "
 
    def initialize(template)
      @template = template
      compile
    end
 
    def compile
      temp = @template.dup
      temp.gsub!(/<\?r\s+(.*?)\s+\?>/m, "#{STOP} \\1; #{ADD} #{START}")
      @compiled = "_out_ = #{START} #{temp} #{STOP} _out_"
    end
 
    def result(binding, filename = '<Etanni>')
      eval(@compiled, binding, filename).to_s.strip
    end
  end
end