Class: Markbridge::Renderers::Discourse::RenderingInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/markbridge/renderers/discourse/rendering_interface.rb

Overview

Interface that tags use for rendering operations Decouples tags from renderer implementation details

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(renderer, context) ⇒ RenderingInterface

Returns a new instance of RenderingInterface.



11
12
13
14
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 11

def initialize(renderer, context)
  @renderer = renderer
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



9
10
11
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 9

def context
  @context
end

Instance Method Details

#block_context?(node) ⇒ Boolean

Check if element should be rendered in block context

Parameters:

  • node (AST::Node)

    container node or leaf like HorizontalRule

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 65

def block_context?(node)
  # Check if it's a block-level element type (but not code, which can be inline)
  return true if node.instance_of?(AST::List) || node.instance_of?(AST::HorizontalRule)
  return false unless node.is_a?(AST::Element)

  # Check if content has newlines
  node.children.any? { |c| c.instance_of?(AST::Text) && c.text.include?("\n") }
end

#count_parents(klass) ⇒ Object



50
51
52
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 50

def count_parents(klass)
  @context.count_parents(klass)
end

#find_parent(klass) ⇒ Object



46
47
48
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 46

def find_parent(klass)
  @context.find_parent(klass)
end

#has_parent?(klass) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 54

def has_parent?(klass)
  @context.has_parent?(klass)
end

#html_mode?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 42

def html_mode?
  @context.html_mode?
end

#render_children(element, context: @context) ⇒ Object



29
30
31
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 29

def render_children(element, context: @context)
  @renderer.render_children(element, context:)
end

#render_default(node, context: @context) ⇒ Object

Render a node with the stock tag for its class, bypassing any override in the renderer's tag library. Lets a custom tag handle only the nodes it cares about and delegate the rest — see Renderer#render_default.



25
26
27
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 25

def render_default(node, context: @context)
  @renderer.render_default(node, context:)
end

#render_node(node, context: @context) ⇒ Object

Core rendering operations



17
18
19
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 17

def render_node(node, context: @context)
  @renderer.render(node, context:)
end

#root?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 58

def root?
  @context.root?
end

#with_html_mode(value) ⇒ Object



38
39
40
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 38

def with_html_mode(value)
  @context.with_html_mode(value)
end

#with_parent(element) ⇒ Object

Context operations



34
35
36
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 34

def with_parent(element)
  @context.with_parent(element)
end

#wrap_inline(content, open_marker, close_marker = nil) ⇒ Object

Helper: wrap inline content with markers Handles edge cases like existing markers and whitespace



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 81

def wrap_inline(content, open_marker, close_marker = nil)
  close_marker ||= open_marker
  return content unless content.match?(/[^[:space:]]/)

  # Handle conflicts with existing markers
  if content.include?(open_marker) || content.include?(close_marker)
    # Use HTML fallback for common cases
    case open_marker
    when "**"
      return "<strong>#{content}</strong>"
    when "*"
      return "<em>#{content}</em>"
    when "~~"
      return "<s>#{content}</s>"
    end
  end

  apply_markers(content, open_marker, close_marker)
end