Outputs RDoc markup as RDoc markup! (mostly)

Methods
A
E
H
I
N
S
U
W
Attributes
[RW] indent

Current indent amount for output in characters

[R] list_index

Stack of current list indexes for alphabetic and numeric lists

[R] list_type

Stack of list types

[R] list_width

Stack of list widths for indentation

[R] prefix

Prefix for the next list item. See use_prefix

[R] res

Output accumulator

[RW] width

Output width in characters

Class Public methods
new(markup = nil)

Creates a new formatter that will output (mostly) RDoc markup

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 47
def initialize markup = nil
  super

  @markup.add_special(/\\S/, :SUPPRESSED_CROSSREF)
  @width = 78
  init_tags

  @headings = {}
  @headings.default = []

  @headings[1] = ['= ',      '']
  @headings[2] = ['== ',     '']
  @headings[3] = ['=== ',    '']
  @headings[4] = ['==== ',   '']
  @headings[5] = ['===== ',  '']
  @headings[6] = ['====== ', '']
end
Instance Public methods
accept_blank_line(blank_line)

Adds blank_line to the output

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 77
def accept_blank_line blank_line
  @res << "\n"
end
accept_heading(heading)

Adds heading to the output

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 84
def accept_heading heading
  use_prefix or @res << ' ' * @indent
  @res << @headings[heading.level][0]
  @res << attributes(heading.text)
  @res << @headings[heading.level][1]
  @res << "\n"
end
accept_indented_paragraph(paragraph)

Adds paragraph to the output

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 177
def accept_indented_paragraph paragraph
  @indent += paragraph.indent
  wrap attributes(paragraph.text)
  @indent -= paragraph.indent
end
accept_list_end(list)

Finishes consumption of list

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 95
def accept_list_end list
  @list_index.pop
  @list_type.pop
  @list_width.pop
end
accept_list_item_end(list_item)

Finishes consumption of list_item

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 104
def accept_list_item_end list_item
  width = case @list_type.last
          when :BULLET then
            2
          when :NOTE, :LABEL then
            @res << "\n"
            2
          else
            bullet = @list_index.last.to_s
            @list_index[-1] = @list_index.last.succ
            bullet.length + 2
          end

  @indent -= width
end
accept_list_item_start(list_item)

Prepares the visitor for consuming list_item

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 123
def accept_list_item_start list_item
  type = @list_type.last

  case type
  when :NOTE, :LABEL then
    bullet = attributes(list_item.label) + ":\n"
    @prefix = ' ' * @indent
    @indent += 2
    @prefix << bullet + (' ' * @indent)
  else
    bullet = type == :BULLET ? '*' :  @list_index.last.to_s + '.'
    @prefix = (' ' * @indent) + bullet.ljust(bullet.length + 1)
    width = bullet.length + 1
    @indent += width
  end
end
accept_list_start(list)

Prepares the visitor for consuming list

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 143
def accept_list_start list
  case list.type
  when :BULLET then
    @list_index << nil
    @list_width << 1
  when :LABEL, :NOTE then
    @list_index << nil
    @list_width << 2
  when :LALPHA then
    @list_index << 'a'
    @list_width << list.items.length.to_s.length
  when :NUMBER then
    @list_index << 1
    @list_width << list.items.length.to_s.length
  when :UALPHA then
    @list_index << 'A'
    @list_width << list.items.length.to_s.length
  else
    raise RDoc::Error, "invalid list type #{list.type}"
  end

  @list_type << list.type
end
accept_paragraph(paragraph)

Adds paragraph to the output

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 170
def accept_paragraph paragraph
  wrap attributes(paragraph.text)
end
accept_raw(raw)

Adds raw to the output

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 186
def accept_raw raw
  @res << raw.parts.join("\n")
end
accept_rule(rule)

Adds rule to the output

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 193
def accept_rule rule
  use_prefix or @res << ' ' * @indent
  @res << '-' * (@width - @indent)
  @res << "\n"
end
accept_verbatim(verbatim)

Outputs verbatim indented 2 columns

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 202
def accept_verbatim verbatim
  indent = ' ' * (@indent + 2)

  verbatim.parts.each do |part|
    @res << indent unless part == "\n"
    @res << part
  end

  @res << "\n" unless @res =~ /\n\z/
end
attributes(text)

Applies attribute-specific markup to text using RDoc::AttributeManager

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 216
def attributes text
  flow = @am.flow text.dup
  convert_flow flow
end
end_accepting()

Returns the generated output

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 224
def end_accepting
  @res.join
end
handle_special_SUPPRESSED_CROSSREF(special)

Removes preceding \ from the suppressed crossref special

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 231
def handle_special_SUPPRESSED_CROSSREF special
  text = special.text
  text = text.sub('\', '') unless in_tt?
  text
end
init_tags()

Maps attributes to HTML sequences

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 68
def init_tags
  add_tag :BOLD, "<b>", "</b>"
  add_tag :TT,   "<tt>", "</tt>"
  add_tag :EM,   "<em>", "</em>"
end
start_accepting()

Prepares the visitor for text generation

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 240
def start_accepting
  @res = [""]
  @indent = 0
  @prefix = nil

  @list_index = []
  @list_type  = []
  @list_width = []
end
use_prefix()

Adds the stored prefix to the output and clears it. Lists generate a prefix for later consumption.

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 254
def use_prefix
  prefix = @prefix
  @prefix = nil
  @res << prefix if prefix

  prefix
end
wrap(text)

Wraps text to width

# File ../ruby/lib/rdoc/markup/to_rdoc.rb, line 265
def wrap text
  return unless text && !text.empty?

  text_len = @width - @indent

  text_len = 20 if text_len < 20

  re = /^(.{0,#{text_len}})[ \n]/
  next_prefix = ' ' * @indent

  prefix = @prefix || next_prefix
  @prefix = nil

  @res << prefix

  while text.length > text_len
    if text =~ re then
      @res << $1
      text.slice!(0, $&.length)
    else
      @res << text.slice!(0, text_len)
    end

    @res << "\n" << next_prefix
  end

  if text.empty? then
    @res.pop
    @res.pop
  else
    @res << text
    @res << "\n"
  end
end