Sha256: 469dd5109a1516e1db2b0ea8219d4e0edb71024b1315df87214c4580298e2b7a

Contents?: true

Size: 1.65 KB

Versions: 15

Compression:

Stored size: 1.65 KB

Contents

module Ro
  class Template
    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

    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

    fattr :path

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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ro-1.4.6 lib/ro/template.rb
ro-1.4.4 lib/ro/template.rb
ro-1.4.3 lib/ro/template.rb
ro-1.4.2 lib/ro/template.rb
ro-1.4.1 lib/ro/template.rb
ro-1.4.0 lib/ro/template.rb
ro-1.3.8 lib/ro/template.rb
ro-1.3.7 lib/ro/template.rb
ro-1.3.6 lib/ro/template.rb
ro-1.3.5 lib/ro/template.rb
ro-1.3.4 lib/ro/template.rb
ro-1.3.3 lib/ro/template.rb
ro-1.3.2 lib/ro/template.rb
ro-1.3.1 lib/ro/template.rb
ro-1.2.0 lib/ro/template.rb