Class: Bbcode::Handler
- Inherits:
-
Object
- Object
- Bbcode::Handler
- Defined in:
- lib/bbcode/handler.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#element_handlers ⇒ Object
Returns the value of attribute element_handlers.
-
#locals ⇒ Object
Returns the value of attribute locals.
Instance Method Summary collapse
- #apply_element_handler_for_element(element) ⇒ Object
- #clear ⇒ Object
- #continue_element(tagname) ⇒ Object
- #end_element(tagname, source) ⇒ Object
- #get_document ⇒ Object
- #get_element_handler(name) ⇒ Object
-
#initialize(element_handlers = nil) ⇒ Handler
constructor
A new instance of Handler.
- #interrupt_element(tagname) ⇒ Object
- #register_element_handler(name, handler) ⇒ Object
- #register_element_handlers(element_handlers) ⇒ Object
- #start_element(tagname, attributes, source) ⇒ Object
- #text(text) ⇒ Object
Constructor Details
#initialize(element_handlers = nil) ⇒ Handler
Returns a new instance of Handler.
7 8 9 10 11 12 13 14 |
# File 'lib/bbcode/handler.rb', line 7 def initialize( element_handlers = nil ) @element_handlers = {}.with_indifferent_access @handler_element_stack = [] self.clear register_element_handlers element_handlers unless element_handlers.blank? @interruption_stack = [] self.locals = {} end |
Instance Attribute Details
#element_handlers ⇒ Object
Returns the value of attribute element_handlers.
5 6 7 |
# File 'lib/bbcode/handler.rb', line 5 def element_handlers @element_handlers end |
#locals ⇒ Object
Returns the value of attribute locals.
5 6 7 |
# File 'lib/bbcode/handler.rb', line 5 def locals @locals end |
Instance Method Details
#apply_element_handler_for_element(element) ⇒ Object
71 72 73 74 |
# File 'lib/bbcode/handler.rb', line 71 def apply_element_handler_for_element(element) callable = get_element_handler(element.is_a?(String) ? :"#text" : element.tagname) callable.arity == 2 ? callable.call(element, locals) : callable.call(element) end |
#clear ⇒ Object
58 59 60 61 |
# File 'lib/bbcode/handler.rb', line 58 def clear @handler_element_stack.clear @handler_element_stack << HandlerElement.new( self, :"#document", {}, "" ) end |
#continue_element(tagname) ⇒ Object
38 39 40 41 42 |
# File 'lib/bbcode/handler.rb', line 38 def continue_element( tagname ) # TODO: Add better way to handle interrupts handler_element = @interruption_stack.pop start_element handler_element.tagname, handler_element.attributes, "" end |
#end_element(tagname, source) ⇒ Object
44 45 46 47 48 |
# File 'lib/bbcode/handler.rb', line 44 def end_element( tagname, source ) raise "Unexpected end of #{tagname.inspect}, expected #{current_handler_element.tagname.inspect}" if tagname != current_handler_element.tagname current_handler_element.end_element source @handler_element_stack.pop end |
#get_document ⇒ Object
54 55 56 |
# File 'lib/bbcode/handler.rb', line 54 def get_document @handler_element_stack.first.element end |
#get_element_handler(name) ⇒ Object
63 64 65 |
# File 'lib/bbcode/handler.rb', line 63 def get_element_handler( name ) @element_handlers[name] || ->(element){ element.is_a?(String) ? element : element.source_wraps_content } end |
#interrupt_element(tagname) ⇒ Object
32 33 34 35 36 |
# File 'lib/bbcode/handler.rb', line 32 def interrupt_element( tagname ) # TODO: Add better way to handle interrupts @interruption_stack << current_handler_element end_element tagname, "" end |
#register_element_handler(name, handler) ⇒ Object
22 23 24 |
# File 'lib/bbcode/handler.rb', line 22 def register_element_handler( name, handler ) @element_handlers[name] = handler end |
#register_element_handlers(element_handlers) ⇒ Object
16 17 18 19 20 |
# File 'lib/bbcode/handler.rb', line 16 def register_element_handlers( element_handlers ) element_handlers.each do |k, v| register_element_handler k, v end end |
#start_element(tagname, attributes, source) ⇒ Object
26 27 28 29 30 |
# File 'lib/bbcode/handler.rb', line 26 def start_element( tagname, attributes, source ) handler_element = HandlerElement.new self, tagname, attributes, source current_handler_element.childs << handler_element @handler_element_stack << handler_element end |
#text(text) ⇒ Object
50 51 52 |
# File 'lib/bbcode/handler.rb', line 50 def text( text ) current_handler_element.childs << text end |