Class: Commonmarker::Merge::Backend::Node

Inherits:
TreeHaver::Base::Node
  • Object
show all
Defined in:
lib/commonmarker/merge/backend.rb

Overview

Commonmarker node wrapper

Wraps Commonmarker::Node to provide TreeHaver::Node-compatible interface.

Instance Method Summary collapse

Instance Method Details

#childrenArray<Node>

Get child nodes

Returns:

  • (Array<Node>)

    Child nodes



171
172
173
174
175
176
177
# File 'lib/commonmarker/merge/backend.rb', line 171

def children
  return [] unless inner_node.respond_to?(:each)

  result = []
  inner_node.each { |child| result << Node.new(child, source: source, lines: lines) }
  result
end

#end_byteObject

Get end byte offset



186
187
188
189
# File 'lib/commonmarker/merge/backend.rb', line 186

def end_byte
  ep = end_point
  calculate_byte_offset(ep[:row], ep[:column])
end

#end_pointPoint

Get end point (0-based row/column)

Returns:

  • (Point)

    End position



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/commonmarker/merge/backend.rb', line 218

def end_point
  if inner_node.respond_to?(:source_position)
    begin
      pos = inner_node.source_position
      if pos && pos[:end_line]
        return Point.new(pos[:end_line] - 1, (pos[:end_column] || 1) - 1)
      end
    rescue
      nil
    end
  end

  begin
    pos = inner_node.sourcepos
    return Point.new(pos[2] - 1, pos[3] - 1) if pos
  rescue
    nil
  end

  Point.new(0, 0)
end

#fence_infoString?

Get fence info for code blocks

Returns:

  • (String, nil)


255
256
257
258
259
260
261
262
# File 'lib/commonmarker/merge/backend.rb', line 255

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

#header_levelInteger?

Get heading level (1-6)

Returns:

  • (Integer, nil)


244
245
246
247
248
249
250
251
# File 'lib/commonmarker/merge/backend.rb', line 244

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

#next_siblingNode?

Get the next sibling

Returns:



282
283
284
285
286
287
288
289
# File 'lib/commonmarker/merge/backend.rb', line 282

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

#parentNode?

Get the parent node

Returns:



304
305
306
307
308
309
310
311
# File 'lib/commonmarker/merge/backend.rb', line 304

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

#prev_siblingNode?

Get the previous sibling

Returns:



293
294
295
296
297
298
299
300
# File 'lib/commonmarker/merge/backend.rb', line 293

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

#start_byteObject

Get start byte offset



180
181
182
183
# File 'lib/commonmarker/merge/backend.rb', line 180

def start_byte
  sp = start_point
  calculate_byte_offset(sp[:row], sp[:column])
end

#start_pointPoint

Get start point (0-based row/column)

Returns:

  • (Point)

    Start position



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/commonmarker/merge/backend.rb', line 193

def start_point
  if inner_node.respond_to?(:source_position)
    begin
      pos = inner_node.source_position
      if pos && pos[:start_line]
        return Point.new(pos[:start_line] - 1, (pos[:start_column] || 1) - 1)
      end
    rescue
      nil
    end
  end

  # Fallback: check sourcepos (old API)
  begin
    pos = inner_node.sourcepos
    return Point.new(pos[0] - 1, pos[1] - 1) if pos
  rescue
    nil
  end

  Point.new(0, 0)
end

#textString

Get the text content of this node

Returns:

  • (String)

    Node text



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/commonmarker/merge/backend.rb', line 156

def text
  if inner_node.respond_to?(:string_content)
    begin
      content = inner_node.string_content.to_s
      return content unless content.empty?
    rescue TypeError
      # Container node - fall through
    end
  end
  children.map(&:text).join
end

#titleString?

Get title for links/images

Returns:

  • (String, nil)


274
275
276
277
278
# File 'lib/commonmarker/merge/backend.rb', line 274

def title
  inner_node.title
rescue
  nil
end

#typeString Also known as: kind

Get the node type as a string

Returns:

  • (String)

    Node type



146
147
148
# File 'lib/commonmarker/merge/backend.rb', line 146

def type
  inner_node.type.to_s
end

#urlString?

Get URL for links/images

Returns:

  • (String, nil)


266
267
268
269
270
# File 'lib/commonmarker/merge/backend.rb', line 266

def url
  inner_node.url
rescue
  nil
end