class HtmlToTextile::Document

Public Instance Methods

characters(string) click to toggle source
# File lib/html_to_textile.rb, line 45
def characters string
  if stack.last.nil? or stack.last['content'].nil?
    stack << "#{string}"
  else
    stack.last['content'] << "#{string}"
  end
end
end_element(name) click to toggle source
# File lib/html_to_textile.rb, line 34
def end_element name
  if valid_tags.include? name
    content = stack.pop
    if stack.empty? or stack.last['content'].nil?
      tree << content
    else
      stack.last['content'] << content
    end
  end
end
stack() click to toggle source
# File lib/html_to_textile.rb, line 13
def stack
  @stack ||= []
end
start_element(name, attrs = []) click to toggle source
# File lib/html_to_textile.rb, line 17
def start_element name, attrs = []
  if valid_tags.include? name
    attrs_hash = {}
    while key = attrs.shift do
      value = attrs.shift
      attrs_hash[key] = value
    end
    
    
    stack << {
      'name' => name, 
      'content' => [],
      'attrs' => attrs_hash
    }
  end
end
to_html() click to toggle source
# File lib/html_to_textile.rb, line 73
def to_html
  @html = ''
  tree.each do |content|
    _to_html_tag content
  end
  return @html
end
to_textile() click to toggle source
# File lib/html_to_textile.rb, line 53
def to_textile  
  ##ActiveRecord::Base.logger.debug  "### tree START"
  ##ActiveRecord::Base.logger.debug  @tree.inspect
  ##ActiveRecord::Base.logger.debug  "### tree END"
  #ActiveRecord::Base.logger.debug  "### HTML START"
  to_html
  #ActiveRecord::Base.logger.debug  "### HTML END"
  
  textile = ''
  tree.each do |content|
    textile += _to_textile_tag content
  end
  
  #ActiveRecord::Base.logger.debug  "### TEXTILE START"
  #ActiveRecord::Base.logger.debug  textile
  #ActiveRecord::Base.logger.debug  "### TEXTILE END"      
  
  return textile
end
tree() click to toggle source
# File lib/html_to_textile.rb, line 9
def tree
  @tree ||= []
end
valid_tags() click to toggle source
# File lib/html_to_textile.rb, line 5
def valid_tags
  ['a','p','br','u','b','i','strong','em','ul','ol','li','sup','sub', 'ins', 'del', 'h1', 'h2', 'h3', 'table', 'tr', 'td', 'th']
end