Class: Markbridge::Renderers::Discourse::TagLibrary

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/markbridge/renderers/discourse/tag_library.rb

Overview

Library of rendering tags for different element types

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTagLibrary

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

.defaultTagLibrary

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.

Returns:



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

Parameters:

  • element_class (Class)

Returns:



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.

Returns:

  • (Class, nil)


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.

Returns:

  • (self)


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.

Yield Parameters:

  • element_class (Class)
  • tag (Tag)

Returns:

  • (Enumerator)

    when no block is given



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

Parameters:

  • element_class (Class)

    the element class

  • tag (Tag)

    the tag instance



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