Class: Markbridge::Renderers::Discourse::TagLibrary
- Inherits:
-
Object
- Object
- Markbridge::Renderers::Discourse::TagLibrary
- Includes:
- Enumerable
- Defined in:
- lib/markbridge/renderers/discourse/tag_library.rb
Overview
Library of rendering tags for different element types
Class Method Summary collapse
-
.default ⇒ TagLibrary
Create the default tag library for Discourse Markdown.
Instance Method Summary collapse
-
#[](element_class) ⇒ Tag?
Get tag for an element class.
-
#ast_class_for(tag_constant) ⇒ Class?
Look up the AST element class matching a
XxxTagconstant via the XxxTag → AST::Xxx naming convention. -
#auto_register! ⇒ self
Auto-register all tags using naming convention Convention: BoldTag handles AST::Bold, ItalicTag handles AST::Italic, etc.
-
#each {|element_class, tag| ... } ⇒ Enumerator
Iterate over registered (element_class, tag) pairs.
-
#initialize ⇒ TagLibrary
constructor
A new instance of TagLibrary.
-
#register(element_class, tag) ⇒ Object
Register a tag for an element class.
Constructor Details
#initialize ⇒ TagLibrary
Returns a new instance of TagLibrary.
10 11 12 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 10 def initialize @tags = {} end |
Class Method Details
.default ⇒ TagLibrary
Create the default tag library for Discourse Markdown.
Each call returns a fresh instance — mutations made to one will not be visible to another. If you want a process-wide singleton, use Markbridge.default_tag_library instead, which memoizes.
66 67 68 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 66 def self.default new.auto_register! end |
Instance Method Details
#[](element_class) ⇒ Tag?
Get tag for an element class
25 26 27 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 25 def [](element_class) @tags[element_class] end |
#ast_class_for(tag_constant) ⇒ Class?
Look up the AST element class matching a XxxTag constant via the XxxTag → AST::Xxx naming convention.
53 54 55 56 57 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 53 def ast_class_for(tag_constant) AST.const_get(tag_constant.to_s.sub(/Tag\z/, "")) rescue NameError nil end |
#auto_register! ⇒ self
Auto-register all tags using naming convention Convention: BoldTag handles AST::Bold, ItalicTag handles AST::Italic, etc.
42 43 44 45 46 47 48 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 42 def auto_register! Tags.constants.each do |tag_constant| element_class = ast_class_for(tag_constant) register(element_class, Tags.const_get(tag_constant).new) if element_class end self end |
#each {|element_class, tag| ... } ⇒ Enumerator
Iterate over registered (element_class, tag) pairs. Useful for debugging custom libraries — e.g. confirming an override has stuck. Iteration order matches registration order.
35 36 37 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 35 def each(&block) @tags.each(&block) end |
#register(element_class, tag) ⇒ Object
Register a tag for an element class
17 18 19 20 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 17 def register(element_class, tag) @tags[element_class] = tag self end |