Some progress made on html2haml understanding ERB. Loops remain a problem.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@491 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2007-04-10 00:29:57 +00:00
parent 94a75d2336
commit 7226e478b5
2 changed files with 44 additions and 3 deletions

View File

@ -225,6 +225,8 @@ END
def initialize(args)
super
@module_opts = {}
begin
require 'haml/html'
rescue LoadError => err
@ -243,6 +245,10 @@ Description: Transforms an HTML file into corresponding Haml code.
Options:
END
opts.on('-r', '--rhtml', 'Parse RHTML tags.') do
@module_opts[:rhtml] = true
end
super
end
@ -252,7 +258,7 @@ END
input = @options[:input]
output = @options[:output]
output.write(::Haml::HTML.new(input).render)
output.write(::Haml::HTML.new(input, @module_opts).render)
end
end

View File

@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../haml'
require 'haml/engine'
require 'rubygems'
require 'hpricot'
require 'cgi'
module Haml
# This class contains the functionality used in the +html2haml+ utility,
@ -12,10 +13,20 @@ module Haml
# Creates a new instance of Haml::HTML that will compile the given template,
# which can either be a string containing HTML or an Hpricot node,
# to a Haml string when +render+ is called.
def initialize(template)
def initialize(template, options = {})
@@options = options
if template.is_a? Hpricot::Node
@template = template
else
if template.is_a? IO
template = template.read
end
if @@options[:rhtml]
match_to_html(template, /<%=(.*?)-?%>/m, 'loud')
match_to_html(template, /<%(.*?)-?%>/m, 'silent')
end
@template = Hpricot(template)
end
end
@ -57,6 +68,10 @@ module Haml
# :stopdoc:
def self.options
@@options
end
TEXT_REGEXP = /^(\s*).*$/
class ::Hpricot::Doc
@ -113,6 +128,10 @@ module Haml
class ::Hpricot::Elem
def to_haml(tabs = 0)
output = "#{tabulate(tabs)}"
if HTML.options[:rhtml] && name[0...5] == 'haml:'
return output + HTML.send("haml_tag_#{name[5..-1]}", self.innerHTML)
end
output += "%#{name}" unless name == 'div' && (attributes.include?('id') || attributes.include?('class'))
if attributes
@ -133,6 +152,22 @@ module Haml
output
end
end
def self.haml_tag_loud(text)
"= #{text.gsub(/\n\s*/, '; ').strip}\n"
end
def self.haml_tag_silent(text)
text.split("\n").map { |line| "- #{line.strip}\n" }.join
end
private
def match_to_html(string, regex, tag)
string.gsub!(regex) do
"<haml:#{tag}>#{CGI.escapeHTML($1)}</haml:#{tag}>"
end
end
# :startdoc:
end
end