Skip to content

Commit a2e9acc

Browse files
author
Etienne Massip
committed
Fixed handling of eols inside token content.
1 parent 0e8008a commit a2e9acc

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

lib/coderay/encoders/html.rb

+10-6
Original file line numberDiff line numberDiff line change
@@ -256,19 +256,23 @@ def text_token text, kind
256256
if text =~ /#{HTML_ESCAPE_PATTERN}/o
257257
text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] }
258258
end
259-
if @independent_lines && @opened.any? && text.end_with?("\n")
260-
text.chomp!
261-
close_eol_reopen = "#{'</span>' * @opened.size}\n"
259+
260+
style = @span_for_kind[@last_opened ? [kind, *@opened] : kind]
261+
262+
if @independent_lines && (i = text.index("\n")) && (c = @opened.size + (style ? 1 : 0)) > 0
263+
close = '</span>' * c
264+
reopen = ''
262265
@opened.each_with_index do |k, index|
263-
close_eol_reopen << (@span_for_kind[index > 0 ? [k, *@opened[0 ... index ]] : k] || '<span>')
266+
reopen << (@span_for_kind[index > 0 ? [k, *@opened[0 ... index ]] : k] || '<span>')
264267
end
268+
text[i .. -1] = text[i .. -1].gsub("\n", "#{close}\n#{reopen}#{style}")
265269
end
266-
if style = @span_for_kind[@last_opened ? [kind, *@opened] : kind]
270+
271+
if style
267272
@out << style << text << '</span>'
268273
else
269274
@out << text
270275
end
271-
@out << close_eol_reopen if close_eol_reopen
272276
end
273277

274278
# token groups, eg. strings

0 commit comments

Comments
 (0)