class RDoc::Markup::ToRdoc

Outputs RDoc markup as RDoc markup! (mostly)

Attributes

indent[RW]

Current indent amount for output in characters

list_index[R]

Stack of current list indexes for alphabetic and numeric lists

list_type[R]

Stack of list types

list_width[R]

Stack of list widths for indentation

prefix[R]

Prefix for the next list item. See use_prefix

res[R]

Output accumulator

width[RW]

Output width in characters

Public Class Methods

new(markup = nil) click to toggle source

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

Calls superclass method RDoc::Markup::Formatter::new
# File rdoc/markup/to_rdoc.rb, line 45
def initialize markup = nil
  super nil, markup

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

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

  @headings[1] = ['= ',      '']
  @headings[2] = ['== ',     '']
  @headings[3] = ['=== ',    '']
  @headings[4] = ['==== ',   '']
  @headings[5] = ['===== ',  '']
  @headings[6] = ['====== ', '']

  @hard_break = "\n"
end

Public Instance Methods

accept_blank_line(blank_line) click to toggle source

Adds blank_line to the output

# File rdoc/markup/to_rdoc.rb, line 77
def accept_blank_line blank_line
  @res << "\n"
end
accept_block_quote(block_quote) click to toggle source

Adds paragraph to the output

# File rdoc/markup/to_rdoc.rb, line 84
def accept_block_quote block_quote
  @indent += 2

  block_quote.parts.each do |part|
    @prefix = '> '

    part.accept self
  end

  @indent -= 2
end
accept_heading(heading) click to toggle source

Adds heading to the output

# File rdoc/markup/to_rdoc.rb, line 99
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) click to toggle source

Adds paragraph to the output

# File rdoc/markup/to_rdoc.rb, line 211
def accept_indented_paragraph paragraph
  @indent += paragraph.indent
  text = paragraph.text @hard_break
  wrap attributes text
  @indent -= paragraph.indent
end
accept_list_end(list) click to toggle source

Finishes consumption of list

# File rdoc/markup/to_rdoc.rb, line 110
def accept_list_end list
  @list_index.pop
  @list_type.pop
  @list_width.pop
end
accept_list_item_end(list_item) click to toggle source

Finishes consumption of list_item

# File rdoc/markup/to_rdoc.rb, line 119
def accept_list_item_end list_item
  width = case @list_type.last
          when :BULLET then
            2
          when :NOTE, :LABEL then
            if @prefix then
              @res << @prefix.strip
              @prefix = nil
            end

            @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) click to toggle source

Prepares the visitor for consuming list_item

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

  case type
  when :NOTE, :LABEL then
    stripped_labels = Array(list_item.label).map do |label|
      attributes(label).strip
    end

    bullets = case type
    when :NOTE
      stripped_labels.map { |b| "#{b}::" }
    when :LABEL
      stripped_labels.map { |b| "[#{b}]" }
    end

    bullets = bullets.join("\n")
    bullets << "\n" unless stripped_labels.empty?

    @prefix = ' ' * @indent
    @indent += 2
    @prefix << bullets + (' ' * @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) click to toggle source

Prepares the visitor for consuming list

# File rdoc/markup/to_rdoc.rb, line 176
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) click to toggle source

Adds paragraph to the output

# File rdoc/markup/to_rdoc.rb, line 203
def accept_paragraph paragraph
  text = paragraph.text @hard_break
  wrap attributes text
end
accept_raw(raw) click to toggle source

Adds raw to the output

# File rdoc/markup/to_rdoc.rb, line 221
def accept_raw raw
  @res << raw.parts.join("\n")
end
accept_rule(rule) click to toggle source

Adds rule to the output

# File rdoc/markup/to_rdoc.rb, line 228
def accept_rule rule
  use_prefix or @res << ' ' * @indent
  @res << '-' * (@width - @indent)
  @res << "\n"
end
accept_table(header, body, aligns) click to toggle source

Adds table to the output

# File rdoc/markup/to_rdoc.rb, line 251
def accept_table header, body, aligns
  widths = header.zip(*body).map do |cols|
    cols.map(&:size).max
  end
  aligns = aligns.map do |a|
    case a
    when nil
      :center
    when :left
      :ljust
    when :right
      :rjust
    end
  end
  @res << header.zip(widths, aligns).map do |h, w, a|
    h.__send__(a, w)
  end.join("|").rstrip << "\n"
  @res << widths.map {|w| "-" * w }.join("|") << "\n"
  body.each do |row|
    @res << row.zip(widths, aligns).map do |t, w, a|
      t.__send__(a, w)
    end.join("|").rstrip << "\n"
  end
end
accept_verbatim(verbatim) click to toggle source

Outputs verbatim indented 2 columns

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

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

  @res << "\n"
end
attributes(text) click to toggle source

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

# File rdoc/markup/to_rdoc.rb, line 279
def attributes text
  flow = @am.flow text.dup
  convert_flow flow
end
end_accepting() click to toggle source

Returns the generated output

# File rdoc/markup/to_rdoc.rb, line 287
def end_accepting
  @res.join
end
handle_regexp_HARD_BREAK(target) click to toggle source

Adds a newline to the output

# File rdoc/markup/to_rdoc.rb, line 303
def handle_regexp_HARD_BREAK target
  "\n"
end
handle_regexp_SUPPRESSED_CROSSREF(target) click to toggle source

Removes preceding \ from the suppressed crossref target

# File rdoc/markup/to_rdoc.rb, line 294
def handle_regexp_SUPPRESSED_CROSSREF target
  text = target.text
  text = text.sub('\\', '') unless in_tt?
  text
end
init_tags() click to toggle source

Maps attributes to HTML sequences

# File 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() click to toggle source

Prepares the visitor for text generation

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

  @list_index = []
  @list_type  = []
  @list_width = []
end
use_prefix() click to toggle source

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

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

  prefix
end
wrap(text) click to toggle source

Wraps text to width

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

  text_len = @width - @indent

  text_len = 20 if text_len < 20

  next_prefix = ' ' * @indent

  prefix = @prefix || next_prefix
  @prefix = nil

  text.scan(/\G(?:([^ \n]{#{text_len}})(?=[^ \n])|(.{1,#{text_len}})(?:[ \n]|\z))/) do
    @res << prefix << ($1 || $2) << "\n"
    prefix = next_prefix
  end
end