| Class | RDoc::RI::Formatter |
| In: |
ri/formatter.rb
|
| Parent: | Object |
# File ri/formatter.rb, line 19
19: def initialize(output, width, indent)
20: @output = output
21: @width = width
22: @indent = indent
23: end
Called when we want to ensure a new ‘wrap’ starts on a newline. Only needed for HtmlFormatter, because the rest do their own line breaking.
# File ri/formatter.rb, line 75
75: def break_to_newline
76: end
Convert HTML entities back to ASCII
# File ri/formatter.rb, line 89
89: def conv_html(txt)
90: txt = txt.gsub(/>/, '>')
91: txt.gsub!(/</, '<')
92: txt.gsub!(/"/, '"')
93: txt.gsub!(/&/, '&')
94: txt
95: end
Convert markup into display form
# File ri/formatter.rb, line 100
100: def conv_markup(txt)
101: txt = txt.gsub(%r{<tt>(.*?)</tt>}, '+\1+')
102: txt.gsub!(%r{<code>(.*?)</code>}, '+\1+')
103: txt.gsub!(%r{<b>(.*?)</b>}, '*\1*')
104: txt.gsub!(%r{<em>(.*?)</em>}, '_\1_')
105: txt
106: end
# File ri/formatter.rb, line 205
205: def display_flow(flow)
206: flow.each do |f|
207: display_flow_item(f)
208: end
209: end
# File ri/formatter.rb, line 153
153: def display_flow_item(item, prefix = @indent)
154: case item
155: when RDoc::Markup::Flow::P, RDoc::Markup::Flow::LI
156: wrap(conv_html(item.body), prefix)
157: blankline
158:
159: when RDoc::Markup::Flow::LIST
160: display_list(item)
161:
162: when RDoc::Markup::Flow::VERB
163: display_verbatim_flow_item(item, @indent)
164:
165: when RDoc::Markup::Flow::H
166: display_heading(conv_html(item.text), item.level, @indent)
167:
168: when RDoc::Markup::Flow::RULE
169: draw_line
170:
171: else
172: raise RDoc::Error, "Unknown flow element: #{item.class}"
173: end
174: end
# File ri/formatter.rb, line 183
183: def display_heading(text, level, indent)
184: text = strip_attributes text
185:
186: case level
187: when 1 then
188: ul = "=" * text.length
189: @output.puts
190: @output.puts text.upcase
191: @output.puts ul
192:
193: when 2 then
194: ul = "-" * text.length
195: @output.puts
196: @output.puts text
197: @output.puts ul
198: else
199: @output.print indent, text, "\n"
200: end
201:
202: @output.puts
203: end
# File ri/formatter.rb, line 108
108: def display_list(list)
109: case list.type
110: when :BULLET
111: prefixer = proc { |ignored| @indent + "* " }
112:
113: when :NUMBER, :UPPERALPHA, :LOWERALPHA then
114: start = case list.type
115: when :NUMBER then 1
116: when :UPPERALPHA then 'A'
117: when :LOWERALPHA then 'a'
118: end
119:
120: prefixer = proc do |ignored|
121: res = @indent + "#{start}.".ljust(4)
122: start = start.succ
123: res
124: end
125:
126: when :LABELED, :NOTE then
127: longest = 0
128:
129: list.contents.each do |item|
130: if RDoc::Markup::Flow::LI === item and item.label.length > longest then
131: longest = item.label.length
132: end
133: end
134:
135: longest += 1
136:
137: prefixer = proc { |li| @indent + li.label.ljust(longest) }
138:
139: else
140: raise ArgumentError, "unknown list type #{list.type}"
141: end
142:
143: list.contents.each do |item|
144: if RDoc::Markup::Flow::LI === item then
145: prefix = prefixer.call item
146: display_flow_item item, prefix
147: else
148: display_flow_item item
149: end
150: end
151: end
# File ri/formatter.rb, line 176
176: def display_verbatim_flow_item(item, prefix=@indent)
177: item.body.split(/\n/).each do |line|
178: @output.print @indent, conv_html(line), "\n"
179: end
180: blankline
181: end
# File ri/formatter.rb, line 25
25: def draw_line(label=nil)
26: len = @width
27: len -= (label.size + 1) if label
28:
29: if len > 0 then
30: @output.print '-' * len
31: if label
32: @output.print ' '
33: bold_print label
34: end
35:
36: @output.puts
37: else
38: @output.print '-' * @width
39: @output.puts
40:
41: @output.puts label
42: end
43: end
# File ri/formatter.rb, line 211
211: def strip_attributes(text)
212: text.gsub(/(<\/?(?:b|code|em|i|tt)>)/, '')
213: end
# File ri/formatter.rb, line 45
45: def wrap(txt, prefix=@indent, linelen=@width)
46: return unless txt && !txt.empty?
47:
48: work = conv_markup(txt)
49: textLen = linelen - prefix.length
50: patt = Regexp.new("^(.{0,#{textLen}})[ \n]")
51: next_prefix = prefix.tr("^ ", " ")
52:
53: res = []
54:
55: while work.length > textLen
56: if work =~ patt
57: res << $1
58: work.slice!(0, $&.length)
59: else
60: res << work.slice!(0, textLen)
61: end
62: end
63: res << work if work.length.nonzero?
64: @output.puts(prefix + res.join("\n" + next_prefix))
65: end