Class: Commonmarker::Merge::Backend::Node
- Inherits:
-
TreeHaver::Base::Node
- Object
- TreeHaver::Base::Node
- Commonmarker::Merge::Backend::Node
- Defined in:
- lib/commonmarker/merge/backend.rb
Overview
Commonmarker node wrapper
Wraps Commonmarker::Node to provide TreeHaver::Node-compatible interface.
Instance Method Summary collapse
-
#children ⇒ Array<Node>
Get child nodes.
-
#end_byte ⇒ Object
Get end byte offset.
-
#end_point ⇒ Point
Get end point (0-based row/column).
-
#fence_info ⇒ String?
Get fence info for code blocks.
-
#header_level ⇒ Integer?
Get heading level (1-6).
-
#next_sibling ⇒ Node?
Get the next sibling.
-
#parent ⇒ Node?
Get the parent node.
-
#prev_sibling ⇒ Node?
Get the previous sibling.
-
#start_byte ⇒ Object
Get start byte offset.
-
#start_point ⇒ Point
Get start point (0-based row/column).
-
#text ⇒ String
Get the text content of this node.
-
#title ⇒ String?
Get title for links/images.
-
#type ⇒ String
(also: #kind)
Get the node type as a string.
-
#url ⇒ String?
Get URL for links/images.
Instance Method Details
#children ⇒ Array<Node>
Get 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_byte ⇒ Object
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_point ⇒ Point
Get end point (0-based row/column)
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_info ⇒ String?
Get fence info for code blocks
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_level ⇒ Integer?
Get heading level (1-6)
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_sibling ⇒ Node?
Get the next sibling
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 |
#parent ⇒ Node?
Get the parent node
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_sibling ⇒ Node?
Get the previous sibling
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_byte ⇒ Object
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_point ⇒ Point
Get start point (0-based row/column)
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 |
#text ⇒ String
Get the text content of this node
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 |
#title ⇒ String?
Get title for links/images
274 275 276 277 278 |
# File 'lib/commonmarker/merge/backend.rb', line 274 def title inner_node.title rescue nil end |
#type ⇒ String Also known as: kind
Get the node type as a string
146 147 148 |
# File 'lib/commonmarker/merge/backend.rb', line 146 def type inner_node.type.to_s end |
#url ⇒ String?
Get URL for links/images
266 267 268 269 270 |
# File 'lib/commonmarker/merge/backend.rb', line 266 def url inner_node.url rescue nil end |