class HtmlToTextile::Document

Public Instance Methods

characters(string) click to toggle source
   # File lib/html_to_textile.rb
45 def characters string
46   if stack.last.nil? or stack.last['content'].nil?
47     stack << "#{string}"
48   else
49     stack.last['content'] << "#{string}"
50   end
51 end
end_element(name) click to toggle source
   # File lib/html_to_textile.rb
34 def end_element name
35   if valid_tags.include? name
36     content = stack.pop
37     if stack.empty? or stack.last['content'].nil?
38       tree << content
39     else
40       stack.last['content'] << content
41     end
42   end
43 end
stack() click to toggle source
   # File lib/html_to_textile.rb
13 def stack
14   @stack ||= []
15 end
start_element(name, attrs = []) click to toggle source
   # File lib/html_to_textile.rb
17 def start_element name, attrs = []
18   if valid_tags.include? name
19     attrs_hash = {}
20     while key = attrs.shift do
21       value = attrs.shift
22       attrs_hash[key] = value
23     end
24     
25     
26     stack << {
27       'name' => name, 
28       'content' => [],
29       'attrs' => attrs_hash
30     }
31   end
32 end
to_html() click to toggle source
   # File lib/html_to_textile.rb
73 def to_html
74   @html = ''
75   tree.each do |content|
76     _to_html_tag content
77   end
78   return @html
79 end
to_textile() click to toggle source
   # File lib/html_to_textile.rb
53 def to_textile  
54   ##ActiveRecord::Base.logger.debug  "### tree START"
55   ##ActiveRecord::Base.logger.debug  @tree.inspect
56   ##ActiveRecord::Base.logger.debug  "### tree END"
57   #ActiveRecord::Base.logger.debug  "### HTML START"
58   to_html
59   #ActiveRecord::Base.logger.debug  "### HTML END"
60   
61   textile = ''
62   tree.each do |content|
63     textile += _to_textile_tag content
64   end
65   
66   #ActiveRecord::Base.logger.debug  "### TEXTILE START"
67   #ActiveRecord::Base.logger.debug  textile
68   #ActiveRecord::Base.logger.debug  "### TEXTILE END"
69   
70   return textile
71 end
tree() click to toggle source
   # File lib/html_to_textile.rb
 9 def tree
10   @tree ||= []
11 end
valid_tags() click to toggle source
  # File lib/html_to_textile.rb
5 def valid_tags
6   ['a','p','br','u','b','i','strong','em','ul','ol','li','sup','sub', 'ins', 'del', 'h1', 'h2', 'h3', 'table', 'tr', 'td', 'th']
7 end