summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKornelius Kalnbach <murphy@rubychan.de>2013-09-20 17:45:14 +0200
committerKornelius Kalnbach <murphy@rubychan.de>2013-09-20 17:45:14 +0200
commit370c6d3c3257da94f68a520467751e078bfdd419 (patch)
treea472b28aad8e7dfc55ae415d0c0adc84910619f8
parent4f19f440bdbd475f17d69e9f9d97ca0db151e0e9 (diff)
downloadcoderay-370c6d3c3257da94f68a520467751e078bfdd419.tar.gz
cleanup changes into make_escape_cache
-rw-r--r--lib/coderay/encoders/html.rb43
1 files changed, 27 insertions, 16 deletions
diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb
index 0f38726..c21b19d 100644
--- a/lib/coderay/encoders/html.rb
+++ b/lib/coderay/encoders/html.rb
@@ -128,11 +128,11 @@ module Encoders
def self.make_html_escape_hash
{
- '&' => '&amp;',
- '"' => '&quot;',
- '>' => '&gt;',
- '<' => '&lt;',
- # "\t" => will be set to ' ' * options[:tab_width] during setup
+ '&' => '&amp;',
+ '"' => '&quot;',
+ '>' => '&gt;',
+ '<' => '&lt;',
+ "\t" => ' ' * DEFAULT_OPTIONS[:tab_width],
}.tap do |hash|
# Escape ASCII control codes except \x9 == \t and \xA == \n.
(Array(0x00..0x8) + Array(0xB..0x1F)).each { |invalid| hash[invalid.chr] = ' ' }
@@ -178,19 +178,10 @@ module Encoders
@out = ''
end
- @tab_replacement = ' ' * options[:tab_width]
- @escape_cache = Hash.new do |cache, text|
- cache.clear if cache.size >= 100
-
- cache[text] =
- if text =~ /#{HTML_ESCAPE_PATTERN}/o
- text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| m == "\t" ? @tab_replacement : HTML_ESCAPE[m] }
- else
- text
- end
- end
@break_lines = (options[:break_lines] == true)
+ @escape_cache = make_escape_cache(options)
+
@opened = []
@last_opened = nil
@css = CSS.new options[:style]
@@ -285,6 +276,26 @@ module Encoders
options[:break_lines] = true if options[:line_numbers] == :inline
end
+ def make_escape_cache options
+ html_escape =
+ if options[:tab_width] == DEFAULT_OPTIONS[:tab_width]
+ HTML_ESCAPE
+ else
+ HTML_ESCAPE.merge("\t" => ' ' * options[:tab_width])
+ end
+
+ Hash.new do |cache, text|
+ cache.clear if cache.size >= 100
+
+ cache[text] =
+ if text =~ /#{HTML_ESCAPE_PATTERN}/o
+ text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| html_escape[m] }
+ else
+ text
+ end
+ end
+ end
+
def css_class_for_kinds kinds
TokenKinds[kinds.is_a?(Symbol) ? kinds : kinds.first]
end