diff --git a/bin/html2haml b/bin/html2haml index 48748ce8..a85e95a3 100755 --- a/bin/html2haml +++ b/bin/html2haml @@ -1,95 +1,6 @@ #!/usr/bin/env ruby require File.dirname(__FILE__) + '/../lib/haml' -require 'haml/engine' - -begin - require 'rubygems' - require 'hpricot' -rescue LoadError => err - dep = err.message.scan(/^no such file to load -- (.*)/)[0] - puts "Required dependency #{dep} not found!" - exit 1 -end - -def tabulate(tabs) - ' ' * tabs -end - -TEXT_REGEXP = /^(\s*).*$/ - -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 - -module Hpricot::Node - def to_haml(tabs) - parse_text(self.to_s, tabs) - end -end - -class Hpricot::Doc - def to_haml - output = '' - children.each { |child| output += child.to_haml(0) } - output - end -end - -class Hpricot::XMLDecl - def to_haml(tabs) - "#{tabulate(tabs)}!!! XML\n" - end -end - -class Hpricot::DocType - def to_haml(tabs) - "#{tabulate(tabs)}!!!\n" - end -end - -class Hpricot::Comment - def to_haml(tabs) - "#{tabulate(tabs)}/\n#{parse_text(self.content, tabs + 1)}" - end -end - -class Hpricot::Elem - def to_haml(tabs) - output = "#{tabulate(tabs)}" - output += "%#{name}" unless name == 'div' && (attributes.include?('id') || attributes.include?('class')) - - if attributes - output += "##{attributes['id']}" if attributes['id'] - attributes['class'].split(' ').each { |c| output += ".#{c}" } if attributes['class'] - attributes.delete("id") - attributes.delete("class") - output += attributes.inspect if attributes.length > 0 - end - - output += "/" if children.length == 0 - output += "\n" - - self.children.each do |child| - output += child.to_haml(tabs + 1) - end - - output - end -end - -# Must be required after Hpricot mods, -# so they're in scope require 'haml/exec' opts = Haml::Exec::HTML2Haml.new(ARGV) diff --git a/lib/haml/exec.rb b/lib/haml/exec.rb index dfc9b478..e1c7e962 100644 --- a/lib/haml/exec.rb +++ b/lib/haml/exec.rb @@ -220,6 +220,18 @@ END # A class encapsulating executable functionality # specific to the html2haml executable. class HTML2Haml < Generic # :nodoc: + def initialize(args) + super + + begin + require 'haml/html' + rescue LoadError => err + dep = err.message.scan(/^no such file to load -- (.*)/)[0] + puts "Required dependency #{dep} not found!" + exit 1 + end + end + def set_opts(opts) opts.banner = < 0 + end + + output += "/" if children.length == 0 + output += "\n" + + self.children.each do |child| + output += child.to_haml(tabs + 1) + end + + output + end + end + # :startdoc: + end +end