Class: BetterHtml::Tokenizer::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/better_html/tokenizer/location.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, start, stop) ⇒ Location

Returns a new instance of Location.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
# File 'lib/better_html/tokenizer/location.rb', line 6

def initialize(document, start, stop)
  raise ArgumentError, "start location #{start} is out of range for document of size #{document.size}" if start > document.size
  raise ArgumentError, "stop location #{stop} is out of range for document of size #{document.size}" if stop > document.size
  raise ArgumentError, "end of range must be greater than start of range (#{stop} < #{start})" if stop < start

  @document = document
  @start = start
  @stop = stop
end

Instance Attribute Details

#documentObject

Returns the value of attribute document.



4
5
6
# File 'lib/better_html/tokenizer/location.rb', line 4

def document
  @document
end

#startObject

Returns the value of attribute start.



4
5
6
# File 'lib/better_html/tokenizer/location.rb', line 4

def start
  @start
end

#stopObject

Returns the value of attribute stop.



4
5
6
# File 'lib/better_html/tokenizer/location.rb', line 4

def stop
  @stop
end

Instance Method Details

#columnObject



44
45
46
# File 'lib/better_html/tokenizer/location.rb', line 44

def column
  start_column
end

#lineObject



32
33
34
# File 'lib/better_html/tokenizer/location.rb', line 32

def line
  start_line
end

#line_rangeObject



20
21
22
# File 'lib/better_html/tokenizer/location.rb', line 20

def line_range
  Range.new(start_line, stop_line)
end

#line_source_with_underlineObject



52
53
54
55
56
57
58
# File 'lib/better_html/tokenizer/location.rb', line 52

def line_source_with_underline
  line_content = extract_line(line: start_line)
  spaces = line_content.scan(/\A\s*/).first
  column_without_spaces = [column - spaces.length, 0].max
  underscore_length = [[stop - start + 1, line_content.length - column_without_spaces].min, 1].max
  "#{line_content.gsub(/\A\s*/, '')}\n#{' ' * column_without_spaces}#{'^' * underscore_length}"
end

#rangeObject



16
17
18
# File 'lib/better_html/tokenizer/location.rb', line 16

def range
  Range.new(start, stop)
end

#sourceObject



24
25
26
# File 'lib/better_html/tokenizer/location.rb', line 24

def source
  @document[range]
end

#start_columnObject



40
41
42
# File 'lib/better_html/tokenizer/location.rb', line 40

def start_column
  @start_column ||= calculate_column(start)
end

#start_lineObject



28
29
30
# File 'lib/better_html/tokenizer/location.rb', line 28

def start_line
  @start_line ||= calculate_line(start)
end

#stop_columnObject



48
49
50
# File 'lib/better_html/tokenizer/location.rb', line 48

def stop_column
  @stop_column ||= calculate_column(stop)
end

#stop_lineObject



36
37
38
# File 'lib/better_html/tokenizer/location.rb', line 36

def stop_line
  @stop_line ||= calculate_line(stop)
end