File: nokogiri.rb

package info (click to toggle)
ruby-tilt 2.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 632 kB
  • sloc: ruby: 4,975; makefile: 7
file content (48 lines) | stat: -rw-r--r-- 942 bytes parent folder | download
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
44
45
46
47
48
# frozen_string_literal: true

# = Nokogiri
#
# Nokogiri template implementation.
#
# === See also
#
# * http://nokogiri.org/
#
# === Related module
#
# * Tilt::NokogiriTemplate

require_relative 'template'
require 'nokogiri'

module Tilt
  class NokogiriTemplate < Template
    DOCUMENT_HEADER = /\A<\?xml version=\"1\.0\"\?>\n?/
    self.default_mime_type = 'text/xml'

    def evaluate(scope, locals)
      if @data.respond_to?(:to_str)
        if block_given?
          super(scope, locals){yield.sub(DOCUMENT_HEADER, "")}
        else
          super
        end
      else
        ::Nokogiri::XML::Builder.new(&@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