class RDoc::RD::BlockParser
用于解析标题、段落、列表、逐字块的 RD
格式解析器,这些元素以块的形式存在。
属性
footnotes[R]
此文档的脚注
include_path[RW]
查找包含文件的路径
labels[R]
此文档中项目的标签
公共类方法
new() 单击以切换源代码
创建一个新的 RDoc::RD::BlockParser
。使用 parse
来解析 rd 格式的文档。
# File rdoc/rd/block_parser.rb, line 695 def initialize @inline_parser = RDoc::RD::InlineParser.new self @include_path = [] # for testing @footnotes = [] @labels = {} end
公共实例方法
add_footnote(content) 单击以切换源代码
向文档添加脚注 content
# File rdoc/rd/block_parser.rb, line 1045 def add_footnote content index = @footnotes.length / 2 + 1 footmark_link = "{^#{index}}[rdoc-label:footmark-#{index}:foottext-#{index}]" @footnotes << RDoc::Markup::Paragraph.new(footmark_link, ' ', *content) @footnotes << RDoc::Markup::BlankLine.new index end
add_label(label) 单击以切换源代码
向文档添加标签 label
# File rdoc/rd/block_parser.rb, line 1059 def add_label label @labels[label] = true label end
content(values) 单击以切换源代码
将 values
的内容检索为单个字符串
# File rdoc/rd/block_parser.rb, line 1028 def content values values.map { |value| value.content }.join end
line_index() 单击以切换源代码
当前行号
# File rdoc/rd/block_parser.rb, line 983 def line_index @i end
on_error(et, ev, _values) 单击以切换源代码
当发现无效格式时引发 ParseError
# File rdoc/rd/block_parser.rb, line 967 def on_error(et, ev, _values) prv, cur, nxt = format_line_num(@i, @i+1, @i+2) raise ParseError, <<Msg RD syntax error: line #{@i+1}: #{prv} |#{@src[@i-1].chomp} #{cur}=>|#{@src[@i].chomp} #{nxt} |#{@src[@i+1].chomp} Msg end
paragraph(value) 单击以切换源代码
为 value
创建一个段落
# File rdoc/rd/block_parser.rb, line 1035 def paragraph value content = cut_off(value).join(' ').rstrip contents = @inline_parser.parse content RDoc::Markup::Paragraph.new(*contents) end
parse(src) 单击以切换源代码
解析 src
并返回一个 RDoc::Markup::Document
。
# File rdoc/rd/block_parser.rb, line 707 def parse src @src = src @src.push false @footnotes = [] @labels = {} # @i: index(line no.) of src @i = 0 # stack for current indentation @indent_stack = [] # how indented. @current_indent = @indent_stack.join("") # RDoc::RD::BlockParser for tmp src @subparser = nil # which part is in now @in_part = nil @part_content = [] @in_verbatim = false @yydebug = true document = do_parse unless @footnotes.empty? then blankline = document.parts.pop document.parts << RDoc::Markup::Rule.new(1) document.parts.concat @footnotes document.parts.push blankline end document end
私有实例方法
cut_off(src) 单击以切换源代码
裁剪掉 src
中多余的空格
# File rdoc/rd/block_parser.rb, line 931 def cut_off(src) ret = [] whiteline_buf = [] line = src.shift /^\s*/ =~ line indent = Regexp.quote($&) ret.push($') while line = src.shift if /^(\s*)$/ =~ line whiteline_buf.push(line) elsif /^#{indent}/ =~ line unless whiteline_buf.empty? ret.concat(whiteline_buf) whiteline_buf.clear end ret.push($') else raise "[BUG]: probably Parser Error while cutting off.\n" end end ret end
format_line_num(*line_numbers) 单击以切换源代码
漂亮地格式化行号 line_numbers
# File rdoc/rd/block_parser.rb, line 1019 def format_line_num(*line_numbers) width = line_numbers.collect{|i| i.to_s.length }.max line_numbers.collect{|i| sprintf("%#{width}d", i) } end
get_included(file) 单击以切换源代码
从 include_path
中检索 file
的内容
# File rdoc/rd/block_parser.rb, line 1000 def get_included(file) included = [] @include_path.each do |dir| file_name = File.join dir, file if File.exist? file_name then included = File.readlines file_name break end end included end
if_current_indent_equal(indent) { || ... } 单击以切换源代码
如果 indent
与当前缩进匹配,则 yield 到给定的块,否则处理缩进令牌。
# File rdoc/rd/block_parser.rb, line 913 def if_current_indent_equal(indent) indent = indent.sub(/\t/, "\s" * 8) if @current_indent == indent @i += 1 # next line yield elsif indent.index(@current_indent) == 0 @indent_stack.push(indent[@current_indent.size .. -1]) [:INDENT, ":INDENT"] else @indent_stack.pop [:DEDENT, ":DEDENT"] end end
parse_subtree(src) 单击以切换源代码
解析子树 src
# File rdoc/rd/block_parser.rb, line 990 def parse_subtree src @subparser ||= RDoc::RD::BlockParser.new @subparser.parse src end
set_term_to_element(parent, term) 单击以切换源代码
# File rdoc/rd/block_parser.rb, line 958 def set_term_to_element(parent, term) # parent.set_term_under_document_struct(term, @tree.document_struct) parent.set_term_without_document_struct(term) end