class RDoc::Markup::ToHtmlSnippet

RDoc 标记输出为仅包含内联标记的段落。

属性

character_limit[R]

在此字符数之后,输入将被截断。

mask[R]

属性位掩码

paragraph_limit[R]

在此段落数之后,输入将被截断。

paragraphs[R]

已找到的段落计数

公共类方法

new(options, characters = 100, paragraphs = 3, markup = nil) 点击以切换源代码

创建一个新的 ToHtmlSnippet 格式化程序,在遇到给定数量的 charactersparagraphs 文本后,将在下一个单词边界处截断输入。

调用超类方法 RDoc::Markup::ToHtml::new
# File rdoc/markup/to_html_snippet.rb, line 37
def initialize options, characters = 100, paragraphs = 3, markup = nil
  super options, markup

  @character_limit = characters
  @paragraph_limit = paragraphs

  @characters = 0
  @mask       = 0
  @paragraphs = 0

  @markup.add_regexp_handling RDoc::CrossReference::CROSSREF_REGEXP, :CROSSREF
end

公共实例方法

accept_heading(heading) 点击以切换源代码

heading 作为段落添加到输出中

# File rdoc/markup/to_html_snippet.rb, line 53
def accept_heading heading
  @res << "<p>#{to_html heading.text}\n"

  add_paragraph
end
accept_list_item_end(list_item) 点击以切换源代码

完成对 list_item 的消费

# File rdoc/markup/to_html_snippet.rb, line 85
def accept_list_item_end list_item
end
accept_list_item_start(list_item) 点击以切换源代码

准备访问者以消费 list_item

# File rdoc/markup/to_html_snippet.rb, line 91
def accept_list_item_start list_item
  @res << list_item_start(list_item, @list.last)
end
accept_list_start(list) 点击以切换源代码

准备访问者以消费 list

# File rdoc/markup/to_html_snippet.rb, line 98
def accept_list_start list
  @list << list.type
  @res << html_list_name(list.type, true)
  @in_list_entry.push ''
end
accept_paragraph(paragraph) 点击以切换源代码

paragraph 添加到输出中

# File rdoc/markup/to_html_snippet.rb, line 72
def accept_paragraph paragraph
  para = @in_list_entry.last || "<p>"

  text = paragraph.text @hard_break

  @res << "#{para}#{to_html text}\n"

  add_paragraph
end
accept_verbatim(verbatim) 点击以切换源代码

verbatim 添加到输出中

调用超类方法
# File rdoc/markup/to_html_snippet.rb, line 107
def accept_verbatim verbatim
  throw :done if @characters >= @character_limit
  input = verbatim.text.rstrip

  text = truncate input
  text << ' ...' unless text == input

  super RDoc::Markup::Verbatim.new text

  add_paragraph
end
add_paragraph() 点击以切换源代码

当遇到 paragraph_limit 段落时,抛出 :done

# File rdoc/markup/to_html_snippet.rb, line 198
def add_paragraph
  @paragraphs += 1

  throw :done if @paragraphs >= @paragraph_limit
end
convert(content) 点击以切换源代码

标记 content

# File rdoc/markup/to_html_snippet.rb, line 207
def convert content
  catch :done do
    return super
  end

  end_accepting
end
convert_flow(flow) 点击以切换源代码

转换流程项 flow

# File rdoc/markup/to_html_snippet.rb, line 218
def convert_flow flow
  throw :done if @characters >= @character_limit

  res = []
  @mask = 0

  flow.each do |item|
    case item
    when RDoc::Markup::AttrChanger then
      off_tags res, item
      on_tags  res, item
    when String then
      text = convert_string item
      res << truncate(text)
    when RDoc::Markup::RegexpHandling then
      text = convert_regexp_handling item
      res << truncate(text)
    else
      raise "Unknown flow element: #{item.inspect}"
    end

    if @characters >= @character_limit then
      off_tags res, RDoc::Markup::AttrChanger.new(0, @mask)
      break
    end
  end

  res << ' ...' if @characters >= @character_limit

  res.join
end
gen_url(url, text) 点击以切换源代码

仅返回 link 的文本,url 仅用于确定链接类型。

# File rdoc/markup/to_html_snippet.rb, line 171
def gen_url url, text
  if url =~ /^rdoc-label:([^:]*)(?::(.*))?/ then
    type = "link"
  elsif url =~ /([A-Za-z]+):(.*)/ then
    type = $1
  else
    type = "http"
  end

  if (type == "http" or type == "https" or type == "link") and
     url =~ /\.(gif|png|jpg|jpeg|bmp)$/ then
    ''
  else
    text.sub(%r%^#{type}:/*%, '')
  end
end
handle_regexp_CROSSREF(target) 点击以切换源代码

target 中的交叉引用中删除转义符

# File rdoc/markup/to_html_snippet.rb, line 131
def handle_regexp_CROSSREF target
  target.text.sub(/\A\\/, '')
end
handle_regexp_HARD_BREAK(target) 点击以切换源代码

target 是一个 <br>

# File rdoc/markup/to_html_snippet.rb, line 138
def handle_regexp_HARD_BREAK target
  @characters -= 4
  '<br>'
end
html_list_name(list_type, open_tag) 点击以切换源代码

在代码片段中,没有列表

# File rdoc/markup/to_html_snippet.rb, line 191
def html_list_name list_type, open_tag
  ''
end
list_item_start(list_item, list_type) 点击以切换源代码

列表是段落,但注释和标签有一个分隔符

# File rdoc/markup/to_html_snippet.rb, line 146
def list_item_start list_item, list_type
  throw :done if @characters >= @character_limit

  case list_type
  when :BULLET, :LALPHA, :NUMBER, :UALPHA then
    "<p>"
  when :LABEL, :NOTE then
    labels = Array(list_item.label).map do |label|
      to_html label
    end.join ', '

    labels << " &mdash; " unless labels.empty?

    start = "<p>#{labels}"
    @characters += 1 # try to include the label
    start
  else
    raise RDoc::Error, "Invalid list type: #{list_type.inspect}"
  end
end
off_tags(res, item) 点击以切换源代码

维护一个位掩码,以便正确关闭 HTML 元素。请参阅 RDoc::Markup::Formatter

# File rdoc/markup/to_html_snippet.rb, line 264
def off_tags res, item
  @mask ^= item.turn_off

  super
end
on_tags(res, item) 点击以切换源代码

维护一个位掩码,以便正确关闭 HTML 元素。请参阅 RDoc::Markup::Formatter

# File rdoc/markup/to_html_snippet.rb, line 254
def on_tags res, item
  @mask ^= item.turn_on

  super
end
start_accepting() 点击以切换源代码

准备访问者以生成 HTML 代码片段

调用超类方法
# File rdoc/markup/to_html_snippet.rb, line 122
def start_accepting
  super

  @characters = 0
end
truncate(text) 点击以切换源代码

在 character_limit 之后,在第一个单词的末尾截断 text

# File rdoc/markup/to_html_snippet.rb, line 273
def truncate text
  length = text.length
  characters = @characters
  @characters += length

  return text if @characters < @character_limit

  remaining = @character_limit - characters

  text =~ /\A(.{#{remaining},}?)(\s|$)/m # TODO word-break instead of \s?

  $1
end