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

Constant Summary

ToRdoc - Inherited

DEFAULT_HEADINGS

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.

#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.

#width

Output width in characters.

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_table

Adds table to the output.

#accept_verbatim

Outputs verbatim indented 2 columns.

#add_text,
#attributes

Applies attribute-specific markup to text using InlineParser

#calculate_text_width, #emit_inline,
#end_accepting

Returns the generated output.

#handle_BOLD, #handle_BOLD_WORD, #handle_EM, #handle_EM_WORD, #handle_HARD_BREAK, #handle_inline, #handle_PLAIN_TEXT, #handle_REGEXP_HANDLING_TEXT,
#handle_regexp_SUPPRESSED_CROSSREF

Removes preceding \ from the suppressed crossref target

#handle_STRIKE, #handle_TIDYLINK, #handle_TT, #off, #on,
#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_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) ⇒ ToMarkdown

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

[ GitHub ]

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

def initialize(markup = nil)
  super

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

  add_regexp_handling_RDOCLINK

  @hard_break = "  \n"
end

Instance Method Details

#accept_list_end(list)

Finishes consumption of list

[ GitHub ]

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

def accept_list_end(list)
  super
end

#accept_list_item_end(list_item)

Finishes consumption of list_item

[ GitHub ]

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

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

            @res << "\n"

            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 58

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 += 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 142

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 159

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 168

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

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

  @res << "\n"
end

#add_tag(tag, simple_tag, content)

[ GitHub ]

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

def add_tag(tag, simple_tag, content)
  if content.match?(/\A[\w\s]+\z/)
    emit_inline("#{simple_tag}#{content}#{simple_tag}")
  else
    emit_inline("<#{tag}>#{content}</#{tag}>")
  end
end

#gen_url(url, text)

Creates a Markdown-style URL from url with text.

[ GitHub ]

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

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

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

#handle_BOLD(nodes)

[ GitHub ]

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

def handle_BOLD(nodes)
  handle_tag(nodes, '**', 'strong')
end

#handle_BOLD_WORD(word)

[ GitHub ]

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

def handle_BOLD_WORD(word)
  add_tag('strong', '**', convert_string(word))
end

#handle_EM(nodes)

[ GitHub ]

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

def handle_EM(nodes)
  handle_tag(nodes, '*', 'em')
end

#handle_EM_WORD(word)

[ GitHub ]

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

def handle_EM_WORD(word)
  add_tag('em', '*', convert_string(word))
end

#handle_HARD_BREAK

[ GitHub ]

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

def handle_HARD_BREAK
  emit_inline("  \n")
end

#handle_STRIKE(nodes)

[ GitHub ]

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

def handle_STRIKE(nodes)
  handle_tag(nodes, '~~', 's')
end

#handle_tag(nodes, simple_tag, tag)

[ GitHub ]

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

def handle_tag(nodes, simple_tag, tag)
  if nodes.size == 1 && String === nodes[0]
    content = apply_regexp_handling(nodes[0]).map do |text, converted|
      converted ? text : convert_string(text)
    end.join
    add_tag(tag, simple_tag, content)
  else
    emit_inline("<#{tag}>")
    traverse_inline_nodes(nodes)
    emit_inline("</#{tag}>")
  end
end

#handle_TT(text)

[ GitHub ]

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

def handle_TT(text)
  add_tag('code', '`', convert_string(text))
end