Skip to content

Commit 370c6d3

Browse files
committed
cleanup changes into make_escape_cache
1 parent 4f19f44 commit 370c6d3

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

lib/coderay/encoders/html.rb

+27-16
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ class HTML < Encoder
128128

129129
def self.make_html_escape_hash
130130
{
131-
'&' => '&amp;',
132-
'"' => '&quot;',
133-
'>' => '&gt;',
134-
'<' => '&lt;',
135-
# "\t" => will be set to ' ' * options[:tab_width] during setup
131+
'&' => '&amp;',
132+
'"' => '&quot;',
133+
'>' => '&gt;',
134+
'<' => '&lt;',
135+
"\t" => ' ' * DEFAULT_OPTIONS[:tab_width],
136136
}.tap do |hash|
137137
# Escape ASCII control codes except \x9 == \t and \xA == \n.
138138
(Array(0x00..0x8) + Array(0xB..0x1F)).each { |invalid| hash[invalid.chr] = ' ' }
@@ -178,19 +178,10 @@ def setup options
178178
@out = ''
179179
end
180180

181-
@tab_replacement = ' ' * options[:tab_width]
182-
@escape_cache = Hash.new do |cache, text|
183-
cache.clear if cache.size >= 100
184-
185-
cache[text] =
186-
if text =~ /#{HTML_ESCAPE_PATTERN}/o
187-
text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| m == "\t" ? @tab_replacement : HTML_ESCAPE[m] }
188-
else
189-
text
190-
end
191-
end
192181
@break_lines = (options[:break_lines] == true)
193182

183+
@escape_cache = make_escape_cache(options)
184+
194185
@opened = []
195186
@last_opened = nil
196187
@css = CSS.new options[:style]
@@ -285,6 +276,26 @@ def check_options! options
285276
options[:break_lines] = true if options[:line_numbers] == :inline
286277
end
287278

279+
def make_escape_cache options
280+
html_escape =
281+
if options[:tab_width] == DEFAULT_OPTIONS[:tab_width]
282+
HTML_ESCAPE
283+
else
284+
HTML_ESCAPE.merge("\t" => ' ' * options[:tab_width])
285+
end
286+
287+
Hash.new do |cache, text|
288+
cache.clear if cache.size >= 100
289+
290+
cache[text] =
291+
if text =~ /#{HTML_ESCAPE_PATTERN}/o
292+
text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| html_escape[m] }
293+
else
294+
text
295+
end
296+
end
297+
end
298+
288299
def css_class_for_kinds kinds
289300
TokenKinds[kinds.is_a?(Symbol) ? kinds : kinds.first]
290301
end

0 commit comments

Comments
 (0)