8000 use EscapeUtils · rubychan/coderay@aa9229f · GitHub
[go: up one dir, main page]

Skip to content

Commit aa9229f

Browse files
committed
use EscapeUtils
1 parent 3e10d56 commit aa9229f

File tree

2 files changed

+4
-38
lines changed

2 files changed

+4
-38
lines changed

coderay.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ Gem::Specification.new do |s|
3232
s.rubyforge_project = s.name
3333
s.rdoc_options = '-SNw2', "-m#{readme_file}", '-t CodeRay Documentation'
3434
s.extra_rdoc_files = readme_file
35+
36+
s.add_dependency 'escape_utils', '>= 1.0'
3537
end

lib/coderay/encoders/html.rb

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'set'
2+
require 'escape_utils'
23

34
module CodeRay
45
module Encoders
@@ -127,21 +128,6 @@ class HTML < Encoder
127128

128129
protected
129130

130-
def self.make_html_escape_hash
131-
{
132-
'&' => '&amp;',
133-
'>' => '&gt;',
134-
'<' => '&lt;',
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-
145131
TOKEN_KIND_TO_INFO = Hash.new do |h, kind|
146132
h[kind] = kind.to_s.gsub(/_/, ' ').gsub(/\b\w/) { $&.capitalize }
147133
end
@@ -180,8 +166,6 @@ def setup options
180166

181167
@break_lines = (options[:break_lines] == true)
182168

183-
@escape_cache = make_escape_cache(options)
184-
185169
@opened = []
186170
@last_opened = nil
187171
@css = CSS.new options[:style]
@@ -220,7 +204,7 @@ def finish options
220204
def text_token text, kind
221205
style = @span_for_kinds[@last_opened ? [kind, *@opened] : kind]
222206

223-
text = @escape_cache[text] if text.size <= 1 || text =~ /#{HTML_ESCAPE_PATTERN}/o
207+
text = EscapeUtils.escape_html text
224208
text = break_lines(text, style) if @break_lines && (style || @opened.size > 0) && text.index("\n")
225209

226210
if style
@@ -276,26 +260,6 @@ def check_options! options
276260
options[:break_lines] = true if options[:line_numbers] == :inline
277261
end
278262

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-
299263
def css_class_for_kinds kinds
300264
TokenKinds[kinds.is_a?(Symbol) ? kinds : kinds.first]
301265
end

0 commit comments

Comments
 (0)
0