Class: Bbcode::Parser
- Inherits:
-
Object
- Object
- 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) ⇒ Object
-
#initialize(tokenizer = nil) ⇒ Parser
constructor
A new instance of Parser.
- #parse(document, handler) ⇒ Object
- #start_element(tagname, attributes, source) ⇒ Object
- #text(text) ⇒ Object
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
#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) ⇒ 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 ) 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.send :interrupt_element, @tags_stack.pop end @handler.send :end_element, @tags_stack.pop, source while !@interruption_stack.empty? do @tags_stack << @interruption_stack.last @handler.send :continue_element, @interruption_stack.pop end end |
#parse(document, handler) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/bbcode/parser.rb', line 43 def parse( document, handler ) @handler = handler @tokenizer.tokenize document do |*args| self.send *args if [:start_element, :end_element, :text].include?(args.first) end end |
#start_element(tagname, attributes, source) ⇒ Object
20 21 22 23 |
# File 'lib/bbcode/parser.rb', line 20 def start_element( tagname, attributes, source ) @tags_stack << tagname @handler.send :start_element, tagname, attributes, source end |
#text(text) ⇒ Object
16 17 18 |
# File 'lib/bbcode/parser.rb', line 16 def text( text ) @handler.send :text, text end |