diff --git a/lib/haml/html.rb b/lib/haml/html.rb index dbc4b13c..e116fc5e 100644 --- a/lib/haml/html.rb +++ b/lib/haml/html.rb @@ -2,9 +2,60 @@ require File.dirname(__FILE__) + '/../haml' require 'haml/engine' require 'rubygems' -require 'hpricot' require 'cgi' +module Haml + class HTML + # A module containing utility methods that every Hpricot node + # should have. + module Node + # Returns the Haml representation of the given node. + # + # @param tabs [Fixnum] The indentation level of the resulting Haml. + # @option options (see Haml::HTML#initialize) + def to_haml(tabs, options) + parse_text(self.to_s, tabs) + end + + private + + def tabulate(tabs) + ' ' * tabs + end + + def parse_text(text, tabs) + text.strip! + if text.empty? + String.new + else + lines = text.split("\n") + + lines.map do |line| + line.strip! + "#{tabulate(tabs)}#{'\\' if Haml::Engine::SPECIAL_CHARACTERS.include?(line[0])}#{line}\n" + end.join + end + end + end + end +end + +# Haml monkeypatches various Hpricot classes +# to add methods for conversion to Haml. +module Hpricot + # @see Hpricot + module Node + include Haml::HTML::Node + end + + # @see Hpricot + class BaseEle + include Haml::HTML::Node + end +end + +require 'hpricot' + module Haml # Converts HTML documents into Haml templates. # Depends on [Hpricot](http://code.whytheluckystiff.net/hpricot/) for HTML parsing. @@ -46,67 +97,35 @@ module Haml end alias_method :to_haml, :render - # Haml monkeypatches various Hpricot classes - # to add methods for conversion to Haml. - module ::Hpricot::Node - # Returns the Haml representation of the given node. - # - # @param tabs [Fixnum] The indentation level of the resulting Haml. - # @option options (see Haml::HTML#initialize) - def to_haml(tabs, options) - parse_text(self.to_s, tabs) - end - - private - - def tabulate(tabs) - ' ' * tabs - end - - def parse_text(text, tabs) - text.strip! - if text.empty? - String.new - else - lines = text.split("\n") - - lines.map do |line| - line.strip! - "#{tabulate(tabs)}#{'\\' if Haml::Engine::SPECIAL_CHARACTERS.include?(line[0])}#{line}\n" - end.join - end - end - end - TEXT_REGEXP = /^(\s*).*$/ - # @see Hpricot::Node + # @see Hpricot class ::Hpricot::Doc - # @see Hpricot::Node#to_haml + # @see Haml::HTML::Node#to_haml def to_haml(tabs, options) (children || []).inject('') {|s, c| s << c.to_haml(0, options)} end end - # @see Hpricot::Node + # @see Hpricot class ::Hpricot::XMLDecl - # @see Hpricot::Node#to_haml + # @see Haml::HTML::Node#to_haml def to_haml(tabs, options) "#{tabulate(tabs)}!!! XML\n" end end - # @see Hpricot::Node + # @see Hpricot class ::Hpricot::CData - # @see Hpricot::Node#to_haml + # @see Haml::HTML::Node#to_haml def to_haml(tabs, options) "#{tabulate(tabs)}:cdata\n#{parse_text(self.content, tabs + 1)}" end end - # @see Hpricot::Node + # @see Hpricot class ::Hpricot::DocType - # @see Hpricot::Node#to_haml + # @see Haml::HTML::Node#to_haml def to_haml(tabs, options) attrs = public_id.scan(/DTD\s+([^\s]+)\s*([^\s]*)\s*([^\s]*)\s*\/\//)[0] if attrs == nil @@ -137,17 +156,17 @@ module Haml end end - # @see Hpricot::Node + # @see Hpricot class ::Hpricot::Comment - # @see Hpricot::Node#to_haml + # @see Haml::HTML::Node#to_haml def to_haml(tabs, options) "#{tabulate(tabs)}/\n#{parse_text(self.content, tabs + 1)}" end end - # @see Hpricot::Node + # @see Hpricot class ::Hpricot::Elem - # @see Hpricot::Node#to_haml + # @see Haml::HTML::Node#to_haml def to_haml(tabs, options) output = "#{tabulate(tabs)}" if options[:rhtml] && name[0...5] == 'haml:' diff --git a/lib/haml/precompiler.rb b/lib/haml/precompiler.rb index 3df8291f..e395ceea 100644 --- a/lib/haml/precompiler.rb +++ b/lib/haml/precompiler.rb @@ -571,7 +571,10 @@ END attributes = {} scanner.scan(/\(\s*/) - until (name, value = parse_new_attribute(scanner)).first.nil? + loop do + name, value = parse_new_attribute(scanner) + break if name.nil? + if name == false text = (Haml::Shared.balance(line, ?(, ?)) || [line]).first raise Haml::SyntaxError.new("Invalid attribute list: #{text.inspect}.", last_line - 1) diff --git a/lib/sass/script/funcall.rb b/lib/sass/script/funcall.rb index de5f396f..d05d2cf9 100644 --- a/lib/sass/script/funcall.rb +++ b/lib/sass/script/funcall.rb @@ -42,7 +42,7 @@ module Sass return Functions::EvaluationContext.new(environment.options).send(name, *args) rescue ArgumentError => e - raise e unless e.backtrace.first =~ /:in `(#{name}|perform)'$/ + raise e unless e.backtrace.first =~ /:in `(block in )?(#{name}|perform)'$/ raise Sass::SyntaxError.new("#{e.message} for `#{name}'") end end diff --git a/test/haml/template_test.rb b/test/haml/template_test.rb index 03012be8..859f767e 100644 --- a/test/haml/template_test.rb +++ b/test/haml/template_test.rb @@ -140,8 +140,10 @@ class TemplateTest < Test::Unit::TestCase end def test_action_view_templates_render_correctly - @base.content_for(:layout) {'Lorem ipsum dolor sit amet'} - assert_renders_correctly 'content_for_layout' + @base.with_output_buffer("") do + @base.content_for(:layout) {'Lorem ipsum dolor sit amet'} + assert_renders_correctly 'content_for_layout' + end end def test_instance_variables_should_work_inside_templates diff --git a/test/test_helper.rb b/test/test_helper.rb index d1c3700c..8a1f3e22 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -7,7 +7,7 @@ $:.unshift lib_dir unless $:.include?(lib_dir) require 'haml' require 'sass' -Sass::RAILS_LOADED = true +Sass::RAILS_LOADED = true unless defined?(Sass::RAILS_LOADED) # required because of Sass::Plugin unless defined? RAILS_ROOT