Class: Dispersion::Formatters::HTML

Inherits:
Object
  • Object
show all
Defined in:
lib/dispersion/formatters/html.rb

Instance Method Summary collapse

Constructor Details

#initialize(code, annotations:) ⇒ HTML

Returns a new instance of HTML.



8
9
10
11
# File 'lib/dispersion/formatters/html.rb', line 8

def initialize(code, annotations:)
	@code = code
	@annotations = annotations
end

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dispersion/formatters/html.rb', line 13

def call
	lines = @code.lines
	current_symbol = nil
	buffer = +""

	lines.each_with_index do |line, line_number|
		cursor = 0

		@annotations[line_number].each do |(symbol, column)|
			buffer << ERB::Escape.html_escape(line[cursor...column])
			cursor = column

			# Always reset the current symbol
			if current_symbol
				buffer << "</span>"
				current_symbol = nil
			end

			unless symbol == :reset
				buffer << %(<span data-dispersion-symbol="#{symbol.name}">)
				current_symbol = symbol
			end

			# binding.irb
		end

		buffer << ERB::Escape.html_escape(line[cursor..])
	end

	if current_symbol
		buffer << "</span>"
		current_symbol = nil
	end

	buffer.gsub(/\t/, "  ")
end