Add :line option for Haml::Engine

This commit is contained in:
Aman Gupta 2008-05-01 23:54:10 -07:00
parent 6eb4795a32
commit 0517c9c3e5
3 changed files with 25 additions and 4 deletions

View File

@ -890,6 +890,10 @@ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
# so it's really only useful for the user to assign
# when dealing with Haml programatically.
#
# [<tt>:line</tt>] The line offset of the Haml template being parsed.
# This is useful for inline templates, similar to the last
# argument to Kernel#eval
#
# [<tt>:filters</tt>] A hash of filters that can be applied to Haml code.
# The keys are the string names of the filters;
# the values are references to the classes of the filters.

View File

@ -70,6 +70,7 @@ module Haml
'textile' => Haml::Filters::Textile,
'markdown' => Haml::Filters::Markdown },
:filename => '(haml)',
:line => 1,
:ugly => false,
:format => :xhtml,
:escape_html => false
@ -106,7 +107,7 @@ END
precompile
rescue Haml::Error
$!.backtrace.unshift "#{@options[:filename]}:#{@index + $!.line_offset}" if @index
$!.backtrace.unshift "#{@options[:filename]}:#{@index + $!.line_offset + @options[:line] - 1}" if @index
raise
end
@ -164,7 +165,7 @@ END
@haml_buffer = buffer
end
eval(@precompiled, scope, @options[:filename])
eval(@precompiled, scope, @options[:filename], @options[:line])
# Get rid of the current buffer
scope_object.instance_eval do
@ -205,7 +206,7 @@ END
end
eval("Proc.new { |*_haml_locals| _haml_locals = _haml_locals[0] || {};" +
precompiled_with_ambles(local_names) + "}\n", scope, @options[:filename])
precompiled_with_ambles(local_names) + "}\n", scope, @options[:filename], @options[:line])
end
# Defines a method on +object+
@ -247,7 +248,7 @@ END
method = object.is_a?(Module) ? :module_eval : :instance_eval
object.send(method, "def #{name}(_haml_locals = {}); #{precompiled_with_ambles(local_names)}; end",
@options[:filename])
@options[:filename], @options[:line])
end
private

View File

@ -218,6 +218,22 @@ END
# Options tests
def test_filename_and_line
begin
render("\n\n = abc", :filename => 'test', :line => 2)
rescue Exception => e
assert_kind_of Haml::SyntaxError, e
assert_match /test:4/, e.backtrace.first
end
begin
render("\n\n= 123\n\n= nil[]", :filename => 'test', :line => 2)
rescue Exception => e
assert_kind_of NoMethodError, e
assert_match /test:6/, e.backtrace.first
end
end
def test_stop_eval
assert_equal("", render("= 'Hello'", :suppress_eval => true))
assert_equal("", render("- puts 'foo'", :suppress_eval => true))