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)


49
50
51
52
53
54
55
56
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 49

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

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

#count_parents(klass) ⇒ Object



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

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

#find_parent(klass) ⇒ Object



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

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

#has_parent?(klass) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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



21
22
23
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 21

def render_children(element, context: @context)
  @renderer.render_children(element, 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)


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

def root?
  @context.root?
end

#with_parent(element) ⇒ Object

Context operations



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

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 60

def wrap_inline(content, open_marker, close_marker = nil)
  close_marker ||= open_marker
  return content if content.strip.empty?

  # 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

  # Preserve leading/trailing whitespace
  content.sub(/^(\s*)(.+?)(\s*)$/m) do
    match = Regexp.last_match
    "#{match[1]}#{open_marker}#{match[2]}#{close_marker}#{match[3]}"
  end
end