1 require 'rdoc/markup/formatter'
2 require 'rdoc/markup/fragments'
3 require 'rdoc/markup/inline'
10 VERB = Struct.new(:body)
11 RULE = Struct.new(:width)
13 attr_reader :type, :contents
22 LI = Struct.new(:label, :body)
23 H = Struct.new(:level, :text)
26 class ToFlow < RDoc::Markup::Formatter
28 :BULLET => [ "<ul>", "</ul>" ],
29 :NUMBER => [ "<ol>", "</ol>" ],
30 :UPPERALPHA => [ "<ol>", "</ol>" ],
31 :LOWERALPHA => [ "<ol>", "</ol>" ],
32 :LABELED => [ "<dl>", "</dl>" ],
33 :NOTE => [ "<table>", "</table>" ],
36 InlineTag = Struct.new(:bit, :on, :off)
45 # Set up the standard mapping of attributes to HTML tags
49 InlineTag.new(RDoc::Markup::Attribute.bitmap_for(:BOLD), "<b>", "</b>"),
50 InlineTag.new(RDoc::Markup::Attribute.bitmap_for(:TT), "<tt>", "</tt>"),
51 InlineTag.new(RDoc::Markup::Attribute.bitmap_for(:EM), "<em>", "</em>"),
56 # Add a new set of HTML tags for an attribute. We allow separate start and
57 # end tags for flexibility
59 def add_tag(name, start, stop)
60 @attr_tags << InlineTag.new(RDoc::Markup::Attribute.bitmap_for(name), start, stop)
64 # Given an HTML tag, decorate it with class information and the like if
65 # required. This is a no-op in the base class, but is overridden in HTML
66 # output classes that implement style sheets
73 # Here's the client side of the visitor pattern
84 def accept_paragraph(am, fragment)
85 @res << Flow::P.new((convert_flow(am.flow(fragment.txt))))
88 def accept_verbatim(am, fragment)
89 @res << Flow::VERB.new((convert_flow(am.flow(fragment.txt))))
92 def accept_rule(am, fragment)
94 size = 10 if size > 10
95 @res << Flow::RULE.new(size)
98 def accept_list_start(am, fragment)
99 @list_stack.push(@res)
100 list = Flow::LIST.new(fragment.type)
105 def accept_list_end(am, fragment)
106 @res = @list_stack.pop
109 def accept_list_item(am, fragment)
110 @res << Flow::LI.new(fragment.param, convert_flow(am.flow(fragment.txt)))
113 def accept_blank_line(am, fragment)
114 # @res << annotate("<p />") << "\n"
117 def accept_heading(am, fragment)
118 @res << Flow::H.new(fragment.head_level, convert_flow(am.flow(fragment.txt)))
123 def on_tags(res, item)
124 attr_mask = item.turn_on
125 return if attr_mask.zero?
127 @attr_tags.each do |tag|
128 if attr_mask & tag.bit != 0
129 res << annotate(tag.on)
134 def off_tags(res, item)
135 attr_mask = item.turn_off
136 return if attr_mask.zero?
138 @attr_tags.reverse_each do |tag|
139 if attr_mask & tag.bit != 0
140 res << annotate(tag.off)
145 def convert_flow(flow)
150 res << convert_string(item)
155 res << convert_special(item)
157 raise "Unknown flow element: #{item.inspect}"
163 def convert_string(item)
167 def convert_special(special)
169 Attribute.each_name_of(special.type) do |name|
170 method_name = "handle_special_#{name}"
171 if self.respond_to? method_name
172 special.text = send(method_name, special)
177 raise "Unhandled special: #{special}" unless handled