PageRenderTime 35ms CodeModel.GetById 23ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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