Class: NRSER::MeanStreak::Document
- Defined in:
- lib/nrser/mean_streak/document.rb
Overview
document NRSER::MeanStreak::Document class.
Defined Under Namespace
Classes: SourcePosition
Instance Attribute Summary collapse
-
#doc ⇒ CommonMarker::Node
readonly
The root CommonMarker::Node (with CommonMarker::Node#type=‘:document`).
-
#mean_streak ⇒ NRSER::MeanStreak
readonly
The NRSER::MeanStreak instance associated with this document, which contains the rendering configuration.
-
#source ⇒ String
readonly
The source string.
Class Method Summary collapse
Instance Method Summary collapse
- #find_by(**attrs) ⇒ Object
- #find_type(type) ⇒ Object
-
#initialize(mean_streak:, source:, doc:) ⇒ Document
constructor
Instantiate a new ‘NRSER::MeanStreak::Document`.
-
#pastel ⇒ Object
Instance Methods ======================================================================.
- #render ⇒ Object
- #render_children(node) ⇒ Object
- #render_node(node) ⇒ Object
- #render_node_2(prev_node, source_before, node, source_after, next_node) ⇒ Object
- #source_after_last_child(node) ⇒ Object
- #source_before_first_child(node) ⇒ Object
- #source_between(start_node, end_node) ⇒ Object
-
#source_byte_index(line:, column:) ⇒ Object
Computes the byte index for a ‘line` and `column` in the source, both of which *are in bytes*.
-
#source_byte_indexes(node) ⇒ Hash<Symbol, Hash<Symbol, Integer>>
All integers are zero-indexed byte array indexes.
- #source_byteslice(**kwds) ⇒ Object
-
#source_for_node(node) ⇒ String
Get the substring of the source that a node came from (via its ‘#sourcepos`).
-
#source_lines ⇒ Hamster::Vector<String>
The lines in #source as a Hamster::Vector of frozen strings.
Constructor Details
#initialize(mean_streak:, source:, doc:) ⇒ Document
Instantiate a new ‘NRSER::MeanStreak::Document`.
99 100 101 102 103 |
# File 'lib/nrser/mean_streak/document.rb', line 99 def initialize mean_streak:, source:, doc: @mean_streak = mean_streak @source = source.dup.freeze @doc = doc end |
Instance Attribute Details
#doc ⇒ CommonMarker::Node (readonly)
The root CommonMarker::Node (with CommonMarker::Node#type=‘:document`).
88 89 90 |
# File 'lib/nrser/mean_streak/document.rb', line 88 def doc @doc end |
#mean_streak ⇒ NRSER::MeanStreak (readonly)
The NRSER::MeanStreak instance associated with this document, which contains the rendering configuration.
74 75 76 |
# File 'lib/nrser/mean_streak/document.rb', line 74 def mean_streak @mean_streak end |
#source ⇒ String (readonly)
The source string.
81 82 83 |
# File 'lib/nrser/mean_streak/document.rb', line 81 def source @source end |
Class Method Details
.parse(source, mean_streak:, cm_options: :DEFAULT, cm_extensions: []) ⇒ NRSER::MeanStreak::Document
Document from method.
Returns @todo Document return value.
53 54 55 56 57 58 59 60 |
# File 'lib/nrser/mean_streak/document.rb', line 53 def self.parse source, mean_streak:, cm_options: :DEFAULT, cm_extensions: [] new mean_streak: mean_streak, source: source, doc: CommonMarker.render_doc( source, , cm_extensions ) end |
Instance Method Details
#find_by(**attrs) ⇒ Object
350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/nrser/mean_streak/document.rb', line 350 def find_by **attrs doc.walk.find_all { |node| attrs.all? { |name, value| begin value === node.send( name ) rescue false end } } end |
#find_type(type) ⇒ Object
363 364 365 |
# File 'lib/nrser/mean_streak/document.rb', line 363 def find_type type doc.walk.find_all { |node| node.type == type } end |
#pastel ⇒ Object
Instance Methods
109 110 111 |
# File 'lib/nrser/mean_streak/document.rb', line 109 def pastel @pastel ||= Pastel.new end |
#render ⇒ Object
345 346 347 |
# File 'lib/nrser/mean_streak/document.rb', line 345 def render render_node doc end |
#render_children(node) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/nrser/mean_streak/document.rb', line 279 def render_children node prev = nil parts = [] node.each do |child| unless prev.nil? between = source_between( prev, child ) # We may need to modify the source strings *surrounding* specific # nodes... # # `:code` is an example thus far: it stores the code string in # `#string_content` so - unlike `:emph` and `:strong` where the # delimiters end up in the "before first child" and "after last child" # *inside* the node's source slice - in `:code` they end up *outside*, # in the source between it and the previous and next sibling nodes. # # # if mean_streak.type_renderers[:code] # There is a renderer for the `:code` type, so assume that it will # take are of any surrounding characters for `:code` strings and # chomp off the starting and ending backticks if prev.type == :code # Previous node is `:code`, chomp off any leading backtick # between = between[1..-1] if between.start_with?( '`' ) between = between.sub /\A`+/, '' elsif child.type == :code # Current node is `:code`, chomp off any leading backtick between = between.sub /`+\z/, '' end end parts << between end parts << render_node( child ) prev = child end parts.join end |
#render_node(node) ⇒ Object
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/nrser/mean_streak/document.rb', line 328 def render_node node if mean_streak.type_renderers[node.type] mean_streak.type_renderers[node.type].call self, node else if node.first # Has children source_before_first_child( node ) + render_children( node ) + source_after_last_child( node ) else # No children! Easy! source_for_node node end end end |
#render_node_2(prev_node, source_before, node, source_after, next_node) ⇒ Object
323 324 325 |
# File 'lib/nrser/mean_streak/document.rb', line 323 def render_node_2 prev_node, source_before, node, source_after, next_node end |
#source_after_last_child(node) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/nrser/mean_streak/document.rb', line 260 def source_after_last_child node last_child = node.each.to_a.last slice = source_byteslice( start_after: source_byte_indexes( last_child )[:last_byte][:index], end_on: source_byte_indexes( node )[:last_byte][:index] ) # See comments in {#render_children} if mean_streak.type_renderers[:code] && last_child.type == :code && slice.end_with?( '`' ) slice = slice.sub /`+\z/, '' end slice end |
#source_before_first_child(node) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/nrser/mean_streak/document.rb', line 243 def source_before_first_child node slice = source_byteslice( start_on: source_byte_indexes( node )[:first_byte][:index], end_before: source_byte_indexes( node.first )[:first_byte][:index], ) # See comments in {#render_children} if mean_streak.type_renderers[:code] && node.first.type == :code && slice.start_with?( '`' ) slice = slice.sub /\A`+/, '' end slice end |
#source_between(start_node, end_node) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/nrser/mean_streak/document.rb', line 230 def source_between start_node, end_node # Ok, all we do is take a byte slice based on their byte indexes source_byteslice( # The index of the start node's last type is the byte just *before* # the slice starts start_after: source_byte_indexes( start_node )[:last_byte][:index], # The index of the end node's first byte is the byte just *after* the # slice ends end_before: source_byte_indexes( end_node )[:first_byte][:index] ) end |
#source_byte_index(line:, column:) ⇒ Object
Computes the byte index for a ‘line` and `column` in the source, both of which *are in bytes*.
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/nrser/mean_streak/document.rb', line 169 def source_byte_index line:, column: t.hash_(keys: t.sym, values: t.non_neg_int?).check( line: line, column: column) # source_lines[0...line].map( &:bytesize ).reduce( column, :+ ) byte_index = column if line > 0 source_lines[0..(line - 1)].each { |_| byte_index += _.bytesize } end byte_index end |
#source_byte_indexes(node) ⇒ Hash<Symbol, Hash<Symbol, Integer>>
All integers are zero-indexed byte array indexes.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/nrser/mean_streak/document.rb', line 141 def source_byte_indexes node pos = node.sourcepos indexes = { first_byte: { line: [0, pos[:start_line] - 1].max, column: [0, pos[:start_column] - 1].max, }, last_byte: { line: [0, pos[:end_line] - 1].max, column: [0, pos[:end_column] - 1].max, }, } indexes.each do |key, byte_index_pos| byte_index_pos[:index] = source_byte_index **byte_index_pos end indexes end |
#source_byteslice(**kwds) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/nrser/mean_streak/document.rb', line 181 def source_byteslice **kwds t.and( # All the values must be `nil` or non-negative integer t.hash_(keys: t.sym, values: t.non_neg_int?), # Exactly one of `start_on` and `start_after` must be `nil` t.xor(t.shape(start_on: nil), t.shape(start_after: nil)), # Exactly one of `end_on` and `end_before` must be `nil` t.xor(t.shape(end_on: nil), t.shape(end_before: nil)) ).check kwds # The first byte we're gonna slice is either the `start_on` keyword # provided or the `start_after` bumped forward by 1 (which we can do # because it must point to the last *byte* before the slice, *not the # character*, so +1 gets us to the first slice byte) start_on = kwds[:start_on] || (kwds[:start_after] + 1) # In the same way, we can figure out the last byte after the slice end_before = kwds[:end_before] || (kwds[:end_on] + 1) # Sanity check if start_on > end_before # We done fucked up, which is not that unusual for me with this shit raise "Shit... start_on: #{ start_on }, end_before: #{ end_before }" end # Take the slice... the `...` range seems kinda easier 'cause the resulting # byte size is the difference `next_byte - end_before` source.byteslice start_on...end_before end |
#source_for_node(node) ⇒ String
Get the substring of the source that a node came from (via its ‘#sourcepos`).
217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/nrser/mean_streak/document.rb', line 217 def source_for_node node indexes = source_byte_indexes node # This one is *really* easy now! source_byteslice( # Start on the first byte start_on: indexes[:first_byte][:index], # End on the last end_on: indexes[:last_byte][:index] ) end |
#source_lines ⇒ Hamster::Vector<String>
The lines in #source as a Hamster::Vector of frozen strings.
118 119 120 |
# File 'lib/nrser/mean_streak/document.rb', line 118 def source_lines @source_lines ||= Hamster::List[*source.lines.map( &:freeze )] end |