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.
-
#initialize_copy(other) ⇒ Object
When a TagLibrary is
dup‘d /clone’d, ensure the internal @tags Hash is independent of the source. -
#merge!(mapping) ⇒ self
Merge a Hash of class → Tag mappings on top of this library in-place.
-
#register(element_class, tag) ⇒ Object
Register a tag for an element class.
-
#unregister(element_class) ⇒ self
Remove a tag binding so the renderer falls through to
render_childrenfor that 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.
106 107 108 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 106 def self.default new.auto_register! end |
Instance Method Details
#[](element_class) ⇒ Tag?
Get tag for an element class
66 67 68 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 66 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.
94 95 96 97 98 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 94 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.
83 84 85 86 87 88 89 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 83 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.
76 77 78 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 76 def each(&block) @tags.each(&block) end |
#initialize_copy(other) ⇒ Object
When a TagLibrary is dup‘d / clone’d, ensure the internal @tags Hash is independent of the source. Without this, both copies would share the same underlying Hash and mutations to one would silently affect the other.
18 19 20 21 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 18 def initialize_copy(other) super @tags = @tags.dup end |
#merge!(mapping) ⇒ self
Merge a Hash of class → Tag mappings on top of this library in-place. A nil value unregisters the corresponding class (so the default auto-passthrough kicks in).
Named with a trailing ! because it mutates self —mirroring Ruby’s Hash#merge / Hash#merge! convention. Use dup first if you need a non-destructive merge.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 52 def merge!(mapping) mapping.each_pair do |klass, tag| if tag.nil? unregister(klass) else register(klass, tag) end end self end |
#register(element_class, tag) ⇒ Object
Register a tag for an element class
26 27 28 29 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 26 def register(element_class, tag) @tags[element_class] = tag self end |
#unregister(element_class) ⇒ self
Remove a tag binding so the renderer falls through to render_children for that element class. See Renderer#render for the auto-passthrough path.
37 38 39 40 |
# File 'lib/markbridge/renderers/discourse/tag_library.rb', line 37 def unregister(element_class) @tags.delete(element_class) self end |