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)


57
58
59
60
61
62
63
64
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 57

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



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

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

#find_parent(klass) ⇒ Object



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

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

#has_parent?(klass) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#html_mode?Boolean

Returns:

  • (Boolean)


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

def html_mode?
  @context.html_mode?
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)


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

def root?
  @context.root?
end

#with_html_mode(value) ⇒ Object



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

def with_html_mode(value)
  @context.with_html_mode(value)
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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 68

def wrap_inline(content, open_marker, close_marker = nil)
  close_marker ||= open_marker
  return content unless content.match?(/\S/)

  # 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(/\A(\s*)(.+?)(\s*)\z/m) do
    match = Regexp.last_match
    "#{match[1]}#{open_marker}#{match[2]}#{close_marker}#{match[3]}"
  end
end