123456789_123456789_123456789_123456789_123456789_

Class: RDoc::Markup::ToRdoc

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Formatter
Instance Chain:
self, Formatter
Inherits: RDoc::Markup::Formatter
Defined in: lib/rdoc/markup/to_rdoc.rb

Overview

Outputs RDoc markup as ::RDoc::RDoc markup! (mostly)

Constant Summary

Class Method Summary

Formatter - Inherited

.gen_relative_url

Converts a target url to one that is relative to a given path.

.new

Creates a new Formatter.

Instance Attribute Summary

  • #indent rw

    Current indent amount for output in characters.

  • #list_index readonly

    Stack of current list indexes for alphabetic and numeric lists.

  • #list_type readonly

    Stack of list types.

  • #list_width readonly

    Stack of list widths for indentation.

  • #prefix readonly

    Prefix for the next list item.

  • #res readonly

    Output accumulator.

  • #width rw

    Output width in characters.

Instance Method Summary

Formatter - Inherited

#accept_document

Adds document to the output.

#add_regexp_handling_RDOCLINK

Adds a regexp handling for links of the form rdoc-…:

#annotate

Allows tag to be decorated with additional information.

#apply_regexp_handling

Applies regexp handling to text and returns an array of [text, converted?] pairs.

#convert

Marks up content

#convert_string

Converts a string to be fancier if desired.

#handle_BOLD

Called when processing bold nodes while traversing inline nodes from handle_inline.

#handle_BOLD_WORD

Called when processing bold word nodes while traversing inline nodes from handle_inline.

#handle_EM

Called when processing emphasis nodes while traversing inline nodes from handle_inline.

#handle_EM_WORD

Called when processing emphasis word nodes while traversing inline nodes from handle_inline.

#handle_HARD_BREAK

Called when processing a hard break while traversing inline nodes from handle_inline.

#handle_inline

Parses inline text, traverse the resulting nodes, and calls the appropriate handler methods.

#handle_PLAIN_TEXT

Called when processing plain text while traversing inline nodes from handle_inline.

#handle_REGEXP_HANDLING_TEXT

Called when processing regexp-handling-processed text while traversing inline nodes from handle_inline.

#handle_STRIKE

Called when processing strike nodes while traversing inline nodes from handle_inline.

#handle_TEXT

Called when processing text node while traversing inline nodes from handle_inline.

#handle_TIDYLINK

Called when processing tidylink nodes while traversing inline nodes from handle_inline.

#handle_TT

Called when processing tt nodes while traversing inline nodes from handle_inline.

#ignore

Use ignore in your subclass to ignore the content of a node.

#parse_url

Extracts and a scheme, url and an anchor id from url and returns them.

#traverse_inline_nodes

Traverses nodes and calls the appropriate handler methods Nodes formats are described in InlineParser#parse

#tt?

Is tag a tt tag?

Constructor Details

.new(markup = nil) ⇒ ToRdoc

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

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 55

def initialize(markup = nil)
  super nil, markup

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

  @headings = DEFAULT_HEADINGS.dup
  @hard_break = "\n"
end

Instance Attribute Details

#indent (rw)

Current indent amount for output in characters

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 20

attr_accessor :indent

#list_index (readonly)

Stack of current list indexes for alphabetic and numeric lists

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 30

attr_reader :list_index

#list_type (readonly)

Stack of list types

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 35

attr_reader :list_type

#list_width (readonly)

Stack of list widths for indentation

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 40

attr_reader :list_width

#prefix (readonly)

Prefix for the next list item. See #use_prefix

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 45

attr_reader :prefix

#res (readonly)

Output accumulator

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 50

attr_reader :res

#width (rw)

Output width in characters

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 25

attr_accessor :width

Instance Method Details

#accept_blank_line(blank_line)

Adds blank_line to the output

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 68

def accept_blank_line(blank_line)
  @res << "\n"
end

#accept_block_quote(block_quote)

Adds paragraph to the output

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 75

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)

Adds heading to the output

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 90

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

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 202

def accept_indented_paragraph(paragraph)
  @indent += paragraph.indent
  text = paragraph.text @hard_break
  wrap attributes text
  @indent -= paragraph.indent
end

#accept_list_end(list)

Finishes consumption of list

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 101

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

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 110

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)

Prepares the visitor for consuming list_item

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 134

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)

Prepares the visitor for consuming list

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 167

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

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 194

