File: nokogiri.rb

package info (click to toggle)
ruby-tilt 2.0.0%2Breally1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 476 kB
  • ctags: 411
  • sloc: ruby: 3,546; makefile: 5
file content (43 lines) | stat: -rw-r--r-- 961 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
40
41
42
43
require 'tilt/template'

module Tilt
  # Nokogiri template implementation. See:
  # http://nokogiri.org/
  class NokogiriTemplate < Template
    DOCUMENT_HEADER = /^<\?xml version=\"1\.0\"\?>\n?/
    self.default_mime_type = 'text/xml'

    def self.engine_initialized?
      defined? ::Nokogiri
    end

    def initialize_engine
      require_template_library 'nokogiri'
    end

    def prepare; end

    def evaluate(scope, locals)
      if data.respond_to?(:to_str)
        wrapper = proc { yield.sub(DOCUMENT_HEADER, "") } if block_given?
        super(scope, locals, &wrapper)
      else
        ::Nokogiri::XML::Builder.new.tap(&data).to_xml
      end
    end

    def precompiled_preamble(locals)
      return super if locals.include? :xml
      "xml = ::Nokogiri::XML::Builder.new { |xml| }\n#{super}"
    end

    def precompiled_postamble(locals)
      "xml.to_xml"
    end

    def precompiled_template(locals)
      data.to_str
    end
  end
end