class RDoc::Markup::ToRdoc

RDoc标记输出为RDoc标记!(大部分情况)

属性

indent[RW]

输出的当前缩进量(以字符为单位)

list_index[R]

字母列表和数字列表的当前列表索引堆栈

list_type[R]

列表类型堆栈

list_width[R]

用于缩进的列表宽度堆栈

prefix[R]

下一个列表项的前缀。请参阅use_prefix

res[R]

输出累加器

width[RW]

输出宽度(以字符为单位)

公共类方法

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

创建一个新的格式化程序,它将输出(大部分)RDoc 标记

调用超类方法 RDoc::Markup::Formatter::new
# File rdoc/markup/to_rdoc.rb, line 45
def initialize markup = nil
  super nil, markup

  @markup.add_regexp_handling(/\\\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

公共实例方法

accept_blank_line(blank_line) 点击切换源代码

blank_line 添加到输出

# File rdoc/markup/to_rdoc.rb, line 77
def accept_blank_line blank_line
  @res << "\n"
end
accept_block_quote(block_quote) 点击切换源代码

paragraph 添加到输出

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

heading 添加到输出

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

paragraph 添加到输出

# File rdoc/markup/to_rdoc.rb, line 211
def accept_indented_paragraph paragraph
  @indent += paragraph.indent
  text = paragraph.text @hard_break
  wrap attributes text
  @indent -= paragraph.indent
end
accept_list_end(list) 点击切换源代码

完成 list 的消耗

# File rdoc/markup/to_rdoc.rb, line 110
def accept_list_end list
  @list_index.pop
  @list_type.pop
  @list_width.pop
end
accept_list_item_end(list_item) 点击切换源代码

完成 list_item 的消耗

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

准备访问者以消耗 list_item

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

准备访问者以消耗 list

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

paragraph 添加到输出

# File rdoc/markup/to_rdoc.rb, line 203
def accept_paragraph paragraph
  text = paragraph.text @hard_break
  wrap attributes text
end
accept_raw(raw) 点击切换源代码

raw 添加到输出

# File rdoc/markup/to_rdoc.rb, line 221
def accept_raw raw
  @res << raw.parts.join("\n")
end
accept_rule(rule) 点击切换源代码

rule 添加到输出

# File rdoc/markup/to_rdoc.rb, line 228
def accept_rule rule
  use_prefix or @res << ' ' * @indent
  @res << '-' * (@width - @indent)
  @res << "\n"
end
accept_table(header, body, aligns) 点击切换源代码

table 添加到输出

# File rdoc/markup/to_rdoc.rb, line 251
def accept_table header, body, aligns
  widths = header.zip(*body).map do |cols|
    cols.map(&:size).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|
    h.__send__(a, w)
  end.join("|").rstrip << "\n"
  @res << widths.map {|w| "-" * w }.join("|") << "\n"
  body.each do |row|
    @res << row.zip(widths, aligns).map do |t, w, a|
      t.__send__(a, w)
    end.join("|").rstrip << "\n"
  end
end
accept_verbatim(verbatim) 点击切换源代码

输出缩进 2 列的 verbatim

# File rdoc/markup/to_rdoc.rb, line 237
def accept_verbatim verbatim
  indent = ' ' * (@indent + 2)

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

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

使用 RDoc::AttributeManager 将特定于属性的标记应用于 text

# File rdoc/markup/to_rdoc.rb, line 279
def attributes text
  flow = @am.flow text.dup
  convert_flow flow
end
end_accepting() 点击切换源代码

返回生成的输出

# File rdoc/markup/to_rdoc.rb, line 287
def end_accepting
  @res.join
end
handle_regexp_HARD_BREAK(target) 点击切换源代码

向输出添加换行符

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

从抑制的交叉引用 target 中删除前面的 \

# File rdoc/markup/to_rdoc.rb, line 294
def handle_regexp_SUPPRESSED_CROSSREF target
  text = target.text
  text = text.sub('\\', '') unless in_tt?
  text
end
init_tags() 点击切换源代码

将属性映射到 HTML 序列

# File rdoc/markup/to_rdoc.rb, line 68
def init_tags
  add_tag :BOLD, "<b>", "</b>"
  add_tag :TT,   "<tt>", "</tt>"
  add_tag :EM,   "<em>", "</em>"
end
start_accepting() 点击切换源代码

准备访问者进行文本生成

# File rdoc/markup/to_rdoc.rb, line 310
def start_accepting
  @res = [""]
  @indent = 0
  @prefix = nil

  @list_index = []
  @list_type  = []
  @list_width = []
end
use_prefix() 点击切换源代码

将存储的prefix添加到输出并清除它。列表生成一个前缀供以后使用。

# File rdoc/markup/to_rdoc.rb, line 324
def use_prefix
  prefix, @prefix = @prefix, nil
  @res << prefix if prefix

  prefix
end
wrap(text) 点击切换源代码

text 换行到 width

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