File: etanni.rb

package info (click to toggle)
libinnate-ruby 2010.07-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 812 kB
  • ctags: 621
  • sloc: ruby: 4,242; makefile: 2
file content (39 lines) | stat: -rw-r--r-- 1,043 bytes parent folder | download | duplicates (2)
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
35
36
37
38
39
module Innate
  module View
    module Etanni
      def self.call(action, string)
        etanni = View.compile(string) do |str|
          filename = action.view || action.method
          Innate::Etanni.new(str, filename)
        end
        html = etanni.result(action.instance)
        return html, Response.mime_type
      end
    end
  end

  class Etanni
    SEPARATOR = "E69t116A65n110N78i105S83e101P80a97R82a97T84o111R82"
    CHOMP = "<<#{SEPARATOR}.chomp!"
    START = "\n_out_ << #{CHOMP}\n"
    STOP = "\n#{SEPARATOR}\n"
    REPLACEMENT = "#{STOP}\\1#{START}"

    def initialize(template, filename = '<Etanni>')
      @template = template
      @filename = filename
      compile
    end

    def compile(filename = @filename)
      temp = @template.strip
      temp.gsub!(/<\?r\s+(.*?)\s+\?>/m, REPLACEMENT)
      @compiled = eval("Proc.new{ _out_ = [#{CHOMP}]\n#{temp}#{STOP}_out_.join }",
        nil, @filename)
    end

    def result(instance, filename = @filename)
      instance.instance_eval(&@compiled)
    end
  end
end