123456789_123456789_123456789_123456789_123456789_

Class: RDoc::RD::BlockParser

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Racc::Parser
Instance Chain:
self, Racc::Parser
Inherits: Racc::Parser
  • Object
Defined in: lib/rdoc/rd/block_parser.rb

Overview

::RDoc::RD format parser for headings, paragraphs, lists, verbatim sections that exist as blocks.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newBlockParser

Creates a new BlockParser. Use #parse to parse an rd-format document.

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 53

def initialize
  @inline_parser = RDoc::RD::InlineParser.new self
  @include_path = []

  # for testing
  @footnotes = []
  @labels    = {}
end

Instance Attribute Details

#footnotes (readonly)

Footnotes for this document

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 37

attr_reader :footnotes

#include_path (rw)

Path to find included files in

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 47

attr_accessor :include_path

#labels (readonly)

Labels for items in this document

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 42

attr_reader :labels

Instance Method Details

#add_footnote(content)

Adds footnote #content to the document

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 397

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)

Adds label label to the document

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 411

def add_label label
  @labels[label] = true

  label
end

#content(values)

Retrieves the content of values as a single String

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 380

def content values
 values.map { |value| value.content }.join
end

#cut_off(src) (private)

Cuts off excess whitespace in src

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 283

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) (private)

Formats line numbers line_numbers prettily

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 371

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) (private)

Retrieves the content for file from the include_path

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 352

def get_included(file)
  included = []

  @include_path.each do |dir|
    file_name = File.join dir, file

    if File.exist? file_name then
      included = IO.readlines file_name
      break
    end
  end

  included
end

#if_current_indent_equal(indent) (private)

Yields to the given block if indent matches the current indent, otherwise an indentation token is processed.

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 265

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

#line_index

Current line number

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 335

def line_index
  @i
end

#on_error(et, ev, _values)

Raises a ParseError when invalid formatting is found

Raises:

  • (ParseError)
[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 319

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)

Creates a paragraph for value

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 387

def paragraph value
  content = cut_off(value).join(' ').rstrip
  contents = @inline_parser.parse content

  RDoc::Markup::Paragraph.new(*contents)
end

#parse(src)

Parses src and returns an ::RDoc::Markup::Document.

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 65

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

#parse_subtree(src) (private)

Parses subtree src

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 342

def parse_subtree src
  @subparser ||= RDoc::RD::BlockParser.new

  @subparser.parse src
end

#set_term_to_element(parent, term) (private)

[ GitHub ]

  
# File 'lib/rdoc/rd/block_parser.rb', line 310

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