Class: Markbridge::Parsers::HTML::HandlerRegistry
- Inherits:
-
Object
- Object
- Markbridge::Parsers::HTML::HandlerRegistry
- Defined in:
- lib/markbridge/parsers/html/handler_registry.rb
Overview
Registry of HTML tag handlers
Class Method Summary collapse
-
.build_from_default {|HandlerRegistry| ... } ⇒ HandlerRegistry
Build a registry from the default configuration with optional customization.
-
.default ⇒ HandlerRegistry
Create the default handler registry with common HTML tags.
Instance Method Summary collapse
-
#[](tag_name) ⇒ BaseHandler, ...
Get handler for a tag name.
-
#initialize ⇒ HandlerRegistry
constructor
A new instance of HandlerRegistry.
-
#register(tag_names, handler) ⇒ Object
Register a handler for one or more tag names.
Constructor Details
#initialize ⇒ HandlerRegistry
Returns a new instance of HandlerRegistry.
8 9 10 |
# File 'lib/markbridge/parsers/html/handler_registry.rb', line 8 def initialize @handlers = {} end |
Class Method Details
.build_from_default {|HandlerRegistry| ... } ⇒ HandlerRegistry
Build a registry from the default configuration with optional customization
84 85 86 87 88 |
# File 'lib/markbridge/parsers/html/handler_registry.rb', line 84 def self.build_from_default registry = default yield(registry) if block_given? registry end |
.default ⇒ HandlerRegistry
Create the default handler registry with common HTML tags
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/markbridge/parsers/html/handler_registry.rb', line 29 def self.default registry = new # Simple formatting handlers registry.register(%w[b strong], Handlers::SimpleHandler.new(AST::Bold)) registry.register(%w[i em], Handlers::SimpleHandler.new(AST::Italic)) registry.register(%w[s strike del], Handlers::SimpleHandler.new(AST::Strikethrough)) registry.register("u", Handlers::SimpleHandler.new(AST::Underline)) registry.register("sup", Handlers::SimpleHandler.new(AST::Superscript)) registry.register("sub", Handlers::SimpleHandler.new(AST::Subscript)) # Code handlers (raw content) registry.register(%w[code pre tt], Handlers::RawHandler.new(AST::Code)) # Link and image handlers registry.register("a", Handlers::UrlHandler.new) registry.register("img", Handlers::ImageHandler.new) # Blockquote handler registry.register("blockquote", Handlers::QuoteHandler.new) # Void elements - use simple inline handlers registry.register( "br", lambda do |element:, parent:| parent << AST::LineBreak.new nil # Return nil - void element, no children end, ) registry.register( "hr", lambda do |element:, parent:| parent << AST::HorizontalRule.new nil # Return nil - void element, no children end, ) # List handlers registry.register(%w[ul ol], Handlers::ListHandler.new) registry.register("li", Handlers::ListItemHandler.new) # Table handlers (thead/tbody/tfoot are transparent - unregistered tags pass through) registry.register("table", Handlers::TableHandler.new) registry.register("tr", Handlers::TableRowHandler.new) registry.register(%w[td th], Handlers::TableCellHandler.new) # Paragraph handler (transparent - doesn't create AST node) registry.register("p", Handlers::ParagraphHandler.new) registry end |
Instance Method Details
#[](tag_name) ⇒ BaseHandler, ...
Get handler for a tag name
23 24 25 |
# File 'lib/markbridge/parsers/html/handler_registry.rb', line 23 def [](tag_name) @handlers[tag_name.to_s.downcase] end |
#register(tag_names, handler) ⇒ Object
Register a handler for one or more tag names
15 16 17 18 |
# File 'lib/markbridge/parsers/html/handler_registry.rb', line 15 def register(tag_names, handler) Array(tag_names).each { |tag_name| @handlers[tag_name.to_s.downcase] = handler } self end |