diff --git a/CHANGELOG.md b/CHANGELOG.md index 842b8566..aa4ff693 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ * Make escape_once respect hexadecimal references. (Matt Wildig) * General performance and memory usage improvements. (Akira Matsuda) * Don't treat the 'data' attribute specially when merging attribute hashes. (Matt Wildig and Norman Clarke) +* Add a tracing option. When enabled, Haml will output a data-trace attribute on each tag showing the path + to the source Haml file from which it was generated. Thanks [Alex Babkin](https://github.com/ababkin). ## 4.0.5 diff --git a/lib/haml/compiler.rb b/lib/haml/compiler.rb index 29ff0d0e..4fc3de35 100644 --- a/lib/haml/compiler.rb +++ b/lib/haml/compiler.rb @@ -500,7 +500,9 @@ END end def prerender_tag(name, self_close, attributes) - # TODO: consider just passing in the damn options here + if @options[:trace] + attributes.merge!({"data-trace" => @options.filename.split('/views').last + ":" + @node.line.to_s}) + end attributes_string = Compiler.build_attributes( @options.html?, @options.attr_wrapper, @options.escape_attrs, @options.hyphenate_data_attrs, attributes) "<#{name}#{attributes_string}#{self_close && @options.xhtml? ? ' /' : ''}>" diff --git a/lib/haml/options.rb b/lib/haml/options.rb index f5a25fdb..c444e5a5 100644 --- a/lib/haml/options.rb +++ b/lib/haml/options.rb @@ -23,7 +23,8 @@ module Haml :ugly => false, :cdata => false, :parser_class => ::Haml::Parser, - :compiler_class => ::Haml::Compiler + :compiler_class => ::Haml::Compiler, + :trace => false } @valid_formats = [:html4, :html5, :xhtml] @@ -168,6 +169,14 @@ module Haml # The compiler class to use. Defaults to Haml::Compiler. attr_accessor :compiler_class + # Enable template tracing. If true, it will add a 'data-trace' attribute to + # each tag generated by Haml. The value of the attribute will be the + # source template name and the line number from which the tag was generated, + # separated by a colon. On Rails applications, the path given will be a + # relative path as from the views directory. On non-Rails applications, + # the path will be the full path. + attr_accessor :trace + def initialize(values = {}, &block) defaults.each {|k, v| instance_variable_set :"@#{k}", v} values.each {|k, v| send("#{k}=", v) if defaults.has_key?(k) && !v.nil?} diff --git a/test/engine_test.rb b/test/engine_test.rb index 62b511c1..b105888a 100644 --- a/test/engine_test.rb +++ b/test/engine_test.rb @@ -1986,6 +1986,11 @@ HAML end end + def test_tracing + result = render('%p', :trace => true, :filename => 'foo').strip + assert_equal "
", result + end + private def assert_valid_encoding_comment(comment)