Skip to content
This repository was archived by the owner on Feb 6, 2018. It is now read-only.

Commit 9d41587

Browse files
committed
cleanup HTML encoder (rubychan#135)
1 parent e2acec3 commit 9d41587

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

lib/coderay/encoders/html.rb

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,21 @@ class HTML < Encoder
126126

127127
protected
128128

129-
HTML_ESCAPE = { #:nodoc:
130-
'&' => '&amp;',
131-
'"' => '&quot;',
132-
'>' => '&gt;',
133-
'<' => '&lt;',
134-
}
129+
def self.make_html_escape_hash
130+
{
131+
'&' => '&amp;',
132+
'"' => '&quot;',
133+
'>' => '&gt;',
134+
'<' => '&lt;',
135+
# "\t" => will be set to ' ' * options[:tab_width] during setup
136+
}.tap do |hash|
137+
# Escape ASCII control codes except \x9 == \t and \xA == \n.
138+
(Array(0x00..0x8) + Array(0xB..0x1F)).each { |invalid| hash[invalid.chr] = ' ' }
139+
end
140+
end
135141

136-
# This was to prevent illegal HTML.
137-
# Strange chars should still be avoided in codes.
138-
evil_chars = Array(0x00...0x20) - [?\n, ?\t, ?\s]
139-
evil_chars.each { |i| HTML_ESCAPE[i.chr] = ' ' }
140-
#ansi_chars = Array(0x7f..0xff)
141-
#ansi_chars.each { |i| HTML_ESCAPE[i.chr] = '&#%d;' % i }
142-
# \x9 (\t) and \xA (\n) not included
143-
#HTML_ESCAPE_PATTERN = /[\t&"><\0-\x8\xB-\x1f\x7f-\xff]/
144-
HTML_ESCAPE_PATTERN = /[\t"&><\0-\x8\xB-\x1f]/
142+
HTML_ESCAPE = make_html_escape_hash
143+
HTML_ESCAPE_PATTERN = /[\t"&><\0-\x8\xB-\x1F]/
145144

146145
TOKEN_KIND_TO_INFO = Hash.new do |h, kind|
147146
h[kind] = kind.to_s.gsub(/_/, ' ').gsub(/\b\w/) { $&.capitalize }
@@ -255,20 +254,10 @@ def finish options
255254
public
256255

257256
def text_token text, kind
258-
if text =~ /#{HTML_ESCAPE_PATTERN}/o
259-
text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] }
260-
end
261-
262257
style = @span_for_kind[@last_opened ? [kind, *@opened] : kind]
263258

264-
if @break_lines && (i = text.index("\n")) && (c = @opened.size + (style ? 1 : 0)) > 0
265-
close = '</span>' * c
266-
reopen = ''
267-
@opened.each_with_index do |k, index|
268-
reopen << (@span_for_kind[index > 0 ? [k, *@opened[0 ... index ]] : k] || '<span>')
269-
end
270-
text[i .. -1] = text[i .. -1].gsub("\n", "#{close}\n#{reopen}#{style}")
271-
end
259+
text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } if text =~ /#{HTML_ESCAPE_PATTERN}/o
260+
text = break_lines(text, style) if @break_lines && (style || @opened.size > 0) && text.index("\n")
272261

273262
if style
274263
@out << style << text << '</span>'
@@ -285,9 +274,7 @@ def begin_group kind
285274
end
286275

287276
def end_group kind
288-
if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind)
289-
warn 'Malformed token stream: Trying to close a token group (%p) that is not open. Open are: %p.' % [kind, @opened[1..-1]]
290-
end
277+
check_group_nesting 'token group', kind if $CODERAY_DEBUG
291278
if @opened.pop
292279
@out << '</span>'
293280
@last_opened = @opened.last if @last_opened
@@ -310,15 +297,28 @@ def begin_line kind
310297
end
311298

312299
def end_line kind
313-
if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind)
314-
warn 'Malformed token stream: Trying to close a line (%p) that is not open. Open are: %p.' % [kind, @opened[1..-1]]
315-
end
300+
check_group_nesting 'line', kind if $CODERAY_DEBUG
316301
if @opened.pop
317302
@out << '</span>'
318303
@last_opened = @opened.last if @last_opened
319304
end
320305
end
321306

307+
protected
308+
309+
def check_group_nesting name, kind
310+
if @opened.empty? || @opened.last != kind
311+
warn "Malformed token stream: Trying to close a #{name} (%p) that is not open. Open are: %p." % [kind, @opened[1..-1]]
312+
end
313+
end
314+
315+
def break_lines text, style
316+
reopen = ''
317+
@opened.each_with_index do |k, index|
318+
reopen << (@span_for_kind[index > 0 ? [k, *@opened[0...index]] : k] || '<span>')
319+
end
320+
text.gsub("\n", "#{'</span>' * @opened.size}#{'</span>' if style}\n#{reopen}#{style}")
321+
end
322322
end
323323

324324
end

0 commit comments

Comments
 (0)