@@ -128,11 +128,11 @@ class HTML < Encoder
128
128
129
129
def self . make_html_escape_hash
130
130
{
131
- '&' => '&' ,
132
- '"' => '"' ,
133
- '>' => '>' ,
134
- '<' => '<' ,
135
- # "\t" => will be set to ' ' * options [:tab_width] during setup
131
+ '&' => '&' ,
132
+ '"' => '"' ,
133
+ '>' => '>' ,
134
+ '<' => '<' ,
135
+ "\t " => ' ' * DEFAULT_OPTIONS [ :tab_width ] ,
136
136
} . tap do |hash |
137
137
# Escape ASCII control codes except \x9 == \t and \xA == \n.
138
138
( Array ( 0x00 ..0x8 ) + Array ( 0xB ..0x1F ) ) . each { |invalid | hash [ invalid . chr ] = ' ' }
@@ -178,19 +178,10 @@ def setup options
178
178
@out = ''
179
179
end
180
180
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
192
181
@break_lines = ( options [ :break_lines ] == true )
193
182
183
+ @escape_cache = make_escape_cache ( options )
184
+
194
185
@opened = [ ]
195
186
@last_opened = nil
196
187
@css = CSS . new options [ :style ]
@@ -285,6 +276,26 @@ def check_options! options
285
276
options [ :break_lines ] = true if options [ :line_numbers ] == :inline
286
277
end
287
278
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
+
288
299
def css_class_for_kinds kinds
289
300
TokenKinds [ kinds . is_a? ( Symbol ) ? kinds : kinds . first ]
290
301
end
0 commit comments