/scalate-jruby/src/main/resources/haml-3.0.25/lib/haml/template.rb

http://github.com/scalate/scalate · Ruby · 97 lines · 61 code · 12 blank · 24 comment · 12 complexity · d7b1a1936fd3dcfd53a42e4d090fac45 MD5 · raw file

  1. require 'haml/template/options'
  2. require 'haml/engine'
  3. require 'haml/helpers/action_view_mods'
  4. require 'haml/helpers/action_view_extensions'
  5. if defined?(ActionPack::VERSION::STRING) &&
  6. ActionPack::VERSION::STRING == "2.3.6"
  7. raise "Haml does not support Rails 2.3.6. Please upgrade to 2.3.7 or later."
  8. end
  9. module Haml
  10. # The class that keeps track of the global options for Haml within Rails.
  11. module Template
  12. # Enables integration with the Rails 2.2.5+ XSS protection,
  13. # if it's available and enabled.
  14. #
  15. # @return [Boolean] Whether the XSS integration was enabled.
  16. def try_enabling_xss_integration
  17. return false unless (ActionView::Base.respond_to?(:xss_safe?) && ActionView::Base.xss_safe?) ||
  18. # We check for ActiveSupport#on_load here because if we're loading Haml that way, it means:
  19. # A) we're in Rails 3 so XSS support is always on, and
  20. # B) we might be in Rails 3 beta 3 where the load order is broken and xss_safe? is undefined
  21. (defined?(ActiveSupport) && Haml::Util.has?(:public_method, ActiveSupport, :on_load))
  22. Haml::Template.options[:escape_html] = true
  23. Haml::Util.module_eval {def rails_xss_safe?; true; end}
  24. require 'haml/helpers/xss_mods'
  25. Haml::Helpers.send(:include, Haml::Helpers::XssMods)
  26. Haml::Precompiler.module_eval do
  27. def precompiled_method_return_value_with_haml_xss
  28. "::Haml::Util.html_safe(#{precompiled_method_return_value_without_haml_xss})"
  29. end
  30. alias_method :precompiled_method_return_value_without_haml_xss, :precompiled_method_return_value
  31. alias_method :precompiled_method_return_value, :precompiled_method_return_value_with_haml_xss
  32. end
  33. true
  34. end
  35. end
  36. end
  37. unless Haml::Util.rails_env == "development"
  38. Haml::Template.options[:ugly] ||= true
  39. end
  40. if Haml::Util.ap_geq_3?
  41. Haml::Template.options[:format] ||= :html5
  42. end
  43. # Decide how we want to load Haml into Rails.
  44. # Patching was necessary for versions <= 2.0.1,
  45. # but we can make it a normal handler for higher versions.
  46. if defined?(ActionView::TemplateHandler) ||
  47. (defined?(ActionView::Template) && defined?(ActionView::Template::Handler))
  48. require 'haml/template/plugin'
  49. else
  50. require 'haml/template/patch'
  51. end
  52. # Enable XSS integration. Use Rails' after_initialize method
  53. # so that integration will be checked after the rails_xss plugin is loaded
  54. # (for Rails 2.3.* where it's not enabled by default).
  55. #
  56. # If we're running under Rails 3, though, we don't want to use after_intialize,
  57. # since Haml loading has already been deferred via ActiveSupport.on_load.
  58. if defined?(Rails.configuration.after_initialize) &&
  59. !(defined?(ActiveSupport) && Haml::Util.has?(:public_method, ActiveSupport, :on_load))
  60. Rails.configuration.after_initialize {Haml::Template.try_enabling_xss_integration}
  61. else
  62. Haml::Template.try_enabling_xss_integration
  63. end
  64. if Haml::Util.rails_root
  65. # Update init.rb to the current version
  66. # if it's out of date.
  67. #
  68. # We can probably remove this as of v1.9,
  69. # because the new init file is sufficiently flexible
  70. # to not need updating.
  71. rails_init_file = File.join(Haml::Util.rails_root, 'vendor', 'plugins', 'haml', 'init.rb')
  72. haml_init_file = Haml::Util.scope('init.rb')
  73. begin
  74. if File.exists?(rails_init_file)
  75. require 'fileutils'
  76. FileUtils.cp(haml_init_file, rails_init_file) unless FileUtils.cmp(rails_init_file, haml_init_file)
  77. end
  78. rescue SystemCallError
  79. Haml::Util.haml_warn <<END
  80. HAML WARNING:
  81. #{rails_init_file} is out of date and couldn't be automatically updated.
  82. Please run `haml --rails #{File.expand_path(Haml::Util.rails_root)}' to update it.
  83. END
  84. end
  85. end