Class: TreeHaver::Backends::Commonmarker::Node
- Inherits:
-
Object
- Object
- TreeHaver::Backends::Commonmarker::Node
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/tree_haver/backends/commonmarker.rb
Overview
Commonmarker node wrapper
Wraps Commonmarker::Node to provide TreeHaver::Node-compatible interface.
Instance Attribute Summary collapse
-
#inner_node ⇒ Object
readonly
Returns the value of attribute inner_node.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #child(index) ⇒ Object
- #child_count ⇒ Object
-
#children ⇒ Array<Node>
Get child nodes.
- #each(&block) ⇒ Object
- #end_byte ⇒ Object
- #end_line ⇒ Object
- #end_point ⇒ Object
-
#fence_info ⇒ String?
Get fence info for code blocks.
-
#first_child ⇒ Node?
Get the first child node.
- #has_error? ⇒ Boolean
-
#header_level ⇒ Integer?
Get heading level (1-6).
-
#initialize(node, source, lines = nil) ⇒ Node
constructor
A new instance of Node.
- #inspect ⇒ Object
- #missing? ⇒ Boolean
- #named? ⇒ Boolean (also: #structural?)
-
#next_sibling ⇒ Node?
Get the next sibling.
-
#parent ⇒ Node?
Get the parent node.
-
#prev_sibling ⇒ Node?
Get the previous sibling.
-
#source_position ⇒ Hash{Symbol => Integer}
Get position information as a hash.
-
#start_byte ⇒ Object
Position information Commonmarker 2.x provides source_position as a hash with start_line, start_column, end_line, end_column.
- #start_line ⇒ Object
- #start_point ⇒ Object
-
#text ⇒ String
Get the text content of this node.
-
#title ⇒ String?
Get title for links/images.
-
#type ⇒ String
(also: #kind)
Get the node type as a string.
-
#url ⇒ String?
Get URL for links/images.
Constructor Details
#initialize(node, source, lines = nil) ⇒ Node
Returns a new instance of Node.
209 210 211 212 213 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 209 def initialize(node, source, lines = nil) @inner_node = node @source = source @lines = lines || source.lines end |
Instance Attribute Details
#inner_node ⇒ Object (readonly)
Returns the value of attribute inner_node.
207 208 209 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 207 def inner_node @inner_node end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
207 208 209 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 207 def source @source end |
Instance Method Details
#<=>(other) ⇒ Object
393 394 395 396 397 398 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 393 def <=>(other) return unless other.respond_to?(:start_byte) cmp = start_byte <=> other.start_byte return cmp unless cmp&.zero? end_byte <=> other.end_byte end |
#child(index) ⇒ Object
266 267 268 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 266 def child(index) children[index] end |
#child_count ⇒ Object
262 263 264 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 262 def child_count children.size end |
#children ⇒ Array<Node>
Get child nodes
249 250 251 252 253 254 255 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 249 def children return [] unless @inner_node.respond_to?(:each) result = [] @inner_node.each { |child| result << Node.new(child, @source, @lines) } result end |
#each(&block) ⇒ Object
257 258 259 260 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 257 def each(&block) return to_enum(__method__) unless block children.each(&block) end |
#end_byte ⇒ Object
278 279 280 281 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 278 def end_byte ep = end_point calculate_byte_offset(ep.row, ep.column) end |
#end_line ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 340 def end_line if @inner_node.respond_to?(:source_position) pos = begin @inner_node.source_position rescue nil end return pos[:end_line] if pos && pos[:end_line] end pos = begin @inner_node.sourcepos rescue nil end pos ? pos[2] : 1 end |
#end_point ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 303 def end_point if @inner_node.respond_to?(:source_position) pos = begin @inner_node.source_position rescue nil end if pos && pos[:end_line] return Point.new(pos[:end_line] - 1, (pos[:end_column] || 1) - 1) end end pos = begin @inner_node.sourcepos rescue nil end return Point.new(0, 0) unless pos Point.new(pos[2] - 1, pos[3] - 1) end |
#fence_info ⇒ String?
Get fence info for code blocks
419 420 421 422 423 424 425 426 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 419 def fence_info return unless type == "code_block" begin @inner_node.fence_info rescue nil end end |
#first_child ⇒ Node?
Get the first child node
375 376 377 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 375 def first_child children.first end |
#has_error? ⇒ Boolean
385 386 387 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 385 def has_error? false end |
#header_level ⇒ Integer?
Get heading level (1-6)
408 409 410 411 412 413 414 415 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 408 def header_level return unless type == "heading" begin @inner_node.header_level rescue nil end end |
#inspect ⇒ Object
400 401 402 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 400 def inspect "#<TreeHaver::Backends::Commonmarker::Node type=#{type}>" end |
#missing? ⇒ Boolean
389 390 391 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 389 def missing? false end |
#named? ⇒ Boolean Also known as: structural?
379 380 381 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 379 def named? true end |
#next_sibling ⇒ Node?
Get the next sibling
446 447 448 449 450 451 452 453 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 446 def next_sibling sibling = begin @inner_node.next_sibling rescue nil end sibling ? Node.new(sibling, @source, @lines) : nil end |
#parent ⇒ Node?
Get the parent node
468 469 470 471 472 473 474 475 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 468 def parent p = begin @inner_node.parent rescue nil end p ? Node.new(p, @source, @lines) : nil end |
#prev_sibling ⇒ Node?
Get the previous sibling
457 458 459 460 461 462 463 464 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 457 def prev_sibling sibling = begin @inner_node.previous_sibling rescue nil end sibling ? Node.new(sibling, @source, @lines) : nil end |
#source_position ⇒ Hash{Symbol => Integer}
Get position information as a hash
Returns a hash with 1-based line numbers and 0-based columns. Compatible with *-merge gems’ FileAnalysisBase.
363 364 365 366 367 368 369 370 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 363 def source_position { start_line: start_line, end_line: end_line, start_column: start_point.column, end_column: end_point.column, } end |
#start_byte ⇒ Object
Position information Commonmarker 2.x provides source_position as a hash with start_line, start_column, end_line, end_column
273 274 275 276 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 273 def start_byte sp = start_point calculate_byte_offset(sp.row, sp.column) end |
#start_line ⇒ Object
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 323 def start_line if @inner_node.respond_to?(:source_position) pos = begin @inner_node.source_position rescue nil end return pos[:start_line] if pos && pos[:start_line] end pos = begin @inner_node.sourcepos rescue nil end pos ? pos[0] : 1 end |
#start_point ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 283 def start_point if @inner_node.respond_to?(:source_position) pos = begin @inner_node.source_position rescue nil end if pos && pos[:start_line] return Point.new(pos[:start_line] - 1, (pos[:start_column] || 1) - 1) end end pos = begin @inner_node.sourcepos rescue nil end return Point.new(0, 0) unless pos Point.new(pos[0] - 1, pos[1] - 1) end |
#text ⇒ String
Get the text content of this node
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 229 def text # Commonmarker nodes have string_content for text nodes # Container nodes don't have string_content and will raise TypeError if @inner_node.respond_to?(:string_content) begin content = @inner_node.string_content.to_s # If string_content is non-empty, use it (leaf node) return content unless content.empty? rescue TypeError # Container node - fall through to concatenate children end end # For container nodes, concatenate children's text children.map(&:text).join end |
#title ⇒ String?
Get title for links/images
438 439 440 441 442 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 438 def title @inner_node.title rescue nil end |
#type ⇒ String Also known as: kind
Get the node type as a string
Commonmarker uses symbols like :document, :heading, :paragraph, etc.
220 221 222 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 220 def type @inner_node.type.to_s end |
#url ⇒ String?
Get URL for links/images
430 431 432 433 434 |
# File 'lib/tree_haver/backends/commonmarker.rb', line 430 def url @inner_node.url rescue nil end |