2007-04-01 08:45:39 +00:00
|
|
|
require File.dirname(__FILE__) + '/../haml'
|
|
|
|
|
|
|
|
require 'haml/engine'
|
|
|
|
require 'rubygems'
|
2007-04-10 00:29:57 +00:00
|
|
|
require 'cgi'
|
2007-04-01 08:45:39 +00:00
|
|
|
|
2009-07-13 23:16:31 -07:00
|
|
|
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!
|
2009-10-05 03:42:15 -07:00
|
|
|
text.gsub!('#{', '\#{') #'
|
2009-10-04 21:36:05 -07:00
|
|
|
return "" if text.empty?
|
|
|
|
|
|
|
|
text.split("\n").map do |line|
|
|
|
|
line.strip!
|
|
|
|
"#{tabulate(tabs)}#{'\\' if Haml::Engine::SPECIAL_CHARACTERS.include?(line[0])}#{line}\n"
|
|
|
|
end.join
|
2009-07-13 23:16:31 -07:00
|
|
|
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'
|
|
|
|
|
2007-04-01 08:45:39 +00:00
|
|
|
module Haml
|
2009-05-01 12:10:17 -07:00
|
|
|
# Converts HTML documents into Haml templates.
|
|
|
|
# Depends on [Hpricot](http://code.whytheluckystiff.net/hpricot/) for HTML parsing.
|
|
|
|
#
|
|
|
|
# Example usage:
|
|
|
|
#
|
|
|
|
# Haml::Engine.new("<a href='http://google.com'>Blat</a>").render
|
|
|
|
# #=> "%a{:href => 'http://google.com'} Blat"
|
2007-04-01 08:45:39 +00:00
|
|
|
class HTML
|
2009-05-01 12:10:17 -07:00
|
|
|
# @param template [String, Hpricot::Node] The HTML template to convert
|
2009-10-04 21:15:16 -07:00
|
|
|
# @option options :erb [Boolean] (false) Whether or not to parse
|
2009-05-01 12:10:17 -07:00
|
|
|
# ERB's `<%= %>` and `<% %>` into Haml's `=` and `-`
|
|
|
|
# @option options :xhtml [Boolean] (false) Whether or not to parse
|
|
|
|
# the HTML strictly as XHTML
|
2007-04-10 00:29:57 +00:00
|
|
|
def initialize(template, options = {})
|
2009-05-01 12:03:13 -07:00
|
|
|
@options = options
|
2007-04-10 00:29:57 +00:00
|
|
|
|
2007-04-01 08:45:39 +00:00
|
|
|
if template.is_a? Hpricot::Node
|
|
|
|
@template = template
|
|
|
|
else
|
2007-04-10 00:29:57 +00:00
|
|
|
if template.is_a? IO
|
|
|
|
template = template.read
|
|
|
|
end
|
|
|
|
|
2009-09-23 15:54:46 -07:00
|
|
|
Haml::Util.check_encoding(template) {|msg, line| raise Haml::Error.new(msg, line)}
|
|
|
|
|
2009-10-04 21:15:16 -07:00
|
|
|
if @options[:erb]
|
2007-04-10 00:29:57 +00:00
|
|
|
match_to_html(template, /<%=(.*?)-?%>/m, 'loud')
|
2008-10-16 23:37:08 +02:00
|
|
|
match_to_html(template, /<%-?(.*?)-?%>/m, 'silent')
|
2007-04-10 00:29:57 +00:00
|
|
|
end
|
2007-12-11 10:30:14 +00:00
|
|
|
|
2009-05-01 12:03:13 -07:00
|
|
|
method = @options[:xhtml] ? Hpricot.method(:XML) : method(:Hpricot)
|
2008-08-03 00:05:36 -04:00
|
|
|
@template = method.call(template.gsub('&', '&'))
|
2007-04-01 08:45:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Processes the document and returns the result as a string
|
|
|
|
# containing the Haml template.
|
|
|
|
def render
|
2009-05-01 12:03:13 -07:00
|
|
|
@template.to_haml(0, @options)
|
2007-04-01 08:45:39 +00:00
|
|
|
end
|
|
|
|
alias_method :to_haml, :render
|
|
|
|
|
|
|
|
TEXT_REGEXP = /^(\s*).*$/
|
|
|
|
|
2009-07-13 23:16:31 -07:00
|
|
|
# @see Hpricot
|
2007-04-01 08:45:39 +00:00
|
|
|
class ::Hpricot::Doc
|
2009-07-13 23:16:31 -07:00
|
|
|
# @see Haml::HTML::Node#to_haml
|
2009-05-01 12:03:13 -07:00
|
|
|
def to_haml(tabs, options)
|
|
|
|
(children || []).inject('') {|s, c| s << c.to_haml(0, options)}
|
2007-04-01 08:45:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-13 23:16:31 -07:00
|
|
|
# @see Hpricot
|
2007-04-01 08:45:39 +00:00
|
|
|
class ::Hpricot::XMLDecl
|
2009-07-13 23:16:31 -07:00
|
|
|
# @see Haml::HTML::Node#to_haml
|
2009-05-01 12:03:13 -07:00
|
|
|
def to_haml(tabs, options)
|
2007-04-01 08:45:39 +00:00
|
|
|
"#{tabulate(tabs)}!!! XML\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-13 23:16:31 -07:00
|
|
|
# @see Hpricot
|
2009-03-26 23:11:20 -07:00
|
|
|
class ::Hpricot::CData
|
2009-07-13 23:16:31 -07:00
|
|
|
# @see Haml::HTML::Node#to_haml
|
2009-05-01 12:03:13 -07:00
|
|
|
def to_haml(tabs, options)
|
2009-03-26 23:11:20 -07:00
|
|
|
"#{tabulate(tabs)}:cdata\n#{parse_text(self.content, tabs + 1)}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-13 23:16:31 -07:00
|
|
|
# @see Hpricot
|
2007-04-01 08:45:39 +00:00
|
|
|
class ::Hpricot::DocType
|
2009-07-13 23:16:31 -07:00
|
|
|
# @see Haml::HTML::Node#to_haml
|
2009-05-01 12:03:13 -07:00
|
|
|
def to_haml(tabs, options)
|
2007-04-01 21:37:00 +00:00
|
|
|
attrs = public_id.scan(/DTD\s+([^\s]+)\s*([^\s]*)\s*([^\s]*)\s*\/\//)[0]
|
2009-09-23 15:54:46 -07:00
|
|
|
raise Haml::SyntaxError.new("Invalid doctype") if attrs == nil
|
2007-04-01 21:37:00 +00:00
|
|
|
|
|
|
|
type, version, strictness = attrs.map { |a| a.downcase }
|
|
|
|
if type == "html"
|
|
|
|
version = "1.0"
|
|
|
|
strictness = "transitional"
|
|
|
|
end
|
|
|
|
|
|
|
|
if version == "1.0" || version.empty?
|
|
|
|
version = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if strictness == 'transitional' || strictness.empty?
|
|
|
|
strictness = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
version = " #{version}" if version
|
|
|
|
if strictness
|
|
|
|
strictness[0] = strictness[0] - 32
|
|
|
|
strictness = " #{strictness}"
|
|
|
|
end
|
|
|
|
|
|
|
|
"#{tabulate(tabs)}!!!#{version}#{strictness}\n"
|
2007-04-01 08:45:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-13 23:16:31 -07:00
|
|
|
# @see Hpricot
|
2007-04-01 08:45:39 +00:00
|
|
|
class ::Hpricot::Comment
|
2009-07-13 23:16:31 -07:00
|
|
|
# @see Haml::HTML::Node#to_haml
|
2009-05-01 12:03:13 -07:00
|
|
|
def to_haml(tabs, options)
|
2007-04-01 08:45:39 +00:00
|
|
|
"#{tabulate(tabs)}/\n#{parse_text(self.content, tabs + 1)}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-13 23:16:31 -07:00
|
|
|
# @see Hpricot
|
2007-04-01 08:45:39 +00:00
|
|
|
class ::Hpricot::Elem
|
2009-07-13 23:16:31 -07:00
|
|
|
# @see Haml::HTML::Node#to_haml
|
2009-05-01 12:03:13 -07:00
|
|
|
def to_haml(tabs, options)
|
2008-04-07 23:09:17 -07:00
|
|
|
output = "#{tabulate(tabs)}"
|
2009-10-04 21:15:16 -07:00
|
|
|
if options[:erb] && name[0...5] == 'haml:'
|
2009-05-01 12:10:17 -07:00
|
|
|
return output + send("haml_tag_#{name[5..-1]}", CGI.unescapeHTML(self.inner_text))
|
2007-04-10 00:29:57 +00:00
|
|
|
end
|
|
|
|
|
2009-05-01 12:03:13 -07:00
|
|
|
output += "%#{name}" unless name == 'div' &&
|
|
|
|
(static_id?(options) || static_classname?(options))
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2007-04-01 08:45:39 +00:00
|
|
|
if attributes
|
2009-05-01 12:03:13 -07:00
|
|
|
if static_id?(options)
|
2008-10-16 11:37:58 +02:00
|
|
|
output += "##{attributes['id']}"
|
|
|
|
remove_attribute('id')
|
|
|
|
end
|
2009-05-01 12:03:13 -07:00
|
|
|
if static_classname?(options)
|
2008-10-16 11:37:58 +02:00
|
|
|
attributes['class'].split(' ').each { |c| output += ".#{c}" }
|
|
|
|
remove_attribute('class')
|
|
|
|
end
|
2009-05-01 12:03:13 -07:00
|
|
|
output += haml_attributes(options) if attributes.length > 0
|
2007-04-01 08:45:39 +00:00
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2009-10-04 21:36:05 -07:00
|
|
|
if children && children.size == 1 && children.first.is_a?(::Hpricot::Text) &&
|
|
|
|
!children.first.to_s.include?("\n")
|
|
|
|
text = children.first.to_haml(tabs + 1, options)
|
|
|
|
return output + " " + text.lstrip unless text.chomp.include?("\n")
|
|
|
|
return output + "\n" + text
|
|
|
|
end
|
|
|
|
|
2009-02-07 02:33:58 -08:00
|
|
|
(self.children || []).inject(output + "\n") do |output, child|
|
2009-05-01 12:03:13 -07:00
|
|
|
output + child.to_haml(tabs + 1, options)
|
2007-04-01 08:45:39 +00:00
|
|
|
end
|
|
|
|
end
|
2007-07-24 09:15:27 +00:00
|
|
|
|
|
|
|
private
|
2008-10-16 11:37:58 +02:00
|
|
|
|
|
|
|
def dynamic_attributes
|
|
|
|
@dynamic_attributes ||= begin
|
2008-11-22 19:17:06 -08:00
|
|
|
Haml::Util.map_hash(attributes) do |name, value|
|
|
|
|
next if value.empty?
|
|
|
|
full_match = nil
|
|
|
|
ruby_value = value.gsub(%r{<haml:loud>\s*(.+?)\s*</haml:loud>}) do
|
|
|
|
full_match = $`.empty? && $'.empty?
|
2009-10-05 03:39:40 -07:00
|
|
|
CGI.unescapeHTML(full_match ? $1: "\#{#{$1}}")
|
2008-10-16 11:37:58 +02:00
|
|
|
end
|
2008-11-22 19:17:06 -08:00
|
|
|
next if ruby_value == value
|
|
|
|
[name, full_match ? ruby_value : %("#{ruby_value}")]
|
2008-10-16 11:37:58 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-05-01 12:10:17 -07:00
|
|
|
|
|
|
|
def haml_tag_loud(text)
|
|
|
|
"= #{text.gsub(/\n\s*/, ' ').strip}\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def haml_tag_silent(text)
|
|
|
|
text.split("\n").map { |line| "- #{line.strip}\n" }.join
|
|
|
|
end
|
|
|
|
|
2009-05-01 12:03:13 -07:00
|
|
|
def static_attribute?(name, options)
|
|
|
|
attributes[name] and !dynamic_attribute?(name, options)
|
2008-10-16 11:37:58 +02:00
|
|
|
end
|
|
|
|
|
2009-05-01 12:03:13 -07:00
|
|
|
def dynamic_attribute?(name, options)
|
2009-10-04 21:15:16 -07:00
|
|
|
options[:erb] and dynamic_attributes.key?(name)
|
2008-10-16 11:37:58 +02:00
|
|
|
end
|
|
|
|
|
2009-05-01 12:03:13 -07:00
|
|
|
def static_id?(options)
|
|
|
|
static_attribute?('id', options)
|
2008-10-16 11:37:58 +02:00
|
|
|
end
|
|
|
|
|
2009-05-01 12:03:13 -07:00
|
|
|
def static_classname?(options)
|
|
|
|
static_attribute?('class', options)
|
2008-10-16 11:37:58 +02:00
|
|
|
end
|
2007-07-24 09:15:27 +00:00
|
|
|
|
|
|
|
# Returns a string representation of an attributes hash
|
|
|
|
# that's prettier than that produced by Hash#inspect
|
2009-05-01 12:03:13 -07:00
|
|
|
def haml_attributes(options)
|
2007-07-24 09:15:27 +00:00
|
|
|
attrs = attributes.map do |name, value|
|
2009-05-01 12:03:13 -07:00
|
|
|
value = dynamic_attribute?(name, options) ? dynamic_attributes[name] : value.inspect
|
2007-07-24 09:15:27 +00:00
|
|
|
name = name.index(/\W/) ? name.inspect : ":#{name}"
|
2008-10-16 11:37:58 +02:00
|
|
|
"#{name} => #{value}"
|
2007-07-24 09:15:27 +00:00
|
|
|
end
|
|
|
|
"{ #{attrs.join(', ')} }"
|
|
|
|
end
|
2007-04-01 08:45:39 +00:00
|
|
|
end
|
2007-04-10 00:29:57 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def match_to_html(string, regex, tag)
|
|
|
|
string.gsub!(regex) do
|
|
|
|
"<haml:#{tag}>#{CGI.escapeHTML($1)}</haml:#{tag}>"
|
|
|
|
end
|
|
|
|
end
|
2007-04-01 08:45:39 +00:00
|
|
|
end
|
|
|
|
end
|