mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Added html2haml to trunk.
git-svn-id: svn://hamptoncatlin.com/haml/trunk@303 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
parent
787cd66705
commit
d1880f036a
2 changed files with 81 additions and 1 deletions
2
Rakefile
2
Rakefile
|
@ -72,7 +72,7 @@ unless ARGV[0] == 'benchmark'
|
|||
list.exclude(/[a-z]/)
|
||||
list.exclude('TODO')
|
||||
end.to_a
|
||||
spec.executables = ['haml', 'sass']
|
||||
spec.executables = ['haml', 'html2haml', 'sass']
|
||||
spec.files = FileList['lib/**/*', 'bin/*', 'test/**/*', 'Rakefile', 'init.rb'].to_a + readmes
|
||||
spec.homepage = 'http://haml.hamptoncatlin.com/'
|
||||
spec.has_rdoc = true
|
||||
|
|
80
bin/html2haml
Normal file
80
bin/html2haml
Normal file
|
@ -0,0 +1,80 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require 'rubygems'
|
||||
require 'hpricot'
|
||||
require 'open-uri'
|
||||
|
||||
doc = open(ARGV[0]) { |f| Hpricot(f) }
|
||||
|
||||
def tabulate(tabs)
|
||||
' ' * tabs
|
||||
end
|
||||
|
||||
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"
|
||||
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'
|
||||
|
||||
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
|
||||
|
||||
puts doc.to_haml
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue