Class: Markbridge::Parsers::TextFormatter::Handlers::AttributeHandler

Inherits:
BaseHandler
  • Object
show all
Defined in:
lib/markbridge/parsers/text_formatter/handlers/attribute_handler.rb

Overview

Generic handler for elements that take a single attribute

This handler extracts a specified attribute and passes it to the AST node constructor. Use this for elements like COLOR, SIZE, ALIGN, SPOILER.

Examples:

# For <COLOR color="red">text</COLOR>
handler = AttributeHandler.new(AST::Color, attribute: :color, param: :color)
registry.register("COLOR", handler)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element_class, attribute:, param: nil) ⇒ AttributeHandler

Returns a new instance of AttributeHandler.

Parameters:

  • element_class (Class)

    the AST node class to instantiate

  • attribute (Symbol)

    the XML attribute name to extract

  • param (Symbol) (defaults to: nil)

    the parameter name to pass to the AST node constructor



20
21
22
23
24
# File 'lib/markbridge/parsers/text_formatter/handlers/attribute_handler.rb', line 20

def initialize(element_class, attribute:, param: nil)
  @element_class = element_class
  @attribute = attribute
  @param = param || attribute
end

Instance Attribute Details

#element_classObject (readonly)

Returns the value of attribute element_class.



35
36
37
# File 'lib/markbridge/parsers/text_formatter/handlers/attribute_handler.rb', line 35

def element_class
  @element_class
end

Instance Method Details

#process(element:, parent:) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/markbridge/parsers/text_formatter/handlers/attribute_handler.rb', line 26

def process(element:, parent:)
  attrs = extract_attributes(element)
  node = @element_class.new(@param => attrs[@attribute])
  parent << node

  # Return node to signal: process children into this node
  node
end