class RDoc::Markup::ToMarkdown

将解析后的标记输出为 Markdown

公共类方法

new(markup = nil) 点击切换源代码

创建一个新的格式化程序,它将输出 Markdown 格式的文本

调用父类方法 RDoc::Markup::ToRdoc::new
# File 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
  add_regexp_handling_TIDYLINK

  @hard_break = "  \n"
end

公共实例方法

accept_list_end(list) 点击切换源代码

完成对 list 的消费

# File rdoc/markup/to_markdown.rb, line 47
def accept_list_end list
  super
end
accept_list_item_end(list_item) 点击切换源代码

完成对 list_item 的消费

# File rdoc/markup/to_markdown.rb, line 54
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) 点击切换源代码

准备访问者以消费 list_item

# File rdoc/markup/to_markdown.rb, line 75
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) 点击切换源代码

准备访问者以消费 list

# File rdoc/markup/to_markdown.rb, line 100
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) 点击切换源代码

rule 添加到输出

# File rdoc/markup/to_markdown.rb, line 117
def accept_rule rule
  use_prefix or @res << ' ' * @indent
  @res << '-' * 3
  @res << "\n"
end
accept_verbatim(verbatim) 点击切换源代码

输出缩进 4 列的 verbatim

# File rdoc/markup/to_markdown.rb, line 126
def accept_verbatim verbatim
  indent = ' ' * (@indent + 4)

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

  @res << "\n"
end
gen_url(url, text) 点击切换源代码

使用 texturl 创建 Markdown 样式的 URL。

# File rdoc/markup/to_markdown.rb, line 140
def gen_url url, text
  scheme, url, = parse_url url

  "[#{text.sub(%r{^#{scheme}:/*}i, '')}](#{url})"
end
handle_regexp_HARD_BREAK(target) 点击切换源代码

向输出添加一个换行符

# File rdoc/markup/to_markdown.rb, line 40
def handle_regexp_HARD_BREAK target
  "  \n"
end
init_tags() 点击切换源代码

将属性映射到 HTML 序列

# File rdoc/markup/to_markdown.rb, line 31
def init_tags
  add_tag :BOLD, '**', '**'
  add_tag :EM,   '*',  '*'
  add_tag :TT,   '`',  '`'
end