Class: NRSER::MeanStreak::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/nrser/mean_streak/document.rb

Overview

TODO:

document NRSER::MeanStreak::Document class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mean_streak:, source:, doc:) ⇒ Document

Instantiate a new ‘NRSER::MeanStreak::Document`.

Parameters:



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

#docCommonMarker::Node (readonly)

The root CommonMarker::Node (with CommonMarker::Node#type=‘:document`).

Returns:

  • (CommonMarker::Node)


82
83
84
# File 'lib/nrser/mean_streak/document.rb', line 82

def doc
  @doc
end

#mean_streakNRSER::MeanStreak (readonly)

The NRSER::MeanStreak instance associated with this document, which contains the rendering configuration.

Returns:



68
69
70
# File 'lib/nrser/mean_streak/document.rb', line 68

def mean_streak
  @mean_streak
end

#sourceString (readonly)

The source string.

Returns:

  • (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

TODO:

Document from method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:



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, options, 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`).

Returns:

  • (String)


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_linesHamster::Vector<String>

The lines in #source as a Hamster::Vector of frozen strings.

Returns:

  • (Hamster::Vector<String>)


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