Class: Markbridge::Renderers::Discourse::RenderingInterface
- Inherits:
-
Object
- Object
- Markbridge::Renderers::Discourse::RenderingInterface
- 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
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
-
#block_context?(node) ⇒ Boolean
Check if element should be rendered in block context.
- #count_parents(klass) ⇒ Object
- #find_parent(klass) ⇒ Object
- #has_parent?(klass) ⇒ Boolean
- #html_mode? ⇒ Boolean
-
#initialize(renderer, context) ⇒ RenderingInterface
constructor
A new instance of RenderingInterface.
-
#render_children(element, context: @context, &block) ⇒ Object
An optional block runs at every join point, before a non-empty child output is appended — see Renderer#render_children.
-
#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.
-
#render_node(node, context: @context) ⇒ Object
Core rendering operations.
- #root? ⇒ Boolean
- #with_html_mode(value) ⇒ Object
-
#with_parent(element) ⇒ Object
Context operations.
-
#wrap_inline(content, open_marker, close_marker = nil) ⇒ Object
Helper: wrap inline content with markers Handles edge cases like existing markers and whitespace.
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
#context ⇒ Object (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
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 67 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) # A Code node whose source construct is a block by definition keeps # its block form even with single-line content (is_a?, so Code # subclasses inherit the behavior). return true if node.is_a?(AST::Code) && node.block 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
52 53 54 |
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 52 def count_parents(klass) @context.count_parents(klass) end |
#find_parent(klass) ⇒ Object
48 49 50 |
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 48 def find_parent(klass) @context.find_parent(klass) end |
#has_parent?(klass) ⇒ Boolean
56 57 58 |
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 56 def has_parent?(klass) @context.has_parent?(klass) end |
#html_mode? ⇒ Boolean
44 45 46 |
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 44 def html_mode? @context.html_mode? end |
#render_children(element, context: @context, &block) ⇒ Object
An optional block runs at every join point, before a non-empty child output is appended — see Renderer#render_children.
31 32 33 |
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 31 def render_children(element, context: @context, &block) @renderer.render_children(element, context:, &block) 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
60 61 62 |
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 60 def root? @context.root? end |
#with_html_mode(value) ⇒ Object
40 41 42 |
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 40 def with_html_mode(value) @context.with_html_mode(value) end |
#with_parent(element) ⇒ Object
Context operations
36 37 38 |
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 36 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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/markbridge/renderers/discourse/rendering_interface.rb', line 87 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 |