In Files

  • rdoc/ri/ri_formatter.rb

Files

Class/Module Index [+]

Quicksearch

RI::TextFormatter

Finally, fill in the list of known formatters

Constants

FORMATTERS

Attributes

indent[R]

Public Class Methods

for(name) click to toggle source
 
               # File rdoc/ri/ri_formatter.rb, line 664
def TextFormatter.for(name)
  FORMATTERS[name.downcase]
end
            
list() click to toggle source
 
               # File rdoc/ri/ri_formatter.rb, line 660
def TextFormatter.list
  FORMATTERS.keys.sort.join(", ")
end
            
new(options, indent) click to toggle source
 
               # File rdoc/ri/ri_formatter.rb, line 6
def initialize(options, indent)
  @options = options
  @width   = options.width
  @indent  = indent
end
            

Public Instance Methods

blankline() click to toggle source
 
               # File rdoc/ri/ri_formatter.rb, line 51
def blankline
  puts
end
            
bold_print(txt) click to toggle source
 
               # File rdoc/ri/ri_formatter.rb, line 66
def bold_print(txt)
  print txt
end
            
break_to_newline() click to toggle source

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

convert HTML entities back to ASCII

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

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
            
display_flow(flow) click to toggle source
 
               # File rdoc/ri/ri_formatter.rb, line 210
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/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
            
display_heading(text, level, indent) click to toggle source
 
               # 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
            
display_list(list) click to toggle source
 
               # 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
            
display_verbatim_flow_item(item, prefix=@indent) click to toggle source
 
               # 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
            
draw_line(label=nil) click to toggle source
 
               # 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
            
raw_print_line(txt) click to toggle source
 
               # File rdoc/ri/ri_formatter.rb, line 72
def raw_print_line(txt)
  puts txt
end
            
strip_attributes(txt) click to toggle source
 
               # 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
            
wrap(txt, prefix=@indent, linelen=@width) click to toggle source
 
               # 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