mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
More error-throwing infrastructural changes.
git-svn-id: svn://hamptoncatlin.com/haml/trunk@309 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
parent
8ffec90301
commit
7c2ff0288d
3 changed files with 42 additions and 47 deletions
|
@ -27,10 +27,18 @@ module Haml
|
||||||
def add_backtrace_entry(lineno, filename = nil) # :nodoc:
|
def add_backtrace_entry(lineno, filename = nil) # :nodoc:
|
||||||
@haml_line = lineno
|
@haml_line = lineno
|
||||||
@haml_filename = filename
|
@haml_filename = filename
|
||||||
backtrace.unshift "#{filename || '(haml)'}:#{lineno}"
|
self.backtrace ||= []
|
||||||
|
self.backtrace.unshift "#{filename || '(haml)'}:#{lineno}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# SyntaxError is the type of exception thrown when Haml encounters an
|
||||||
|
# ill-formatted document.
|
||||||
|
# It's not particularly interesting, except in that it includes Haml::Error.
|
||||||
|
class SyntaxError < StandardError
|
||||||
|
include Haml::Error
|
||||||
|
end
|
||||||
|
|
||||||
# This is the class where all the parsing and processing of the Haml
|
# This is the class where all the parsing and processing of the Haml
|
||||||
# template is done. It can be directly used by the user by creating a
|
# template is done. It can be directly used by the user by creating a
|
||||||
# new instance and calling <tt>to_html</tt> to render the template. For example:
|
# new instance and calling <tt>to_html</tt> to render the template. For example:
|
||||||
|
@ -169,9 +177,13 @@ module Haml
|
||||||
# flattened block. -1 signifies that there is no such block.
|
# flattened block. -1 signifies that there is no such block.
|
||||||
@flat_spaces = -1
|
@flat_spaces = -1
|
||||||
|
|
||||||
|
begin
|
||||||
# Only do the first round of pre-compiling if we really need to.
|
# Only do the first round of pre-compiling if we really need to.
|
||||||
# They might be passing in the precompiled string.
|
# They might be passing in the precompiled string.
|
||||||
do_precompile if @precompiled.nil? && (@precompiled = String.new)
|
do_precompile if @precompiled.nil? && (@precompiled = String.new)
|
||||||
|
rescue Haml::SyntaxError => e
|
||||||
|
e.add_backtrace_entry(@index, @options[:filename])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Processes the template and returns the result as a string.
|
# Processes the template and returns the result as a string.
|
||||||
|
@ -302,11 +314,19 @@ module Haml
|
||||||
push_text line
|
push_text line
|
||||||
end
|
end
|
||||||
when ESCAPE
|
when ESCAPE
|
||||||
|
if block_opened
|
||||||
|
raise SyntaxError.new("Illegal Nesting: Nesting within plain text is illegal.")
|
||||||
|
else
|
||||||
push_text line[1..-1]
|
push_text line[1..-1]
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if block_opened
|
||||||
|
raise SyntaxError.new("Illegal Nesting: Nesting within plain text is illegal.")
|
||||||
else
|
else
|
||||||
push_text line
|
push_text line
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Returns whether or not the line is a silent script line with one
|
# Returns whether or not the line is a silent script line with one
|
||||||
# of Ruby's mid-block keywords.
|
# of Ruby's mid-block keywords.
|
||||||
|
@ -375,20 +395,11 @@ module Haml
|
||||||
include Haml::Error
|
include Haml::Error
|
||||||
end
|
end
|
||||||
|
|
||||||
|
lineno = @scope_object.haml_lineno
|
||||||
|
|
||||||
# Get information from the exception and format it so that
|
# Get information from the exception and format it so that
|
||||||
# Rails can understand it.
|
# Rails can understand it.
|
||||||
compile_error = e.message.scan(/\(eval\):([0-9]*):in `[-_a-zA-Z]*': compile error/)[0]
|
compile_error = e.message.scan(/\(eval\):([0-9]*):in `[-_a-zA-Z]*': compile error/)[0]
|
||||||
filename = nil
|
|
||||||
if @scope_object.respond_to? :haml_filename
|
|
||||||
# For some reason that I can't figure out,
|
|
||||||
# @scope_object.methods.include? "haml_filename" && @scope_object.haml_filename
|
|
||||||
# is false when it shouldn't be. Nested if statements work, though.
|
|
||||||
|
|
||||||
if @scope_object.haml_filename
|
|
||||||
filename = "#{@scope_object.haml_filename}.haml"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
lineno = @scope_object.haml_lineno
|
|
||||||
|
|
||||||
if compile_error
|
if compile_error
|
||||||
eval_line = compile_error[0].to_i
|
eval_line = compile_error[0].to_i
|
||||||
|
@ -396,7 +407,7 @@ module Haml
|
||||||
lineno = line_marker.scan(/[0-9]+/)[0].to_i if line_marker
|
lineno = line_marker.scan(/[0-9]+/)[0].to_i if line_marker
|
||||||
end
|
end
|
||||||
|
|
||||||
e.add_backtrace_entry(lineno, filename)
|
e.add_backtrace_entry(lineno, @options[:filename])
|
||||||
raise e
|
raise e
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -58,13 +58,16 @@ module Haml
|
||||||
|
|
||||||
if @view.haml_inline
|
if @view.haml_inline
|
||||||
engine = Haml::Engine.new(template, options)
|
engine = Haml::Engine.new(template, options)
|
||||||
elsif @precompiled = get_precompiled(template)
|
else
|
||||||
|
options[:filename] ||= template
|
||||||
|
if @precompiled = get_precompiled(template)
|
||||||
options[:precompiled] ||= @precompiled
|
options[:precompiled] ||= @precompiled
|
||||||
engine = Haml::Engine.new("", options)
|
engine = Haml::Engine.new("", options)
|
||||||
else
|
else
|
||||||
engine = Haml::Engine.new(File.read(template), options)
|
engine = Haml::Engine.new(File.read(template), options)
|
||||||
set_precompiled(template, engine.precompiled)
|
set_precompiled(template, engine.precompiled)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
yield_proc = @view.instance_eval do
|
yield_proc = @view.instance_eval do
|
||||||
proc { |*name| instance_variable_get("@content_for_#{name.first || 'layout'}") }
|
proc { |*name| instance_variable_get("@content_for_#{name.first || 'layout'}") }
|
||||||
|
@ -101,15 +104,8 @@ end
|
||||||
# here[http://rubyonrails.org/api/classes/ActionView/Base.html].
|
# here[http://rubyonrails.org/api/classes/ActionView/Base.html].
|
||||||
module ActionView
|
module ActionView
|
||||||
class Base # :nodoc:
|
class Base # :nodoc:
|
||||||
attr :haml_filename, true
|
|
||||||
attr :haml_inline
|
attr :haml_inline
|
||||||
|
|
||||||
alias_method :haml_old_render_file, :render_file
|
|
||||||
def render_file(template_path, use_full_path = true, local_assigns = {})
|
|
||||||
@haml_filename = File.basename(template_path)
|
|
||||||
haml_old_render_file(template_path, use_full_path, local_assigns)
|
|
||||||
end
|
|
||||||
|
|
||||||
alias_method :read_template_file_old, :read_template_file
|
alias_method :read_template_file_old, :read_template_file
|
||||||
def read_template_file(template_path, extension)
|
def read_template_file(template_path, extension)
|
||||||
if extension =~ /haml/i
|
if extension =~ /haml/i
|
||||||
|
|
|
@ -111,28 +111,16 @@ class TemplateTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_exceptions_should_work_correctly
|
def test_exceptions_should_work_correctly
|
||||||
template = <<END
|
|
||||||
%p
|
|
||||||
%h1 Hello!
|
|
||||||
= "lots of lines"
|
|
||||||
- raise "Oh no!"
|
|
||||||
%p
|
|
||||||
this is after the exception
|
|
||||||
%strong yes it is!
|
|
||||||
ho ho ho.
|
|
||||||
END
|
|
||||||
@base.haml_filename = "(test)"
|
|
||||||
begin
|
begin
|
||||||
render(template.chomp)
|
Haml::Template.new(@base).render(File.dirname(__FILE__) + '/templates/breakage.haml')
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
assert_equal("(test).haml:4", e.backtrace[0])
|
assert_equal("./test/haml/templates/breakage.haml:4", e.backtrace[0])
|
||||||
end
|
end
|
||||||
|
|
||||||
@base.haml_filename = nil
|
|
||||||
begin
|
begin
|
||||||
render(template.chomp)
|
render("- raise 'oops!'")
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
assert_equal("(haml):4", e.backtrace[0])
|
assert_equal("(haml):1", e.backtrace[0])
|
||||||
end
|
end
|
||||||
|
|
||||||
template = <<END
|
template = <<END
|
||||||
|
@ -140,7 +128,7 @@ END
|
||||||
%h1 Hello!
|
%h1 Hello!
|
||||||
= "lots of lines"
|
= "lots of lines"
|
||||||
= "even more!"
|
= "even more!"
|
||||||
- compile_error(
|
- raise 'oh no!'
|
||||||
%p
|
%p
|
||||||
this is after the exception
|
this is after the exception
|
||||||
%strong yes it is!
|
%strong yes it is!
|
||||||
|
|
Loading…
Reference in a new issue