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:
parent
94a75d2336
commit
7226e478b5
|
@ -225,6 +225,8 @@ END
|
||||||
def initialize(args)
|
def initialize(args)
|
||||||
super
|
super
|
||||||
|
|
||||||
|
@module_opts = {}
|
||||||
|
|
||||||
begin
|
begin
|
||||||
require 'haml/html'
|
require 'haml/html'
|
||||||
rescue LoadError => err
|
rescue LoadError => err
|
||||||
|
@ -243,6 +245,10 @@ Description: Transforms an HTML file into corresponding Haml code.
|
||||||
Options:
|
Options:
|
||||||
END
|
END
|
||||||
|
|
||||||
|
opts.on('-r', '--rhtml', 'Parse RHTML tags.') do
|
||||||
|
@module_opts[:rhtml] = true
|
||||||
|
end
|
||||||
|
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -252,7 +258,7 @@ END
|
||||||
input = @options[:input]
|
input = @options[:input]
|
||||||
output = @options[:output]
|
output = @options[:output]
|
||||||
|
|
||||||
output.write(::Haml::HTML.new(input).render)
|
output.write(::Haml::HTML.new(input, @module_opts).render)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../haml'
|
||||||
require 'haml/engine'
|
require 'haml/engine'
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'hpricot'
|
require 'hpricot'
|
||||||
|
require 'cgi'
|
||||||
|
|
||||||
module Haml
|
module Haml
|
||||||
# This class contains the functionality used in the +html2haml+ utility,
|
# 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,
|
# 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,
|
# which can either be a string containing HTML or an Hpricot node,
|
||||||
# to a Haml string when +render+ is called.
|
# to a Haml string when +render+ is called.
|
||||||
def initialize(template)
|
def initialize(template, options = {})
|
||||||
|
@@options = options
|
||||||
|
|
||||||
if template.is_a? Hpricot::Node
|
if template.is_a? Hpricot::Node
|
||||||
@template = template
|
@template = template
|
||||||
else
|
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)
|
@template = Hpricot(template)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -56,6 +67,10 @@ module Haml
|
||||||
end
|
end
|
||||||
|
|
||||||
# :stopdoc:
|
# :stopdoc:
|
||||||
|
|
||||||
|
def self.options
|
||||||
|
@@options
|
||||||
|
end
|
||||||
|
|
||||||
TEXT_REGEXP = /^(\s*).*$/
|
TEXT_REGEXP = /^(\s*).*$/
|
||||||
|
|
||||||
|
@ -112,7 +127,11 @@ module Haml
|
||||||
|
|
||||||
class ::Hpricot::Elem
|
class ::Hpricot::Elem
|
||||||
def to_haml(tabs = 0)
|
def to_haml(tabs = 0)
|
||||||
output = "#{tabulate(tabs)}"
|
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'))
|
output += "%#{name}" unless name == 'div' && (attributes.include?('id') || attributes.include?('class'))
|
||||||
|
|
||||||
if attributes
|
if attributes
|
||||||
|
@ -133,6 +152,22 @@ module Haml
|
||||||
output
|
output
|
||||||
end
|
end
|
||||||
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:
|
# :startdoc:
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue