Class: RDoc::Markup::ToRdoc

### Home Classes Methods

### In Files

- lib/rdoc/markup/to_rdoc.rb

### Parent

RDoc::Markup::Formatter

### Methods

- ::new
- #accept_blank_line
- #accept_block_quote
- #accept_heading
- #accept_indented_paragraph
- #accept_list_end
- #accept_list_item_end
- #accept_list_item_start
- #accept_list_start
- #accept_paragraph
- #accept_raw
- #accept_rule
- #accept_verbatim
- #attributes
- #end_accepting
- #handle_special_HARD_BREAK
- #handle_special_SUPPRESSED_CROSSREF
- #init_tags
- #start_accepting
- #use_prefix
- #wrap

### Files

- CONTRIBUTING.rdoc
- CVE-2013-0256.rdoc
- ExampleRDoc.rdoc
- LICENSE.rdoc
- Manifest.txt
- README.rdoc
- RI.rdoc
- Rakefile
- TODO.rdoc

### Class/Module Index

Quicksearch

- RDoc
- RDoc::Alias
- RDoc::AnonClass
- RDoc::AnyMethod
- RDoc::Attr
- RDoc::ClassModule
- RDoc::CodeObject
- RDoc::Comment
- RDoc::Constant
- RDoc::Context
- RDoc::Context::Section
- RDoc::CrossReference
- RDoc::ERBIO
- RDoc::ERBPartial
- RDoc::Encoding
- RDoc::Error
- RDoc::Extend
- RDoc::Gauntlet
- RDoc::Generator
- RDoc::Generator::Darkfish
- RDoc::Generator::JsonIndex
- RDoc::Generator::Markup
- RDoc::Generator::POT
- RDoc::Generator::RI
- RDoc::GhostMethod
- RDoc::Include
- RDoc::Markdown
- RDoc::Markdown::Literals
- RDoc::Markup
- RDoc::Markup::AttrChanger
- RDoc::Markup::AttrSpan
- RDoc::Markup::AttributeManager
- RDoc::Markup::Attributes
- RDoc::Markup::BlankLine
- RDoc::Markup::BlockQuote
- RDoc::Markup::Document
- RDoc::Markup::Formatter
- RDoc::Markup::FormatterTestCase
- RDoc::Markup::HardBreak
- RDoc::Markup::Include
- RDoc::Markup::IndentedParagraph
- RDoc::Markup::List
- RDoc::Markup::ListItem
- RDoc::Markup::Paragraph
- RDoc::Markup::Parser
- RDoc::Markup::Parser::Error
- RDoc::Markup::Parser::ParseError
- RDoc::Markup::PreProcess
- RDoc::Markup::Raw
- RDoc::Markup::Rule
- RDoc::Markup::Special
- RDoc::Markup::TextFormatterTestCase
- RDoc::Markup::ToAnsi
- RDoc::Markup::ToBs
- RDoc::Markup::ToHtml
- RDoc::Markup::ToHtmlCrossref
- RDoc::Markup::ToHtmlSnippet
- RDoc::Markup::ToJoinedParagraph
- RDoc::Markup::ToLabel
- RDoc::Markup::ToMarkdown
- RDoc::Markup::ToRdoc
- RDoc::Markup::ToTableOfContents
- RDoc::Markup::ToTest
- RDoc::Markup::ToTtOnly
- RDoc::Markup::Verbatim
- RDoc::MetaMethod
- RDoc::MethodAttr
- RDoc::Mixin
- RDoc::NormalClass
- RDoc::NormalModule
- RDoc::Options
- RDoc::Parser
- RDoc::Parser::C
- RDoc::Parser::ChangeLog
- RDoc::Parser::Markdown
- RDoc::Parser::RD
- RDoc::Parser::Ruby
- RDoc::Parser::RubyTools
- RDoc::Parser::Simple
- RDoc::Parser::Text
- RDoc::RD
- RDoc::RD::BlockParser
- RDoc::RD::Inline
- RDoc::RD::InlineParser
- RDoc::RDoc
- RDoc::RI
- RDoc::RI::Driver
- RDoc::RI::Driver::Error
- RDoc::RI::Driver::NotFoundError
- RDoc::RI::Error
- RDoc::RI::Paths
- RDoc::Require
- RDoc::RubyLex
- RDoc::RubyLex::Error
- RDoc::RubyToken
- RDoc::RubygemsHook
- RDoc::Servlet
- RDoc::SingleClass
- RDoc::Stats
- RDoc::Stats::Normal
- RDoc::Stats::Quiet
- RDoc::Stats::Verbose
- RDoc::Store
- RDoc::Store::Error
- RDoc::Store::MissingFileError
- RDoc::Task
- RDoc::Task
- RDoc::TestCase
- RDoc::TokenStream
- RDoc::TomDoc
- RDoc::TopLevel
- Object

No matching classes.

# 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

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

      @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] = ['====== ', '']

      @hard_break = "\n"
    end

### Public Instance Methods

accept_blank_line(blank_line) click to toggle source

Adds `blank_line` to the output

    # File lib/rdoc/markup/to_rdoc.rb, line 76
    def accept_blank_line blank_line
      @res << "\n"
    end

accept_block_quote(block_quote) click to toggle source

Adds `paragraph` to the output

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

Finishes consumption of `list`

    # File lib/rdoc/markup/to_rdoc.rb, line 109
    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 lib/rdoc/markup/to_rdoc.rb, line 118
    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 lib/rdoc/markup/to_rdoc.rb, line 142
    def accept_list_item_start list_item
      type = @list_type.last

      case type
      when :NOTE, :LABEL then
        bullets = Array(list_item.label).map do |label|
          attributes(label).strip
        end.join "\n"

        bullets << ":\n" unless bullets.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 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) click to toggle source

Adds `paragraph` to the output

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

Adds `raw` to the output

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

accept_rule(rule) click to toggle source

Adds `rule` to the output

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

Outputs `verbatim` indented 2 columns

    # 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" unless @res =~ /\n\z/
    end

attributes(text) click to toggle source

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

    # File lib/rdoc/markup/to_rdoc.rb, line 242
    def attributes text
      flow = @am.flow text.dup
      convert_flow flow
    end

end_accepting() click to toggle source

Returns the generated output

    # File lib/rdoc/markup/to_rdoc.rb, line 250
    def end_accepting
      @res.join
    end

handle_special_HARD_BREAK(special) click to toggle source

Adds a newline to the output

    # File lib/rdoc/markup/to_rdoc.rb, line 266
    def handle_special_HARD_BREAK special
      "\n"
    end

handle_special_SUPPRESSED_CROSSREF(special) click to toggle source

Removes preceding \ from the suppressed crossref `special`

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

init_tags() click to toggle source

Maps attributes to HTML sequences

    # File lib/rdoc/markup/to_rdoc.rb, line 67
    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 lib/rdoc/markup/to_rdoc.rb, line 273
    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 lib/rdoc/markup/to_rdoc.rb, line 287
    def use_prefix
      prefix, @prefix = @prefix, nil
      @res << prefix if prefix

      prefix
    end

wrap(text) click to toggle source

Wraps `text` to width

    # File lib/rdoc/markup/to_rdoc.rb, line 297
    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

[Validate]

Generated with the Darkfish Rdoc Generator 2.