class RDoc::Comment
一个注释保存着 RDoc::CodeObject
的文本注释,并提供了一种统一的方式来清理它并将其解析为 RDoc::Markup::Document。
每个注释可以通过 format=
设置不同的标记格式。默认情况下使用 ‘rdoc’。 :markup: 指令告诉 RDoc
使用哪种格式。
属性
此注释的格式。默认为 RDoc::Markup
找到此注释的 RDoc::TopLevel
此注释的文本
此注释的文本
公共类方法
源代码
# File lib/rdoc/comment.rb, line 56 def initialize text = nil, location = nil, language = nil @location = location @text = text.nil? ? nil : text.dup @language = language @document = nil @format = 'rdoc' @normalized = false end
创建一个新的注释,其 text
在 RDoc::TopLevel location
中找到。
公共实例方法
源代码
# File lib/rdoc/comment.rb, line 128 def empty? @text.empty? end
如果注释的文本 String
为空,则该注释为空。
源代码
# File lib/rdoc/comment.rb, line 135 def encode! encoding @text = String.new @text, encoding: encoding self end
HACK 可疑
源代码
# File lib/rdoc/comment.rb, line 95 def extract_call_seq method # we must handle situations like the above followed by an unindented first # comment. The difficulty is to make sure not to match lines starting # with ARGF at the same indent, but that are after the first description # paragraph. if /^(?<S> ((?!\n)\s)*+ (?# whitespaces except newline)) :?call-seq: (?<B> \g<S>(?<N>\n|\z) (?# trailing spaces))? (?<seq> (\g<S>(?!\w)\S.*\g<N>)* (?> (?<H> \g<S>\w+ (?# ' # ARGF' in the example above)) .*\g<N>)? (\g<S>\S.*\g<N> (?# other non-blank line))*+ (\g<B>+(\k<H>.*\g<N> (?# ARGF.to_a lines))++)*+ ) (?m:^\s*$|\z) /x =~ @text seq = $~[:seq] all_start, all_stop = $~.offset(0) @text.slice! all_start...all_stop seq.gsub!(/^\s*/, '') method.call_seq = seq end method end
在注释中查找 ‘call-seq’ 以覆盖正常的参数处理。 :call-seq: 从基线缩进。将消耗相同缩进级别和前缀的所有行。
例如,以下所有内容都将用作 :call-seq
# :call-seq: # ARGF.readlines(sep=$/) -> array # ARGF.readlines(limit) -> array # ARGF.readlines(sep, limit) -> array # # ARGF.to_a(sep=$/) -> array # ARGF.to_a(limit) -> array # ARGF.to_a(sep, limit) -> array
源代码
# File lib/rdoc/comment.rb, line 143 def format= format @format = format @document = nil end
设置此注释的格式并重置任何已解析的文档
源代码
# File lib/rdoc/comment.rb, line 157 def normalize return self unless @text return self if @normalized # TODO eliminate duplicate normalization @text = normalize_comment @text @normalized = true self end
标准化文本。有关详细信息,请参阅 RDoc::Text#normalize_comment
源代码
# File lib/rdoc/comment.rb, line 179 def parse return @document if @document @document = super @text, @format @document.file = @location @document end
将注释解析为 RDoc::Markup::Document。已解析的文档会被缓存,直到文本更改为止。
调用父类方法
RDoc::Text#parse
源代码
# File lib/rdoc/comment.rb, line 200 def remove_private # Workaround for gsub encoding for Ruby 1.9.2 and earlier empty = '' empty = RDoc::Encoding.change_encoding empty, @text.encoding @text = @text.gsub(%r%^\s*([#*]?)--.*?^\s*(\1)\+\+\n?%m, empty) @text = @text.sub(%r%^\s*[#*]?--.*%m, '') end
从此注释中删除私有部分。私有部分与注释标记齐平,并以 --
开始,以 ++
结束。对于 C 风格的注释,私有标记可能不会从注释的开头开始。
/* *-- * private *++ * public */
源代码
# File lib/rdoc/comment.rb, line 214 def text= text raise RDoc::Error, 'replacing document-only comment is not allowed' if @text.nil? and @document @document = nil @text = text.nil? ? nil : text.dup end
用 text
替换此注释的文本并重置已解析的文档。
如果注释包含文档但没有文本,则会引发错误。
源代码
# File lib/rdoc/comment.rb, line 225 def tomdoc? @format == 'tomdoc' end
如果此注释采用 TomDoc 格式,则返回 true。