123456789_123456789_123456789_123456789_123456789_

Class: RDoc::Markup::ToMarkdown

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

Overview

Outputs parsed markup as ::RDoc::Markdown

Class Method Summary

ToRdoc - Inherited

.new

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

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

ToRdoc - Inherited

#indent

Current indent amount for output in characters.

#width

Output width in characters.

#list_index

Stack of current list indexes for alphabetic and numeric lists.

#list_type

Stack of list types.

#list_width

Stack of list widths for indentation.

#prefix

Prefix for the next list item.

#res

Output accumulator.

Formatter - Inherited

#in_tt?

Are we currently inside tt tags?

Instance Method Summary

ToRdoc - Inherited

#accept_blank_line

Adds blank_line to the output.

#accept_block_quote

Adds paragraph to the output.

#accept_heading

Adds heading to the output.

#accept_indented_paragraph

Adds paragraph to the output.

#accept_list_end

Finishes consumption of list

#accept_list_item_end

Finishes consumption of list_item

#accept_list_item_start

Prepares the visitor for consuming list_item

#accept_list_start

Prepares the visitor for consuming list

#accept_paragraph

Adds paragraph to the output.

#accept_raw

Adds raw to the output.

#accept_rule

Adds rule to the output.

#accept_verbatim

Outputs verbatim indented 2 columns.

#attributes

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

#end_accepting

Returns the generated output.

#handle_special_HARD_BREAK

Adds a newline to the output.

#handle_special_SUPPRESSED_CROSSREF

Removes preceding \ from the suppressed crossref special

#init_tags

Maps attributes to HTML sequences.

#start_accepting

Prepares the visitor for text generation.

#use_prefix

Adds the stored #prefix to the output and clears it.

#wrap

Wraps text to #width

Formatter - Inherited

#accept_document

Adds document to the output.

#add_special_RDOCLINK

Adds a special for links of the form rdoc-…:

#add_special_TIDYLINK

Adds a special for links of the form <text> and <#add_tag

Add a new set of tags for an attribute.

#annotate

Allows tag to be decorated with additional information.

#convert

Marks up content

#convert_flow

Converts flow items flow

#convert_special

Converts added specials.

#convert_string

Converts a string to be fancier if desired.

#ignore

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

#off_tags

Turns off tags for item on res

#on_tags

Turns on tags for item on res

#parse_url

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

#tt?

Is tag a tt tag?

Constructor Details

.new(markup = nil) ⇒ ToMarkdown

Creates a new formatter that will output ::RDoc::Markdown format text

[ GitHub ]

  
# File 'lib/rdoc/markup/to_markdown.rb', line 11

def initialize markup = nil
  super

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

  add_special_RDOCLINK
  add_special_TIDYLINK

  @hard_break = "  \n"
end

Instance Method Details

#accept_list_end(list)

Finishes consumption of list

[ GitHub ]

  
# File 'lib/rdoc/markup/to_markdown.rb', line 46

def accept_list_end list
  @res << "\n"

  super
end

#accept_list_item_end(list_item)

Finishes consumption of list_item

[ GitHub ]

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

def accept_list_item_end list_item
  width = case @list_type.last
          when :BULLET then
            4
          when :NOTE, :LABEL then
            use_prefix

            4
          else
            @list_index[-1] = @list_index.last.succ
            4
          end

  @indent -= width
end

#accept_list_item_start(list_item)

Prepares the visitor for consuming list_item

[ GitHub ]

  
# File 'lib/rdoc/markup/to_markdown.rb', line 74

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:"

    @prefix = ' ' * @indent
    @indent += 4
    @prefix << bullets + (' ' * (@indent - 1))
  else
    bullet = type == :BULLET ? '*' : @list_index.last.to_s + '.'
    @prefix = (' ' * @indent) + bullet.ljust(4)

    @indent += 4
  end
end

#accept_list_start(list)

Prepares the visitor for consuming list

[ GitHub ]

  
# File 'lib/rdoc/markup/to_markdown.rb', line 99

def accept_list_start list
  case list.type
  when :BULLET, :LABEL, :NOTE then
    @list_index << nil
  when :LALPHA, :NUMBER, :UALPHA then
    @list_index << 1
  else
    raise RDoc::Error, "invalid list type #{list.type}"
  end

  @list_width << 4
  @list_type << list.type
end

#accept_rule(rule)

Adds rule to the output

[ GitHub ]

  
# File 'lib/rdoc/markup/to_markdown.rb', line 116

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

#accept_verbatim(verbatim)

Outputs verbatim indented 4 columns

[ GitHub ]

  
# File 'lib/rdoc/markup/to_markdown.rb', line 125

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

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

  @res << "\n" unless @res =~ /\n\z/
end

#gen_url(url, text)

Creates a Markdown-style URL from url with text.

[ GitHub ]

  
# File 'lib/rdoc/markup/to_markdown.rb', line 139

def gen_url url, text
  scheme, url, = parse_url url

  "[#{text.sub(%r{^#{scheme}:/*}i, '')}](#{url})"
end

#handle_special_HARD_BREAK(special)

Adds a newline to the output

[ GitHub ]

  
# File 'lib/rdoc/markup/to_markdown.rb', line 39

def handle_special_HARD_BREAK special
  "  \n"
end

#init_tags

Maps attributes to HTML sequences

[ GitHub ]

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

def init_tags
  add_tag :BOLD, '**', '**'
  add_tag :EM,   '*',  '*'
  add_tag :TT,   '`',  '`'
end