Finally, fill in the list of known formatters
# File rdoc/ri/ri_formatter.rb, line 664
def TextFormatter.for(name)
FORMATTERS[name.downcase]
end
# File rdoc/ri/ri_formatter.rb, line 66
def bold_print(txt)
print txt
end
called when we want to ensure a nbew ‘wrap’ starts on a newline Only needed for HtmlFormatter, because the rest do their own line breaking
# File rdoc/ri/ri_formatter.rb, line 61
def break_to_newline
end
convert HTML entities back to ASCII
# File rdoc/ri/ri_formatter.rb, line 79
def conv_html(txt)
txt.
gsub(/>/, '>').
gsub(/</, '<').
gsub(/"/, '"').
gsub(/&/, '&')
end
convert markup into display form
# File rdoc/ri/ri_formatter.rb, line 89
def conv_markup(txt)
txt.
gsub(%r{<tt>(.*?)</tt>}) { "+#$1+" } .
gsub(%r{<code>(.*?)</code>}) { "+#$1+" } .
gsub(%r{<b>(.*?)</b>}) { "*#$1*" } .
gsub(%r{<em>(.*?)</em>}) { "_#$1_" }
end
# File rdoc/ri/ri_formatter.rb, line 210
def display_flow(flow)
flow.each do |f|
display_flow_item(f)
end
end
# File rdoc/ri/ri_formatter.rb, line 154
def display_flow_item(item, prefix=@indent)
case item
when SM::Flow::P, SM::Flow::LI
wrap(conv_html(item.body), prefix)
blankline
when SM::Flow::LIST
display_list(item)
when SM::Flow::VERB
display_verbatim_flow_item(item, @indent)
when SM::Flow::H
display_heading(conv_html(item.text), item.level, @indent)
when SM::Flow::RULE
draw_line
else
fail "Unknown flow element: #{item.class}"
end
end
# File rdoc/ri/ri_formatter.rb, line 188
def display_heading(text, level, indent)
text = strip_attributes(text)
case level
when 1
ul = "=" * text.length
puts
puts text.upcase
puts ul
# puts
when 2
ul = "-" * text.length
puts
puts text
puts ul
# puts
else
print indent, text, "\n"
end
end
# File rdoc/ri/ri_formatter.rb, line 99
def display_list(list)
case list.type
when SM::ListBase::BULLET
prefixer = proc { |ignored| @indent + "* " }
when SM::ListBase::NUMBER,
SM::ListBase::UPPERALPHA,
SM::ListBase::LOWERALPHA
start = case list.type
when SM::ListBase::NUMBER then 1
when SM::ListBase::UPPERALPHA then 'A'
when SM::ListBase::LOWERALPHA then 'a'
end
prefixer = proc do |ignored|
res = @indent + "#{start}.".ljust(4)
start = start.succ
res
end
when SM::ListBase::LABELED
prefixer = proc do |li|
li.label
end
when SM::ListBase::NOTE
longest = 0
list.contents.each do |item|
if item.kind_of?(SM::Flow::LI) && item.label.length > longest
longest = item.label.length
end
end
prefixer = proc do |li|
@indent + li.label.ljust(longest+1)
end
else
fail "unknown list type"
end
list.contents.each do |item|
if item.kind_of? SM::Flow::LI
prefix = prefixer.call(item)
display_flow_item(item, prefix)
else
display_flow_item(item)
end
end
end
# File rdoc/ri/ri_formatter.rb, line 179
def display_verbatim_flow_item(item, prefix=@indent)
item.body.split(/\n/).each do |line|
print @indent, conv_html(line), "\n"
end
blankline
end
# File rdoc/ri/ri_formatter.rb, line 15
def draw_line(label=nil)
len = @width
len -= (label.size+1) if label
print "-"*len
if label
print(" ")
bold_print(label)
end
puts
end
# File rdoc/ri/ri_formatter.rb, line 72
def raw_print_line(txt)
puts txt
end
# File rdoc/ri/ri_formatter.rb, line 216
def strip_attributes(txt)
tokens = txt.split(%r{(</?(?:b|code|em|i|tt)>)})
text = []
attributes = 0
tokens.each do |tok|
case tok
when %r{^</(\w+)>$}, %r{^<(\w+)>$}
;
else
text << tok
end
end
text.join
end
# File rdoc/ri/ri_formatter.rb, line 28
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?
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.