# File rdoc/ri/formatter.rb, line 11
def self.for(name)
FORMATTERS[name.downcase]
end
# File rdoc/ri/formatter.rb, line 80
def blankline
@output.puts
end
# File rdoc/ri/formatter.rb, line 91
def bold_print(txt)
@output.print txt
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 rdoc/ri/formatter.rb, line 88
def break_to_newline
end
Convert HTML entities back to ASCII
# File rdoc/ri/formatter.rb, line 102
def conv_html(txt)
txt = txt.gsub(/>/, '>')
txt.gsub!(/</, '<')
txt.gsub!(/"/, '"')
txt.gsub!(/&/, '&')
txt
end
Convert markup into display form
# File rdoc/ri/formatter.rb, line 113
def conv_markup(txt)
txt = txt.gsub(%r{<tt>(.*?)</tt>}, '+\1+')
txt.gsub!(%r{<code>(.*?)</code>}, '+\1+')
txt.gsub!(%r{<b>(.*?)</b>}, '*\1*')
txt.gsub!(%r{<em>(.*?)</em>}, '_\1_')
txt
end
# File rdoc/ri/formatter.rb, line 218
def display_flow(flow)
flow.each do |f|
display_flow_item(f)
end
end
# File rdoc/ri/formatter.rb, line 166
def display_flow_item(item, prefix = @indent)
case item
when RDoc::Markup::Flow::P, RDoc::Markup::Flow::LI
wrap(conv_html(item.body), prefix)
blankline
when RDoc::Markup::Flow::LIST
display_list(item)
when RDoc::Markup::Flow::VERB
display_verbatim_flow_item(item, @indent)
when RDoc::Markup::Flow::H
display_heading(conv_html(item.text), item.level, @indent)
when RDoc::Markup::Flow::RULE
draw_line
else
raise RDoc::Error, "Unknown flow element: #{item.class}"
end
end
# File rdoc/ri/formatter.rb, line 196
def display_heading(text, level, indent)
text = strip_attributes text
case level
when 1 then
ul = "=" * text.length
@output.puts
@output.puts text.upcase
@output.puts ul
when 2 then
ul = "-" * text.length
@output.puts
@output.puts text
@output.puts ul
else
@output.print indent, text, "\n"
end
@output.puts
end
# File rdoc/ri/formatter.rb, line 121
def display_list(list)
case list.type
when :BULLET
prefixer = proc { |ignored| @indent + "* " }
when :NUMBER, :UPPERALPHA, :LOWERALPHA then
start = case list.type
when :NUMBER then 1
when :UPPERALPHA then 'A'
when :LOWERALPHA then 'a'
end
prefixer = proc do |ignored|
res = @indent + "#{start}.".ljust(4)
start = start.succ
res
end
when :LABELED, :NOTE then
longest = 0
list.contents.each do |item|
if RDoc::Markup::Flow::LI === item and item.label.length > longest then
longest = item.label.length
end
end
longest += 1
prefixer = proc { |li| @indent + li.label.ljust(longest) }
else
raise ArgumentError, "unknown list type #{list.type}"
end
list.contents.each do |item|
if RDoc::Markup::Flow::LI === item then
prefix = prefixer.call item
display_flow_item item, prefix
else
display_flow_item item
end
end
end
# File rdoc/ri/formatter.rb, line 189
def display_verbatim_flow_item(item, prefix=@indent)
item.body.split(/\n/).each do |line|
@output.print @indent, conv_html(line), "\n"
end
blankline
end
# File rdoc/ri/formatter.rb, line 26
def draw_line(label=nil)
len = @width
len -= (label.size + 1) if label
if len > 0 then
@output.print '-' * len
if label
@output.print ' '
bold_print label
end
@output.puts
else
@output.print '-' * @width
@output.puts
@output.puts label
end
end
# File rdoc/ri/formatter.rb, line 46
def indent
return @indent unless block_given?
begin
indent = @indent.dup
@indent += @original_indent
yield
ensure
@indent = indent
end
end
# File rdoc/ri/formatter.rb, line 95
def raw_print_line(txt)
@output.print txt
end
# File rdoc/ri/formatter.rb, line 224
def strip_attributes(text)
text.gsub(/(<\/?(?:b|code|em|i|tt)>)/, '')
end
# File rdoc/ri/formatter.rb, line 58
def wrap(txt, prefix=@indent, linelen=@width)
return unless txt && !txt.empty?
work = conv_markup(txt)
textLen = linelen - prefix.length
patt = Regexp.new("^(.{0,#{textLen}})[ \n]")
next_prefix = prefix.tr("^ ", " ")
res = []
while work.length > textLen
if work =~ patt
res << $1
work.slice!(0, $&.length)
else
res << work.slice!(0, textLen)
end
end
res << work if work.length.nonzero?
@output.puts(prefix + res.join("\n" + next_prefix))
end
Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.
If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.
If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.
If you want to help improve the Ruby documentation, please see Improve the docs, or visit Documenting-ruby.org.