nokogiri.rb 913 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. require 'tilt/template'
  2. module Tilt
  3. # Nokogiri template implementation. See:
  4. # http://nokogiri.org/
  5. class NokogiriTemplate < Template
  6. self.default_mime_type = 'text/xml'
  7. def self.engine_initialized?
  8. defined? ::Nokogiri
  9. end
  10. def initialize_engine
  11. require_template_library 'nokogiri'
  12. end
  13. def prepare; end
  14. def evaluate(scope, locals, &block)
  15. block &&= proc { yield.gsub(/^<\?xml version=\"1\.0\"\?>\n?/, "") }
  16. if data.respond_to?(:to_str)
  17. super(scope, locals, &block)
  18. else
  19. ::Nokogiri::XML::Builder.new.tap(&data).to_xml
  20. end
  21. end
  22. def precompiled_preamble(locals)
  23. return super if locals.include? :xml
  24. "xml = ::Nokogiri::XML::Builder.new { |xml| }\n#{super}"
  25. end
  26. def precompiled_postamble(locals)
  27. "xml.to_xml"
  28. end
  29. def precompiled_template(locals)
  30. data.to_str
  31. end
  32. end
  33. end