Skip to content

Commit

Permalink
[Haml] Fix html2haml for Ruby 1.9.
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Jul 14, 2009
1 parent 8c7f5e0 commit 4d4a95a
Showing 1 changed file with 64 additions and 45 deletions.
109 changes: 64 additions & 45 deletions lib/haml/html.rb
Expand Up @@ -2,9 +2,60 @@

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.
Expand Down Expand Up @@ -46,67 +97,35 @@ def render
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
Expand Down Expand Up @@ -137,17 +156,17 @@ def to_haml(tabs, options)
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:'
Expand Down

0 comments on commit 4d4a95a

Please sign in to comment.