Class: TreeHaver::Backends::Commonmarker::Node

Inherits:
Object
  • Object
show all
Includes:
Comparable
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

Instance Method Summary collapse

Constructor Details

#initialize(node, source, lines = nil) ⇒ Node

Returns a new instance of Node.



184
185
186
187
188
# File 'lib/tree_haver/backends/commonmarker.rb', line 184

def initialize(node, source, lines = nil)
  @inner_node = node
  @source = source
  @lines = lines || source.lines
end

Instance Attribute Details

#inner_nodeObject (readonly)

Returns the value of attribute inner_node.



182
183
184
# File 'lib/tree_haver/backends/commonmarker.rb', line 182

def inner_node
  @inner_node
end

#sourceObject (readonly)

Returns the value of attribute source.



182
183
184
# File 'lib/tree_haver/backends/commonmarker.rb', line 182

def source
  @source
end

Instance Method Details

#<=>(other) ⇒ Object



367
368
369
370
371
372
# File 'lib/tree_haver/backends/commonmarker.rb', line 367

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



240
241
242
# File 'lib/tree_haver/backends/commonmarker.rb', line 240

def child(index)
  children[index]
end

#child_countObject



236
237
238
# File 'lib/tree_haver/backends/commonmarker.rb', line 236

def child_count
  children.size
end

#childrenArray<Node>

Get child nodes

Returns:

  • (Array<Node>)

    Child nodes



223
224
225
226
227
228
229
# File 'lib/tree_haver/backends/commonmarker.rb', line 223

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



231
232
233
234
# File 'lib/tree_haver/backends/commonmarker.rb', line 231

def each(&block)
  return to_enum(__method__) unless block
  children.each(&block)
end

#end_byteObject



252
253
254
255
# File 'lib/tree_haver/backends/commonmarker.rb', line 252

def end_byte
  ep = end_point
  calculate_byte_offset(ep.row, ep.column)
end

#end_lineObject



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/tree_haver/backends/commonmarker.rb', line 314

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_pointObject



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/tree_haver/backends/commonmarker.rb', line 277

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_infoString?

Get fence info for code blocks

Returns:

  • (String, nil)


393
394
395
396
397
398
399
400
# File 'lib/tree_haver/backends/commonmarker.rb', line 393

def fence_info
  return unless type == "code_block"
  begin
    @inner_node.fence_info
  rescue
    nil
  end
end

#first_childNode?

Get the first child node

Returns:

  • (Node, nil)

    First child or nil



349
350
351
# File 'lib/tree_haver/backends/commonmarker.rb', line 349

def first_child
  children.first
end

#has_error?Boolean

Returns:

  • (Boolean)


359
360
361
# File 'lib/tree_haver/backends/commonmarker.rb', line 359

def has_error?
  false
end

#header_levelInteger?

Get heading level (1-6)

Returns:

  • (Integer, nil)


382
383
384
385
386
387
388
389
# File 'lib/tree_haver/backends/commonmarker.rb', line 382

def header_level
  return unless type == "heading"
  begin
    @inner_node.header_level
  rescue
    nil
  end
end

#inspectObject



374
375
376
# File 'lib/tree_haver/backends/commonmarker.rb', line 374

def inspect
  "#<TreeHaver::Backends::Commonmarker::Node type=#{type}>"
end

#missing?Boolean

Returns:

  • (Boolean)


363
364
365
# File 'lib/tree_haver/backends/commonmarker.rb', line 363

def missing?
  false
end

#named?Boolean Also known as: structural?

Returns:

  • (Boolean)


353
354
355
# File 'lib/tree_haver/backends/commonmarker.rb', line 353

def named?
  true
end

#next_siblingNode?

Get the next sibling

Returns:



420
421
422
423
424
425
426
427
# File 'lib/tree_haver/backends/commonmarker.rb', line 420

def next_sibling
  sibling = begin
    @inner_node.next_sibling
  rescue
    nil
  end
  sibling ? Node.new(sibling, @source, @lines) : nil
end

#parentNode?

Get the parent node

Returns:



442
443
444
445
446
447
448
449
# File 'lib/tree_haver/backends/commonmarker.rb', line 442

def parent
  p = begin
    @inner_node.parent
  rescue
    nil
  end
  p ? Node.new(p, @source, @lines) : nil
end

#previous_siblingNode?

Get the previous sibling

Returns:



431
432
433
434
435
436
437
438
# File 'lib/tree_haver/backends/commonmarker.rb', line 431

def previous_sibling
  sibling = begin
    @inner_node.previous_sibling
  rescue
    nil
  end
  sibling ? Node.new(sibling, @source, @lines) : nil
end

#source_positionHash{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.

Returns:

  • (Hash{Symbol => Integer})

    Position hash



337
338
339
340
341
342
343
344
# File 'lib/tree_haver/backends/commonmarker.rb', line 337

def source_position
  {
    start_line: start_line,
    end_line: end_line,
    start_column: start_point.column,
    end_column: end_point.column,
  }
end

#start_byteObject

Position information Commonmarker 2.x provides source_position as a hash with start_line, start_column, end_line, end_column



247
248
249
250
# File 'lib/tree_haver/backends/commonmarker.rb', line 247

def start_byte
  sp = start_point
  calculate_byte_offset(sp.row, sp.column)
end

#start_lineObject



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/tree_haver/backends/commonmarker.rb', line 297

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_pointObject



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/tree_haver/backends/commonmarker.rb', line 257

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

#textString

Get the text content of this node

Returns:

  • (String)

    Node text



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/tree_haver/backends/commonmarker.rb', line 204

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
      @inner_node.string_content.to_s
    rescue TypeError
      # Container node - concatenate children's text
      children.map(&:text).join
    end
  else
    # For container nodes, concatenate children's text
    children.map(&:text).join
  end
end

#titleString?

Get title for links/images

Returns:

  • (String, nil)


412
413
414
415
416
# File 'lib/tree_haver/backends/commonmarker.rb', line 412

def title
  @inner_node.title
rescue
  nil
end

#typeString Also known as: kind

Get the node type as a string

Commonmarker uses symbols like :document, :heading, :paragraph, etc.

Returns:

  • (String)

    Node type



195
196
197
# File 'lib/tree_haver/backends/commonmarker.rb', line 195

def type
  @inner_node.type.to_s
end

#urlString?

Get URL for links/images

Returns:

  • (String, nil)


404
405
406
407
408
# File 'lib/tree_haver/backends/commonmarker.rb', line 404

def url
  @inner_node.url
rescue
  nil
end