mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Make html2haml usable from withing Ruby, using the Haml::HTML class and/or the Hpricot to_haml extension.
git-svn-id: svn://hamptoncatlin.com/haml/trunk@475 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
parent
dcf5697241
commit
2a5337bff0
3 changed files with 127 additions and 90 deletions
|
@ -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)
|
||||
|
|
|
@ -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 = <<END
|
||||
Usage: html2haml [options] (html file) (output file)
|
||||
|
@ -238,7 +250,7 @@ END
|
|||
input = @options[:input]
|
||||
output = @options[:output]
|
||||
|
||||
output.write(Hpricot(input).to_haml)
|
||||
output.write(::Haml::HTML.new(input).render)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
114
lib/haml/html.rb
Normal file
114
lib/haml/html.rb
Normal file
|
@ -0,0 +1,114 @@
|
|||
require File.dirname(__FILE__) + '/../haml'
|
||||
|
||||
require 'haml/engine'
|
||||
require 'rubygems'
|
||||
require 'hpricot'
|
||||
|
||||
module Haml
|
||||
# This class contains the functionality used in the +html2haml+ utility,
|
||||
# namely converting HTML documents to Haml templates.
|
||||
# It depends on Hpricot for HTML parsing (http://code.whytheluckystiff.net/hpricot/).
|
||||
class HTML
|
||||
# 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)
|
||||
if template.is_a? Hpricot::Node
|
||||
@template = template
|
||||
else
|
||||
@template = Hpricot(template)
|
||||
end
|
||||
end
|
||||
|
||||
# Processes the document and returns the result as a string
|
||||
# containing the Haml template.
|
||||
def render
|
||||
@template.to_haml(0)
|
||||
end
|
||||
alias_method :to_haml, :render
|
||||
|
||||
module ::Hpricot::Node
|
||||
# Returns the Haml representation of the given node,
|
||||
# at the given tabulation.
|
||||
def to_haml(tabs = 0)
|
||||
parse_text(self.to_s, tabs)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def tabulate(tabs)
|
||||
' ' * tabs
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
# :stopdoc:
|
||||
|
||||
TEXT_REGEXP = /^(\s*).*$/
|
||||
|
||||
class ::Hpricot::Doc
|
||||
def to_haml(tabs = 0)
|
||||
output = ''
|
||||
children.each { |child| output += child.to_haml(0) }
|
||||
output
|
||||
end
|
||||
end
|
||||
|
||||
class ::Hpricot::XMLDecl
|
||||
def to_haml(tabs = 0)
|
||||
"#{tabulate(tabs)}!!! XML\n"
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Understand STRICT, etc. doctypes
|
||||
class ::Hpricot::DocType
|
||||
def to_haml(tabs = 0)
|
||||
"#{tabulate(tabs)}!!!\n"
|
||||
end
|
||||
end
|
||||
|
||||
class ::Hpricot::Comment
|
||||
def to_haml(tabs = 0)
|
||||
"#{tabulate(tabs)}/\n#{parse_text(self.content, tabs + 1)}"
|
||||
end
|
||||
end
|
||||
|
||||
class ::Hpricot::Elem
|
||||
def to_haml(tabs = 0)
|
||||
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
|
||||
# :startdoc:
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue