1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Merge branch 'stable'

This commit is contained in:
Nathan Weizenbaum 2009-07-13 23:17:40 -07:00
commit 977d47fe67
5 changed files with 74 additions and 50 deletions

View file

@ -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:'

View file

@ -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)

View file

@ -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

View file

@ -140,9 +140,11 @@ class TemplateTest < Test::Unit::TestCase
end
def test_action_view_templates_render_correctly
@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
@base.instance_variable_set("@content_for_layout", 'something')

View file

@ -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