Class: Kotoshu::Documents::TextNode
- Inherits:
-
Struct
- Object
- Struct
- Kotoshu::Documents::TextNode
- 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
-
#flattened_offset ⇒ Object
Returns the value of attribute flattened_offset.
-
#format ⇒ Object
Returns the value of attribute format.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#source_range ⇒ Object
Returns the value of attribute source_range.
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
-
#contains_flattened?(offset) ⇒ Boolean
True when
offsetfalls within this node's flattened range. -
#flattened_range ⇒ Object
Flattened-text range covered by this node: [flattened_offset, flattened_offset + text.length).
-
#initialize(text:, source_range:, flattened_offset:, format: :plain, metadata: {}) ⇒ TextNode
constructor
A new instance of TextNode.
-
#length ⇒ Object
Length of the flattened text in this node.
- #to_s ⇒ Object
Constructor Details
#initialize(text:, source_range:, flattened_offset:, format: :plain, metadata: {}) ⇒ TextNode
Returns a new instance of TextNode.
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_offset ⇒ Object
Returns the value of attribute flattened_offset
18 19 20 |
# File 'lib/kotoshu/documents/text_node.rb', line 18 def flattened_offset @flattened_offset end |
#format ⇒ Object
Returns the value of attribute format
18 19 20 |
# File 'lib/kotoshu/documents/text_node.rb', line 18 def format @format end |
#metadata ⇒ Object
Returns the value of attribute metadata
18 19 20 |
# File 'lib/kotoshu/documents/text_node.rb', line 18 def @metadata end |
#source_range ⇒ Object
Returns the value of attribute source_range
18 19 20 |
# File 'lib/kotoshu/documents/text_node.rb', line 18 def source_range @source_range end |
#text ⇒ Object
Returns the value of attribute 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.
40 41 42 |
# File 'lib/kotoshu/documents/text_node.rb', line 40 def contains_flattened?(offset) flattened_range.include?(offset) end |
#flattened_range ⇒ Object
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 |
#length ⇒ Object
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_s ⇒ Object
44 45 46 |
# File 'lib/kotoshu/documents/text_node.rb', line 44 def to_s "#{format}(#{text.inspect} @ #{source_range})" end |