Class: Kotoshu::Documents::TextNode

Inherits:
Struct
  • Object
show all
Defined in:
lib/kotoshu/documents/text_node.rb

Overview

A contiguous run of flattened text plus its source position.

The checker scans the concatenation of every text node's text (the document's flattened form). Each node carries the SourceRange it occupies in the original markup-bearing source, plus its flattened_offset (where the node starts in the concatenated flattened text) so the document can map errors back to source.

format and metadata are escape hatches for plugins that want to attach structural context (e.g. format: :heading, metadata: { level: 2 } for an h2). Kotoshu's core checker ignores them; consumers (editors, reporters) can use them to surface context-aware UI.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text:, source_range:, flattened_offset:, format: :plain, metadata: {}) ⇒ TextNode

Returns a new instance of TextNode.

Raises:

  • (TypeError)


20
21
22
23
24
25
26
# File 'lib/kotoshu/documents/text_node.rb', line 20

def initialize(text:, source_range:, flattened_offset:, format: :plain, metadata: {})
  raise TypeError, "source_range must be a SourceRange" unless source_range.is_a?(SourceRange)
  raise ArgumentError, "flattened_offset must be >= 0" if flattened_offset.negative?

  super
  freeze
end

Instance Attribute Details

#flattened_offsetObject

Returns the value of attribute flattened_offset

Returns:

  • (Object)

    the current value of flattened_offset



18
19
20
# File 'lib/kotoshu/documents/text_node.rb', line 18

def flattened_offset
  @flattened_offset
end

#formatObject

Returns the value of attribute format

Returns:

  • (Object)

    the current value of format



18
19
20
# File 'lib/kotoshu/documents/text_node.rb', line 18

def format
  @format
end

#metadataObject

Returns the value of attribute metadata

Returns:

  • (Object)

    the current value of metadata



18
19
20
# File 'lib/kotoshu/documents/text_node.rb', line 18

def 
  @metadata
end

#source_rangeObject

Returns the value of attribute source_range

Returns:

  • (Object)

    the current value of source_range



18
19
20
# File 'lib/kotoshu/documents/text_node.rb', line 18

def source_range
  @source_range
end

#textObject

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



18
19
20
# File 'lib/kotoshu/documents/text_node.rb', line 18

def text
  @text
end

Instance Method Details

#contains_flattened?(offset) ⇒ Boolean

True when offset falls within this node's flattened range.

Returns:

  • (Boolean)


40
41
42
# File 'lib/kotoshu/documents/text_node.rb', line 40

def contains_flattened?(offset)
  flattened_range.include?(offset)
end

#flattened_rangeObject

Flattened-text range covered by this node: [flattened_offset, flattened_offset + text.length).



35
36
37
# File 'lib/kotoshu/documents/text_node.rb', line 35

def flattened_range
  flattened_offset...(flattened_offset + text.length)
end

#lengthObject

Length of the flattened text in this node.



29
30
31
# File 'lib/kotoshu/documents/text_node.rb', line 29

def length
  text.length
end

#to_sObject



44
45
46
# File 'lib/kotoshu/documents/text_node.rb', line 44

def to_s
  "#{format}(#{text.inspect} @ #{source_range})"
end