Class: Ro::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/ro/template.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Template

Returns a new instance of Template.



73
74
75
# File 'lib/ro/template.rb', line 73

def initialize(path)
  @path = Ro.realpath(path)
end

Class Method Details

.render(path, node = nil) ⇒ Object



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
# File 'lib/ro/template.rb', line 3

def Template.render(path, node = nil)
  parts = File.basename(path).split('.')
  base = parts.shift
  exts = parts.reverse

  content = IO.binread(path).force_encoding('utf-8')

  loop do
    break if exts.empty?
    ext = exts.shift

    case ext.to_s.downcase
      when 'erb', 'eruby'
        content = Ro.erb(content, node)

      when 'yml'
        content = YAML.load(content)

      else
        tilt = Tilt[ext] || Tilt['txt']

        if tilt.nil?
          content
        else
          content = tilt.new{ content }.render(node)
        end
    end
  end

  content
end

.render_source(path, node = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ro/template.rb', line 35

def Template.render_source(path, node = nil)
  parts = File.basename(path).split('.')
  base = parts.shift
  exts = parts.reverse

  content = IO.binread(path).force_encoding('utf-8')

  loop do
    break if exts.empty?
    ext = exts.shift

    if exts.empty?
      code = content
      language = ext
      content = 
        begin
          ::Pygments.highlight(code, :lexer => language, :options => {:encoding => 'utf-8'})
        rescue
          content
        end
    else
      case ext.to_s.downcase
        when 'erb', 'eruby'
          content = Ro.erb(content, node)
        when 'yml'
          content = YAML.load(content)
        else
          tilt = Tilt[ext].new{ content  }
          content = tilt.render(node)
      end
    end
  end

  content
end