Class: Markly::Merge::Backend::Node
- Inherits:
-
TreeHaver::Base::Node
- Object
- TreeHaver::Base::Node
- Markly::Merge::Backend::Node
- Defined in:
- lib/markly/merge/backend.rb
Overview
Markly node wrapper
Wraps Markly::Node to provide TreeHaver::Node-compatible interface.
Constant Summary collapse
- TYPE_MAP =
Type normalization map (Markly → canonical)
{ header: 'heading', hrule: 'thematic_break', html: 'html_block' }.freeze
- DEFAULT_SOURCE_POSITION =
Default source position for nodes that don't have position info
{ start_line: 1, start_column: 1, end_line: 1, end_column: 1 }.freeze
Instance Method Summary collapse
-
#children ⇒ Array<Node>
Get child nodes (Markly uses first_child/next pattern).
- #end_byte ⇒ Object
- #end_point ⇒ Object
-
#inner_source_position ⇒ Hash{Symbol => Integer}
private
Get source position from the inner Markly node.
-
#raw_type ⇒ String
Get the raw (non-normalized) type.
-
#start_byte ⇒ Object
Position information.
- #start_point ⇒ Object
-
#text ⇒ String
Get the text content of this node.
-
#to_commonmark ⇒ Object
Convert node to CommonMark/Markdown/HTML/plaintext.
- #to_html ⇒ Object
- #to_markdown ⇒ Object
- #to_plaintext ⇒ Object
-
#type ⇒ String
Get the node type as a string (normalized).
Instance Method Details
#children ⇒ Array<Node>
Get child nodes (Markly uses first_child/next pattern)
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/markly/merge/backend.rb', line 227 def children result = [] child = begin inner_node.first_child rescue StandardError nil end while child result << Node.new(child, source: source, lines: lines) child = begin child.next rescue StandardError nil end end result end |
#end_byte ⇒ Object
252 253 254 255 |
# File 'lib/markly/merge/backend.rb', line 252 def end_byte pos = inner_source_position calculate_byte_offset(pos[:end_line] - 1, pos[:end_column] - 1) end |
#end_point ⇒ Object
262 263 264 265 |
# File 'lib/markly/merge/backend.rb', line 262 def end_point pos = inner_source_position { row: pos[:end_line] - 1, column: pos[:end_column] - 1 } end |
#inner_source_position ⇒ Hash{Symbol => Integer}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get source position from the inner Markly node
182 183 184 185 186 187 188 |
# File 'lib/markly/merge/backend.rb', line 182 def inner_source_position @inner_source_position ||= if inner_node.respond_to?(:source_position) inner_node.source_position || DEFAULT_SOURCE_POSITION else DEFAULT_SOURCE_POSITION end end |
#raw_type ⇒ String
Get the raw (non-normalized) type
200 201 202 |
# File 'lib/markly/merge/backend.rb', line 200 def raw_type inner_node.type.to_s end |
#start_byte ⇒ Object
Position information
247 248 249 250 |
# File 'lib/markly/merge/backend.rb', line 247 def start_byte pos = inner_source_position calculate_byte_offset(pos[:start_line] - 1, pos[:start_column] - 1) end |
#start_point ⇒ Object
257 258 259 260 |
# File 'lib/markly/merge/backend.rb', line 257 def start_point pos = inner_source_position { row: pos[:start_line] - 1, column: pos[:start_column] - 1 } end |
#text ⇒ String
Get the text content of this node
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/markly/merge/backend.rb', line 207 def text if inner_node.respond_to?(:string_content) content = inner_node.string_content.to_s return content unless content.empty? end if inner_node.respond_to?(:to_plaintext) begin inner_node.to_plaintext rescue StandardError children.map(&:text).join end else children.map(&:text).join end end |
#to_commonmark ⇒ Object
Convert node to CommonMark/Markdown/HTML/plaintext
268 269 270 |
# File 'lib/markly/merge/backend.rb', line 268 def to_commonmark inner_node.to_commonmark end |
#to_html ⇒ Object
280 281 282 |
# File 'lib/markly/merge/backend.rb', line 280 def to_html inner_node.to_html end |
#to_markdown ⇒ Object
272 273 274 |
# File 'lib/markly/merge/backend.rb', line 272 def to_markdown inner_node.to_markdown end |
#to_plaintext ⇒ Object
276 277 278 |
# File 'lib/markly/merge/backend.rb', line 276 def to_plaintext inner_node.to_plaintext end |
#type ⇒ String
Get the node type as a string (normalized)
193 194 195 196 |
# File 'lib/markly/merge/backend.rb', line 193 def type raw = inner_node.type.to_s TYPE_MAP[raw.to_sym]&.to_s || raw end |