Work on html2haml, including a reasonable UI and ability to escape Haml-sensitive plain text.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@336 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2007-02-02 07:36:58 +00:00
parent cb79a5f9f7
commit 01dfd23a18
2 changed files with 41 additions and 7 deletions

View File

@ -1,22 +1,27 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../lib/haml'
require 'haml/engine'
require 'rubygems'
require 'hpricot'
require 'open-uri'
doc = open(ARGV[0]) { |f| Hpricot(f) }
def tabulate(tabs)
' ' * tabs
end
TEXT_REGEXP = /^(\s*).*$/
def parse_text(text, tabs)
text.strip!
if text.empty?
String.new
else
text = text.gsub(/[\t ]+/, " ").gsub(/(\r|\n|\r\n)\s*/, "\n#{tabulate(tabs)}")
"#{tabulate(tabs)}#{text}\n"
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
@ -76,5 +81,9 @@ class Hpricot::Elem
end
end
puts doc.to_haml
# Must be required after Hpricot mods,
# so they're in scope
require 'haml/exec'
opts = Haml::Exec::HTML2Haml.new(ARGV)
opts.parse!

View File

@ -95,7 +95,7 @@ module Haml
def set_opts(opts)
opts.banner = <<END
Usage: #{@name.downcase} [options] (template file) (output file)
Usage: #{@name.downcase} [options] (#{@name.downcase} file) (output file)
Description:
Uses the #{@name} engine to parse the specified template
@ -154,5 +154,30 @@ END
output.close() if output.is_a? File
end
end
# A class encapsulating executable functionality
# specific to the html2haml executable.
class HTML2Haml < Generic
def set_opts(opts)
opts.banner = <<END
Usage: html2haml [options] (html file) (output file)
Description: Transforms an HTML file into corresponding Haml code.
Options:
END
super
end
def process_result
super
input = @options[:input]
output = @options[:output]
output.write(Hpricot(input).to_haml)
end
end
end
end