Class: Markdown::Merge::GapLineNode
- Inherits:
-
Ast::Merge::AstNode
- Object
- Ast::Merge::AstNode
- Markdown::Merge::GapLineNode
- Defined in:
- lib/markdown/merge/gap_line_node.rb
Overview
Represents a “gap” line that exists between parsed Markdown nodes.
Markdown parsers like Markly consume certain content during parsing (like link reference definitions) and don’t preserve blank lines between nodes in the AST. This class represents lines that fall into “gaps” between parsed nodes, allowing them to be preserved during merge operations.
Gap lines include:
-
Blank lines between sections
-
Link reference definitions (handled specially by LinkDefinitionNode)
-
Any other content consumed by the parser
Instance Attribute Summary collapse
-
#content ⇒ String
readonly
The line content (may be empty for blank lines).
-
#line_number ⇒ Integer
readonly
1-based line number.
-
#preceding_node ⇒ Object?
This is set after integration to avoid circular dependencies during creation.
Instance Method Summary collapse
-
#blank? ⇒ Boolean
Check if this is a blank line.
-
#children ⇒ Array
TreeHaver::Node protocol: children (none).
-
#initialize(content, line_number:) ⇒ GapLineNode
constructor
Initialize a new GapLineNode.
-
#inspect ⇒ Object
For debugging.
-
#signature ⇒ Array
Generate a signature for matching gap lines.
-
#source_position ⇒ Hash
TreeHaver::Node protocol: source_position.
-
#text ⇒ String
TreeHaver::Node protocol: text.
-
#to_commonmark ⇒ String
Convert to commonmark format.
-
#type ⇒ Symbol
(also: #merge_type)
TreeHaver::Node protocol: type.
Constructor Details
#initialize(content, line_number:) ⇒ GapLineNode
Initialize a new GapLineNode
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/markdown/merge/gap_line_node.rb', line 37 def initialize(content, line_number:) @content = content.chomp @line_number = line_number @preceding_node = nil # Set later during integration location = Ast::Merge::AstNode::Location.new( start_line: line_number, end_line: line_number, start_column: 0, end_column: @content.length, ) super(slice: @content, location: location) end |
Instance Attribute Details
#content ⇒ String (readonly)
Returns The line content (may be empty for blank lines).
24 25 26 |
# File 'lib/markdown/merge/gap_line_node.rb', line 24 def content @content end |
#line_number ⇒ Integer (readonly)
Returns 1-based line number.
27 28 29 |
# File 'lib/markdown/merge/gap_line_node.rb', line 27 def line_number @line_number end |
#preceding_node ⇒ Object?
This is set after integration to avoid circular dependencies during creation
31 32 33 |
# File 'lib/markdown/merge/gap_line_node.rb', line 31 def preceding_node @preceding_node end |
Instance Method Details
#blank? ⇒ Boolean
Check if this is a blank line
120 121 122 |
# File 'lib/markdown/merge/gap_line_node.rb', line 120 def blank? @content.strip.empty? end |
#children ⇒ Array
TreeHaver::Node protocol: children (none)
108 109 110 |
# File 'lib/markdown/merge/gap_line_node.rb', line 108 def children [] end |
#inspect ⇒ Object
For debugging
131 132 133 |
# File 'lib/markdown/merge/gap_line_node.rb', line 131 def inspect "#<#{self.class.name} line=#{@line_number} content=#{@content.inspect}>" end |
#signature ⇒ Array
Generate a signature for matching gap lines. Gap lines are matched by their position relative to the preceding structural node. This allows blank lines after a heading in template to match blank lines after the same heading in destination, even if they’re on different absolute line numbers.
For gap lines at the start of the document (no preceding node), we use line number. For gap lines after a structural node, we use offset from that node’s end line.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/markdown/merge/gap_line_node.rb', line 71 def signature if @preceding_node&.respond_to?(:source_position) pos = @preceding_node.source_position preceding_end_line = pos[:end_line] if pos if preceding_end_line # Offset from preceding node's end (e.g., heading ends on line 1, gap is line 2, offset = 1) offset = @line_number - preceding_end_line # Use the preceding node's type as context (simpler than full signature) # This works because gap lines after headings match gap lines after headings, etc. preceding_type = @preceding_node.respond_to?(:type) ? @preceding_node.type : :unknown [:gap_line_after, preceding_type, offset, @content] else # Fallback if we can't get position [:gap_line, @line_number, @content] end else # No preceding node - use absolute line number (for gaps at document start) [:gap_line, @line_number, @content] end end |
#source_position ⇒ Hash
TreeHaver::Node protocol: source_position
97 98 99 100 101 102 103 104 |
# File 'lib/markdown/merge/gap_line_node.rb', line 97 def source_position { start_line: @line_number, end_line: @line_number, start_column: 0, end_column: @content.length, } end |
#text ⇒ String
TreeHaver::Node protocol: text
114 115 116 |
# File 'lib/markdown/merge/gap_line_node.rb', line 114 def text @content end |
#to_commonmark ⇒ String
Convert to commonmark format
126 127 128 |
# File 'lib/markdown/merge/gap_line_node.rb', line 126 def to_commonmark "#{@content}\n" end |
#type ⇒ Symbol Also known as: merge_type
TreeHaver::Node protocol: type
54 55 56 |
# File 'lib/markdown/merge/gap_line_node.rb', line 54 def type :gap_line end |