|
1 | 1 | require 'set'
|
| 2 | +require 'escape_utils' |
2 | 3 |
|
3 | 4 | module CodeRay
|
4 | 5 | module Encoders
|
@@ -127,21 +128,6 @@ class HTML < Encoder
|
127 | 128 |
|
128 | 129 | protected
|
129 | 130 |
|
130 |
| - def self.make_html_escape_hash |
131 |
| - { |
132 |
| - '&' => '&', |
133 |
| - '>' => '>', |
134 |
| - '<' => '<', |
135 |
| - "\t" => ' ' * DEFAULT_OPTIONS[:tab_width], |
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 |
141 |
| - |
142 |
| - HTML_ESCAPE = make_html_escape_hash |
143 |
| - HTML_ESCAPE_PATTERN = /[\t&><\0-\x8\xB-\x1F]/ |
144 |
| - |
145 | 131 | TOKEN_KIND_TO_INFO = Hash.new do |h, kind|
|
146 | 132 | h[kind] = kind.to_s.gsub(/_/, ' ').gsub(/\b\w/) { $&.capitalize }
|
147 | 133 | end
|
@@ -180,8 +166,6 @@ def setup options
|
180 | 166 |
|
181 | 167 | @break_lines = (options[:break_lines] == true)
|
182 | 168 |
|
183 |
| - @escape_cache = make_escape_cache(options) |
184 |
| - |
185 | 169 | @opened = []
|
186 | 170 | @last_opened = nil
|
187 | 171 | @css = CSS.new options[:style]
|
@@ -220,7 +204,7 @@ def finish options
|
220 | 204 | def text_token text, kind
|
221 | 205 | style = @span_for_kinds[@last_opened ? [kind, *@opened] : kind]
|
222 | 206 |
|
223 |
| - text = @escape_cache[text] if text.size <= 1 || text =~ /#{HTML_ESCAPE_PATTERN}/o |
| 207 | + text = EscapeUtils.escape_html text |
224 | 208 | text = break_lines(text, style) if @break_lines && (style || @opened.size > 0) && text.index("\n")
|
225 | 209 |
|
226 | 210 | if style
|
@@ -276,26 +260,6 @@ def check_options! options
|
276 | 260 | options[:break_lines] = true if options[:line_numbers] == :inline
|
277 | 261 | end
|
278 | 262 |
|
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] ? ' ' * options[:tab_width] : "\t") |
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 |
| - |
299 | 263 | def css_class_for_kinds kinds
|
300 | 264 | TokenKinds[kinds.is_a?(Symbol) ? kinds : kinds.first]
|
301 | 265 | end
|
|
0 commit comments