Class: NRSER::MeanStreak::Document
- Inherits:
-
Object
- Object
- NRSER::MeanStreak::Document
- Defined in:
- lib/nrser/mean_streak/document.rb
Overview
document NRSER::MeanStreak::Document class.
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
-
#initialize(mean_streak:, source:, doc:) ⇒ Document
constructor
Instantiate a new ‘NRSER::MeanStreak::Document`.
- #render_node(node, output = '') ⇒ 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`.
93 94 95 96 97 |
# File 'lib/nrser/mean_streak/document.rb', line 93 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`).
82 83 84 |
# File 'lib/nrser/mean_streak/document.rb', line 82 def doc @doc end |
#mean_streak ⇒ NRSER::MeanStreak (readonly)
The NRSER::MeanStreak instance associated with this document, which contains the rendering configuration.
68 69 70 |
# File 'lib/nrser/mean_streak/document.rb', line 68 def mean_streak @mean_streak end |
#source ⇒ String (readonly)
The source string.
75 76 77 |
# File 'lib/nrser/mean_streak/document.rb', line 75 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.
46 47 48 49 50 51 52 53 |
# File 'lib/nrser/mean_streak/document.rb', line 46 def self.parse source, mean_streak:, cm_options: :DEFAULT, cm_extensions: [] new mean_streak: mean_streak, source: source, doc: CommonMarker.render_doc( source, , extensions ) end |
Instance Method Details
#render_node(node, output = '') ⇒ Object
143 144 145 |
# File 'lib/nrser/mean_streak/document.rb', line 143 def render_node node, output = '' end |
#source_for_node(node) ⇒ String
Get the substring of the source that a node came from (via its ‘#sourcepos`).
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/nrser/mean_streak/document.rb', line 117 def source_for_node node pos = node.sourcepos if pos[:start_line] == pos[:end_line] source_lines[pos[:start_line] - 1][ (pos[:start_column] - 1)...pos[:end_column] ] else lines = source_lines[(pos[:start_line] - 1)...pos[:end_line]] # Trim the start off the first line, unless the start column is 1 unless pos[:start_column] == 1 lines[0] = lines[0][(pos[:start_column] - 1)..-1] end # Trim the end off the first line, unless the end column is the last # line's length unless pos[:end_column] == lines[-1].length lines[-1] = lines[-1][0...pos[:end_column]] end lines.join end end |
#source_lines ⇒ Hamster::Vector<String>
The lines in #source as a Hamster::Vector of frozen strings.
107 108 109 |
# File 'lib/nrser/mean_streak/document.rb', line 107 def source_lines @source_lines = Hamster::Vector.new source.lines.map( &:freeze ) end |