In Files

  • rdoc/ri/formatter.rb

Files

Class/Module Index [+]

Quicksearch

RDoc::RI::Formatter

Constants

FORMATTERS

Attributes

indent[W]
output[RW]

Public Class Methods

for(name) click to toggle source
 
               # File rdoc/ri/formatter.rb, line 11
def self.for(name)
  FORMATTERS[name.downcase]
end
            
list() click to toggle source
 
               # File rdoc/ri/formatter.rb, line 15
def self.list
  FORMATTERS.keys.sort.join ", "
end
            
new(output, width, indent) click to toggle source
 
               # File rdoc/ri/formatter.rb, line 19
def initialize(output, width, indent)
  @output = output
  @width  = width
  @indent = indent
  @original_indent = indent.dup
end
            

Public Instance Methods

blankline() click to toggle source
 
               # File rdoc/ri/formatter.rb, line 80
def blankline
  @output.puts
end
            
bold_print(txt) click to toggle source
 
               # File rdoc/ri/formatter.rb, line 91
def bold_print(txt)
  @output.print txt
end
            
break_to_newline() click to toggle source

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
            
conv_html(txt) click to toggle source

Convert HTML entities back to ASCII

 
               # File rdoc/ri/formatter.rb, line 102
def conv_html(txt)
  txt = txt.gsub(/>/, '>')
  txt.gsub!(/&lt;/, '<')
  txt.gsub!(/&quot;/, '"')
  txt.gsub!(/&amp;/, '&')
  txt
end
            
conv_markup(txt) click to toggle source

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
            
display_flow(flow) click to toggle source
 
               # File rdoc/ri/formatter.rb, line 218
def display_flow(flow)
  flow.each do |f|
    display_flow_item(f)
  end
end
            
display_flow_item(item, prefix = @indent) click to toggle source
 
               # 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
            
display_heading(text, level, indent) click to toggle source
 
               # 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
            
display_list(list) click to toggle source
 
               # 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
            
display_verbatim_flow_item(item, prefix=@indent) click to toggle source
 
               # 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
            
draw_line(label=nil) click to toggle source
 
               # 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
            
indent() click to toggle source
 
               # 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
            
raw_print_line(txt) click to toggle source
 
               # File rdoc/ri/formatter.rb, line 95
def raw_print_line(txt)
  @output.print txt
end
            
strip_attributes(text) click to toggle source
 
               # File rdoc/ri/formatter.rb, line 224
def strip_attributes(text)
  text.gsub(/(<\/?(?:b|code|em|i|tt)>)/, '')
end
            
wrap(txt, prefix=@indent, linelen=@width) click to toggle source
 
               # 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