2006-12-03 18:56:47 -05:00
|
|
|
require 'haml/engine'
|
2006-09-29 14:39:13 -04:00
|
|
|
|
|
|
|
module Haml
|
2009-04-30 15:43:21 -04:00
|
|
|
# The class that keeps track of the global options for Haml within Rails.
|
2009-04-22 16:45:26 -04:00
|
|
|
module Template
|
|
|
|
extend self
|
2006-11-04 03:35:06 -05:00
|
|
|
|
2009-04-22 16:45:26 -04:00
|
|
|
@options = {}
|
2009-04-30 15:43:21 -04:00
|
|
|
# The options hash for Haml when used within Rails.
|
2009-06-18 16:40:57 -04:00
|
|
|
# See {file:HAML_REFERENCE.md#haml_options the Haml options documentation}.
|
2009-04-30 15:43:21 -04:00
|
|
|
#
|
|
|
|
# @return [Hash<Symbol, Object>]
|
2009-04-22 16:45:26 -04:00
|
|
|
attr_accessor :options
|
2009-10-29 16:27:24 -04:00
|
|
|
|
|
|
|
# Enables integration with the Rails 2.2.5+ XSS protection,
|
|
|
|
# if it's available and enabled.
|
|
|
|
#
|
|
|
|
# @return [Boolean] Whether the XSS integration was enabled.
|
|
|
|
def try_enabling_xss_integration
|
|
|
|
return false unless ActionView::Base.respond_to?(:xss_safe?) && ActionView::Base.xss_safe?
|
|
|
|
|
|
|
|
Haml::Template.options[:escape_html] = true
|
|
|
|
|
|
|
|
Haml::Util.module_eval {def rails_xss_safe?; true; end}
|
|
|
|
|
|
|
|
require 'haml/helpers/xss_mods'
|
|
|
|
Haml::Helpers.send(:include, Haml::Helpers::XssMods)
|
|
|
|
|
2009-10-29 17:14:30 -04:00
|
|
|
Haml::Precompiler.module_eval do
|
|
|
|
def precompiled_method_return_value_with_haml_xss
|
|
|
|
"(#{precompiled_method_return_value_without_haml_xss}).html_safe!"
|
|
|
|
end
|
|
|
|
alias_method :precompiled_method_return_value_without_haml_xss, :precompiled_method_return_value
|
|
|
|
alias_method :precompiled_method_return_value, :precompiled_method_return_value_with_haml_xss
|
|
|
|
end
|
|
|
|
|
2009-10-29 16:27:24 -04:00
|
|
|
true
|
|
|
|
end
|
2006-09-29 14:39:13 -04:00
|
|
|
end
|
2006-10-14 18:24:53 -04:00
|
|
|
end
|
|
|
|
|
2009-07-04 21:14:15 -04:00
|
|
|
if defined?(RAILS_ENV) && RAILS_ENV == "production"
|
|
|
|
Haml::Template.options[:ugly] = true
|
|
|
|
end
|
|
|
|
|
2007-12-15 16:07:27 -05:00
|
|
|
# Decide how we want to load Haml into Rails.
|
|
|
|
# Patching was necessary for versions <= 2.0.1,
|
|
|
|
# but we can make it a normal handler for higher versions.
|
|
|
|
if defined?(ActionView::TemplateHandler)
|
|
|
|
require 'haml/template/plugin'
|
|
|
|
else
|
|
|
|
require 'haml/template/patch'
|
2006-10-14 18:24:53 -04:00
|
|
|
end
|
2008-01-06 21:38:19 -05:00
|
|
|
|
2009-10-29 16:27:24 -04:00
|
|
|
# Enable XSS integration. Use Rails' after_initialize method if possible
|
|
|
|
# so that integration will be checked after the rails_xss plugin is loaded
|
|
|
|
# (for Rails 2.3.* where it's not enabled by default).
|
|
|
|
if defined?(Rails.configuration.after_initialize)
|
|
|
|
Rails.configuration.after_initialize {Haml::Template.try_enabling_xss_integration}
|
|
|
|
else
|
|
|
|
Haml::Template.try_enabling_xss_integration
|
2009-10-16 01:31:24 -04:00
|
|
|
end
|
|
|
|
|
2009-11-04 22:01:39 -05:00
|
|
|
if Haml::Util.rails_root
|
2008-01-07 00:04:46 -05:00
|
|
|
# Update init.rb to the current version
|
|
|
|
# if it's out of date.
|
|
|
|
#
|
|
|
|
# We can probably remove this as of v1.9,
|
|
|
|
# because the new init file is sufficiently flexible
|
|
|
|
# to not need updating.
|
2009-11-04 22:01:39 -05:00
|
|
|
rails_init_file = File.join(Haml::Util.rails_root, 'vendor', 'plugins', 'haml', 'init.rb')
|
2009-04-30 17:22:05 -04:00
|
|
|
haml_init_file = Haml::Util.scope('init.rb')
|
2008-05-24 15:19:18 -04:00
|
|
|
begin
|
|
|
|
if File.exists?(rails_init_file)
|
|
|
|
require 'fileutils'
|
|
|
|
FileUtils.cp(haml_init_file, rails_init_file) unless FileUtils.cmp(rails_init_file, haml_init_file)
|
|
|
|
end
|
|
|
|
rescue SystemCallError
|
|
|
|
warn <<END
|
|
|
|
HAML WARNING:
|
|
|
|
#{rails_init_file} is out of date and couldn't be automatically updated.
|
2009-11-04 22:01:39 -05:00
|
|
|
Please run `haml --rails #{File.expand_path(Haml::Util.rails_root)}' to update it.
|
2008-05-24 15:19:18 -04:00
|
|
|
END
|
2008-01-07 00:04:46 -05:00
|
|
|
end
|
2008-01-06 21:38:19 -05:00
|
|
|
end
|