Class: RedQuilt::NodeRef
- Inherits:
-
Object
- Object
- RedQuilt::NodeRef
- Includes:
- Enumerable, Enumerable[NodeRef]
- Defined in:
- lib/red_quilt/node_ref.rb,
sig/red_quilt.rbs
Instance Attribute Summary collapse
-
#document ⇒ Document
readonly
Returns the value of attribute document.
-
#node_id ⇒ Integer
readonly
Returns the value of attribute node_id.
Instance Method Summary collapse
- #children ⇒ Array[NodeRef]
- #each {|arg0| ... } ⇒ Object
- #find_all(type) ⇒ Array[NodeRef]
-
#footnote_label ⇒ String?
FOOTNOTE_DEFINITION / FOOTNOTE_REFERENCE: the label as authored.
-
#footnote_number ⇒ Integer?
FOOTNOTE_DEFINITION / FOOTNOTE_REFERENCE: the resolved 1-based number.
-
#header? ⇒ Boolean
TABLE_ROW / TABLE_CELL: whether this belongs to the header row.
-
#heading_level ⇒ Integer?
HEADING: nesting level (1..6).
-
#info ⇒ String?
Node attributes: nil when the node's type does not carry the attribute.
-
#initialize(document, node_id) ⇒ NodeRef
constructor
A new instance of NodeRef.
-
#link_destination ⇒ String?
LINK / IMAGE: the destination URL.
-
#link_title ⇒ String?
LINK / IMAGE: the optional title, nil when absent.
-
#list_delimiter ⇒ String?
LIST: the item delimiter as authored, e.g.
-
#list_ordered? ⇒ Boolean
LIST: ordered (
1.) vs bullet (-). -
#list_start ⇒ Integer?
LIST: start number of an ordered list.
-
#list_tight? ⇒ Boolean
LIST: tight (no blank lines between items) vs loose.
- #source_location ⇒ { start_line: Integer, start_column: Integer, end_line: Integer, end_column: Integer }?
- #source_span ⇒ SourceSpan?
- #text ⇒ String?
- #to_h ⇒ Hash[Symbol, untyped]
- #type ⇒ Symbol
- #walk {|_self| ... } ⇒ Object
Constructor Details
#initialize(document, node_id) ⇒ NodeRef
Returns a new instance of NodeRef.
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
#document ⇒ Document (readonly)
Returns the value of attribute document.
7 8 9 |
# File 'lib/red_quilt/node_ref.rb', line 7 def document @document end |
#node_id ⇒ Integer (readonly)
Returns the value of attribute node_id.
7 8 9 |
# File 'lib/red_quilt/node_ref.rb', line 7 def node_id @node_id end |
Instance Method Details
#children ⇒ Array[NodeRef]
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 |
#each ⇒ void #each ⇒ Enumerator[NodeRef, void]
15 16 17 |
# File 'lib/red_quilt/node_ref.rb', line 15 def each(&) walk(&) end |
#find_all(type) ⇒ Array[NodeRef]
130 131 132 |
# File 'lib/red_quilt/node_ref.rb', line 130 def find_all(type) walk.select { |node| node.type == type } end |
#footnote_label ⇒ String?
FOOTNOTE_DEFINITION / FOOTNOTE_REFERENCE: the label as authored.
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_number ⇒ Integer?
FOOTNOTE_DEFINITION / FOOTNOTE_REFERENCE: the resolved 1-based number.
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.
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_level ⇒ Integer?
HEADING: nesting level (1..6).
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 |
#info ⇒ String?
Node attributes: nil when the node's type does not carry the attribute.
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_destination ⇒ String?
LINK / IMAGE: the destination URL.
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_title ⇒ String?
LINK / IMAGE: the optional title, nil when absent.
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_delimiter ⇒ String?
LIST: the item delimiter as authored, e.g. "-" or "1.".
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 (-).
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_start ⇒ Integer?
LIST: start number of an ordered list.
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.
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 }?
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_span ⇒ SourceSpan?
126 127 128 |
# File 'lib/red_quilt/node_ref.rb', line 126 def source_span @arena.source_span(@node_id) end |
#text ⇒ String?
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_h ⇒ 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 |
#type ⇒ Symbol
19 20 21 |
# File 'lib/red_quilt/node_ref.rb', line 19 def type @arena.type_name(@node_id) end |