Module: Coradoc::AsciiDoc::Transformer::SourceLineExtractor
- Defined in:
- lib/coradoc/asciidoc/transformer/source_line_extractor.rb
Overview
Walks a Parslet AST subtree (Hash/Array/Slice/String/Model) and
returns the 1-indexed source line of the first Parslet::Slice
or Model::Base#source_line it finds.
Parslet preserves byte offsets on every matched slice
(+slice.line_and_column+ returns [line, column]); the
transformer receives these slices in the AST but most rules
discard the position when building Model objects. This helper
recovers the start line of any subtree so transformer rules
can populate Model::Base#source_line without changing the
parser.
Handles two distinct shapes:
* Pre-transform AST (Hash/Array of Parslet::Slice) — used by
rules that bind raw slices via +simple(:x)+.
* Post-transform Model tree (Model::Base instances whose
+source_line+ was populated by an earlier rule) — used by
rules that bind via +subtree(:x)+ and receive already-
transformed content.
Returns nil when no position is found (programmatic input, already-stripped ASTs, etc.) — callers should treat nil as "source position unavailable".
Class Method Summary collapse
- .extract(node) ⇒ Object
- .extract_from_array(array) ⇒ Object
- .extract_from_hash(hash) ⇒ Object
- .line_of(slice) ⇒ Object
Class Method Details
.extract(node) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/coradoc/asciidoc/transformer/source_line_extractor.rb', line 34 def extract(node) case node when Parslet::Slice then line_of(node) when Coradoc::AsciiDoc::Model::Base then node.source_line when Hash then extract_from_hash(node) when Array then extract_from_array(node) end end |
.extract_from_array(array) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/coradoc/asciidoc/transformer/source_line_extractor.rb', line 56 def extract_from_array(array) array.each do |value| line = extract(value) return line if line end nil end |
.extract_from_hash(hash) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/coradoc/asciidoc/transformer/source_line_extractor.rb', line 48 def extract_from_hash(hash) hash.each_value do |value| line = extract(value) return line if line end nil end |
.line_of(slice) ⇒ Object
43 44 45 46 |
# File 'lib/coradoc/asciidoc/transformer/source_line_extractor.rb', line 43 def line_of(slice) line, _column = slice.line_and_column line end |