Class: Bbcode::Parser

Inherits:
AbstractHandler show all
Defined in:
lib/bbcode/parser.rb

Overview

Attempts to pair a stream of tokens created by a tokenizer

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractHandler

#continue_element, #interrupt_element, #restart_element, #void_element

Constructor Details

#initialize(tokenizer = nil) ⇒ Parser

Returns a new instance of Parser.



6
7
8
9
# File 'lib/bbcode/parser.rb', line 6

def initialize( tokenizer = nil )
  @tags_stack = []
  self.tokenizer = tokenizer unless tokenizer.blank?
end

Instance Attribute Details

#tokenizerObject

Returns the value of attribute tokenizer.



4
5
6
# File 'lib/bbcode/parser.rb', line 4

def tokenizer
  @tokenizer
end

Instance Method Details

#end_element(tagname, source = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bbcode/parser.rb', line 25

def end_element( tagname, source = nil )
  return @tags_stack.last.blank? ? self.text(source) : end_element(@tags_stack.last, source) if tagname.blank?
  return self.text(source) unless @tags_stack.include?(tagname)

  @interruption_stack = []
  while @tags_stack.last != tagname do
    @interruption_stack << @tags_stack.last
    @handler.interrupt_element @tags_stack.pop
  end

  @handler.end_element @tags_stack.pop, source

  while !@interruption_stack.empty? do
    @tags_stack << @interruption_stack.last
    @handler.continue_element @interruption_stack.pop
  end
end

#parse(document, handler) ⇒ Object



43
44
45
46
# File 'lib/bbcode/parser.rb', line 43

def parse( document, handler )
  @handler = handler
  @tokenizer.tokenize document, self
end

#start_element(tagname, attributes, source = nil) ⇒ Object



20
21
22
23
# File 'lib/bbcode/parser.rb', line 20

def start_element( tagname, attributes, source = nil )
  @tags_stack << tagname
  @handler.start_element tagname, attributes, source
end

#text(text) ⇒ Object



16
17
18
# File 'lib/bbcode/parser.rb', line 16

def text( text )
  @handler.text text
end