From 370c6d3c3257da94f68a520467751e078bfdd419 Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Fri, 20 Sep 2013 17:45:14 +0200 Subject: cleanup changes into make_escape_cache --- lib/coderay/encoders/html.rb | 43 +++++++++++++++++++++++++++---------------- 1 file 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 { - '&' => '&', - '"' => '"', - '>' => '>', - '<' => '<', - # "\t" => will be set to ' ' * options[:tab_width] during setup + '&' => '&', + '"' => '"', + '>' => '>', + '<' => '<', + "\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 -- cgit v1.2.1