Class: Bbcode::Parser
- Inherits:
-
AbstractHandler
- Object
- AbstractHandler
- Bbcode::Parser
- Defined in:
- lib/bbcode/parser.rb
Overview
Attempts to pair a stream of tokens created by a tokenizer
Instance Attribute Summary collapse
-
#tokenizer ⇒ Object
Returns the value of attribute tokenizer.
Instance Method Summary collapse
- #end_element(tagname, source = nil) ⇒ Object
-
#initialize(tokenizer = nil) ⇒ Parser
constructor
A new instance of Parser.
- #parse(document, handler) ⇒ Object
- #start_element(tagname, attributes, source = nil) ⇒ Object
- #text(text) ⇒ Object
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 ) = [] self.tokenizer = tokenizer unless tokenizer.blank? end |
Instance Attribute Details
#tokenizer ⇒ Object
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 .last.blank? ? self.text(source) : end_element(.last, source) if tagname.blank? return self.text(source) unless .include?(tagname) @interruption_stack = [] while .last != tagname do @interruption_stack << .last @handler.interrupt_element .pop end @handler.end_element .pop, source while !@interruption_stack.empty? do << @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 ) << 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 |