def accept_paragraph(paragraph)
  text = paragraph.text @hard_break
  wrap attributes text
end

#accept_raw(raw)

Adds raw to the output

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 212

def accept_raw(raw)
  @res << raw.parts.join("\n")
end

#accept_rule(rule)

Adds rule to the output

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 219

def accept_rule(rule)
  use_prefix or @res << ' ' * @indent
  @res << '-' * (@width - @indent)
  @res << "\n"
end

#accept_table(header, body, aligns)

Adds table to the output

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 242

def accept_table(header, body, aligns)
  header = header.map { |h| attributes h }
  body = body.map { |row| row.map { |t| attributes t } }
  widths = header.zip(*body).map do |cols|
    cols.compact.map { |col| calculate_text_width(col) }.max
  end
  aligns = aligns.map do |a|
    case a
    when nil, :center
      :center
    when :left
      :ljust
    when :right
      :rjust
    end
  end
  @res << header.zip(widths, aligns).map do |h, w, a|
    extra_width = h.size - calculate_text_width(h)
    h.__send__(a, w + extra_width)
  end.join("|").rstrip << "\n"
  @res << widths.map {|w| "-" * w }.join("|") << "\n"
  body.each do |row|
    @res << widths.zip(aligns).each_with_index.map do |(w, a), i|
      t = row[i] || ""
      extra_width = t.size - calculate_text_width(t)
      t.__send__(a, w + extra_width)
    end.join("|").rstrip << "\n"
  end
end

#accept_verbatim(verbatim)

Outputs verbatim indented 2 columns

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 228

def accept_verbatim(verbatim)
  indent = ' ' * (@indent + 2)

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

  @res << "\n"
end

#add_text(text)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 347

def add_text(text)
  emit_inline(text)
end

#attributes(text)

Applies attribute-specific markup to text using InlineParser

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 358

def attributes(text)
  handle_inline(text)
end

#calculate_text_width(text)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 272

def calculate_text_width(text)
  text.size
end

#emit_inline(text)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 351

def emit_inline(text)
  @inline_output << text
end

#end_accepting

Returns the generated output

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 365

def end_accepting
  @res.join
end

#handle_BOLD(target)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 284

def handle_BOLD(target)
  on(:BOLD)
  super
  off(:BOLD)
end

#handle_BOLD_WORD(word)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 296

def handle_BOLD_WORD(word)
  on(:BOLD)
  super
  off(:BOLD)
end

#handle_EM(target)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 290

def handle_EM(target)
  on(:EM)
  super
  off(:EM)
end

#handle_EM_WORD(word)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 302

def handle_EM_WORD(word)
  on(:EM)
  super
  off(:EM)
end

#handle_HARD_BREAK

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 320

def handle_HARD_BREAK
  add_text("\n")
end

#handle_inline(text, initial_attributes = [])

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 329

def handle_inline(text, initial_attributes = [])
  @attributes = Hash.new(0)
  initial_attributes.each { |attr| on(attr) }
  out = @inline_output = +''
  super(text)
  @inline_output = nil
  out
end

#handle_PLAIN_TEXT(text)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 276

def handle_PLAIN_TEXT(text)
  add_text(text)
end

#handle_REGEXP_HANDLING_TEXT(text)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 280

def handle_REGEXP_HANDLING_TEXT(text)
  add_text(text)
end

#handle_regexp_SUPPRESSED_CROSSREF(text)

Removes preceding \ from the suppressed crossref target

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 372

def handle_regexp_SUPPRESSED_CROSSREF(text)
  text.sub('\\', '')
end

#handle_STRIKE(target)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 314

def handle_STRIKE(target)
  on(:STRIKE)
  super
  off(:STRIKE)
end

#handle_TT(code)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 308

def handle_TT(code)
  on(:TT)
  super
  off(:TT)
end

#off(attr)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 342

def off(attr)
  @attributes[attr] -= 1
  @attributes.delete(attr) if @attributes[attr] == 0
end

#on(attr)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 338

def on(attr)
  @attributes[attr] += 1
end

#start_accepting

Prepares the visitor for text generation

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 379

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.

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 393

def use_prefix
  prefix, @prefix = @prefix, nil
  @res << prefix if prefix

  prefix
end

#wrap(text)

Wraps text to #width

[ GitHub ]

  
# File 'lib/rdoc/markup/to_rdoc.rb', line 403

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