Class: RedQuilt::NodeRef

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Enumerable[NodeRef]
Defined in:
lib/red_quilt/node_ref.rb,
sig/red_quilt.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, node_id) ⇒ NodeRef

Returns a new instance of NodeRef.

Parameters:



9
10
11
12
13
# File 'lib/red_quilt/node_ref.rb', line 9

def initialize(document, node_id)
  @document = document
  @arena = document.arena
  @node_id = node_id
end

Instance Attribute Details

#documentDocument (readonly)

Returns the value of attribute document.

Returns:



7
8
9
# File 'lib/red_quilt/node_ref.rb', line 7

def document
  @document
end

#node_idInteger (readonly)

Returns the value of attribute node_id.

Returns:

  • (Integer)


7
8
9
# File 'lib/red_quilt/node_ref.rb', line 7

def node_id
  @node_id
end

Instance Method Details

#childrenArray[NodeRef]

Returns:



23
24
25
# File 'lib/red_quilt/node_ref.rb', line 23

def children
  @arena.child_ids(@node_id).map { |child_id| NodeRef.new(@document, child_id) }
end

#eachvoid #eachEnumerator[NodeRef, void]

Overloads:

  • #eachvoid

    This method returns an undefined value.

  • #eachEnumerator[NodeRef, void]

    Returns:

Yields:

Yield Parameters:

Yield Returns:

  • (void)


15
16
17
# File 'lib/red_quilt/node_ref.rb', line 15

def each(&)
  walk(&)
end

#find_all(type) ⇒ Array[NodeRef]

Parameters:

  • type (Symbol)

Returns:



130
131
132
# File 'lib/red_quilt/node_ref.rb', line 130

def find_all(type)
  walk.select { |node| node.type == type }
end

#footnote_labelString?

FOOTNOTE_DEFINITION / FOOTNOTE_REFERENCE: the label as authored.

Returns:

  • (String, nil)


105
106
107
# File 'lib/red_quilt/node_ref.rb', line 105

def footnote_label
  @arena.footnote_label(@node_id) if footnote_like?
end

#footnote_numberInteger?

FOOTNOTE_DEFINITION / FOOTNOTE_REFERENCE: the resolved 1-based number.

Returns:

  • (Integer, nil)


110
111
112
# File 'lib/red_quilt/node_ref.rb', line 110

def footnote_number
  @arena.footnote_number(@node_id) if footnote_like?
end

#header?Boolean

TABLE_ROW / TABLE_CELL: whether this belongs to the header row.

nil rather than false outside a table, matching the other attribute accessors: a paragraph is not a non-header row, it has no header-ness at all. Both answers are falsy, so if node.header? reads the same.

Returns:

  • (Boolean)


119
120
121
122
123
124
# File 'lib/red_quilt/node_ref.rb', line 119

def header?
  return @arena.table_row_header?(@node_id) if type?(NodeType::TABLE_ROW)
  return @arena.table_cell_header?(@node_id) if type?(NodeType::TABLE_CELL)

  nil # rubocop:disable Style/ReturnNilInPredicateMethodDefinition
end

#heading_levelInteger?

HEADING: nesting level (1..6).

Returns:

  • (Integer, nil)


70
71
72
# File 'lib/red_quilt/node_ref.rb', line 70

def heading_level
  @arena.heading_level(@node_id) if type?(NodeType::HEADING)
end

#infoString?

Node attributes: nil when the node's type does not carry the attribute.

Returns:

  • (String, nil)


63
64
65
66
67
# File 'lib/red_quilt/node_ref.rb', line 63

def info
  return nil unless type?(NodeType::CODE_BLOCK)

  @arena.code_block_info(@node_id).to_s
end

LINK / IMAGE: the destination URL.

Returns:

  • (String, nil)


95
96
97
# File 'lib/red_quilt/node_ref.rb', line 95

def link_destination
  @arena.link_destination(@node_id) if link_like?
end

LINK / IMAGE: the optional title, nil when absent.

Returns:

  • (String, nil)


100
101
102
# File 'lib/red_quilt/node_ref.rb', line 100

def link_title
  @arena.link_title(@node_id) if link_like?
end

#list_delimiterString?

LIST: the item delimiter as authored, e.g. "-" or "1.".

Returns:

  • (String, nil)


90
91
92
# File 'lib/red_quilt/node_ref.rb', line 90

def list_delimiter
  @arena.list_delimiter(@node_id) if type?(NodeType::LIST)
end

#list_ordered?Boolean

LIST: ordered (1.) vs bullet (-).

Returns:

  • (Boolean)


75
76
77
# File 'lib/red_quilt/node_ref.rb', line 75

def list_ordered?
  @arena.list_ordered?(@node_id) if type?(NodeType::LIST)
end

#list_startInteger?

LIST: start number of an ordered list.

Returns:

  • (Integer, nil)


80
81
82
# File 'lib/red_quilt/node_ref.rb', line 80

def list_start
  @arena.list_start(@node_id) if type?(NodeType::LIST)
end

#list_tight?Boolean

LIST: tight (no blank lines between items) vs loose.

Returns:

  • (Boolean)


85
86
87
# File 'lib/red_quilt/node_ref.rb', line 85

def list_tight?
  @arena.list_tight?(@node_id) if type?(NodeType::LIST)
end

#source_location{ start_line: Integer, start_column: Integer, end_line: Integer, end_column: Integer }?

Returns:

  • ({ start_line: Integer, start_column: Integer, end_line: Integer, end_column: Integer }, nil)


134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/red_quilt/node_ref.rb', line 134

def source_location
  span = source_span
  return nil unless span

  start_loc = @document.source_map.line_column(span.start_byte)
  end_loc = @document.source_map.line_column(span.end_byte)

  {
    start_line: start_loc[:line],
    start_column: start_loc[:column],
    end_line: end_loc[:line],
    end_column: end_loc[:column],
  }
end

#source_spanSourceSpan?

Returns:



126
127
128
# File 'lib/red_quilt/node_ref.rb', line 126

def source_span
  @arena.source_span(@node_id)
end

#textString?

Returns:

  • (String, nil)


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/red_quilt/node_ref.rb', line 36

def text
  first_child_id = @arena.raw_first_child_id(@node_id)
  return @arena.text(@node_id) if first_child_id == -1

  text = +""
  @arena.child_ids(@node_id).each do |child_id|
    child = NodeRef.new(@document, child_id)
    fragment = child.text
    text << fragment.to_s unless fragment.nil?
  end
  text
end

#to_hHash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


149
150
151
152
153
154
155
156
157
158
159
# File 'lib/red_quilt/node_ref.rb', line 149

def to_h
  ast = {
    type: type,
    source_span: source_span,
    children: children.map(&:to_h),
  }

  attributes = ast_attributes
  ast[:attributes] = attributes unless attributes.empty?
  ast
end

#typeSymbol

Returns:

  • (Symbol)


19
20
21
# File 'lib/red_quilt/node_ref.rb', line 19

def type
  @arena.type_name(@node_id)
end

#walkvoid #walkEnumerator[NodeRef, void]

Overloads:

  • #walkvoid

    This method returns an undefined value.

  • #walkEnumerator[NodeRef, void]

    Returns:

Yields:

  • (_self)

Yield Parameters:



27
28
29
30
31
32
33
34
# File 'lib/red_quilt/node_ref.rb', line 27

def walk(&block)
  return enum_for(:walk) unless block_given?

  yield self
  @arena.child_ids(@node_id).each do |child_id|
    NodeRef.new(@document, child_id).walk(&block)
  end
end