From 90cf0323db24d5fe2e326f9a413d85a49402f5a9 Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Sat, 31 Aug 2013 16:09:34 +0200 Subject: cache escaped tokens, reduce HTML enc setup time --- lib/coderay/encoders/html.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'lib/coderay/encoders/html.rb') diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index d2ebb5a..0f38726 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -178,10 +178,19 @@ 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) - @HTML_ESCAPE = HTML_ESCAPE.merge("\t" => ' ' * options[:tab_width]) - @opened = [] @last_opened = nil @css = CSS.new options[:style] @@ -197,7 +206,7 @@ module Encoders @last_opened = nil end - if @out.respond_to? :to_str + if options[:wrap] || options[:line_numbers] @out.extend Output @out.css = @css if options[:line_numbers] @@ -220,7 +229,7 @@ module Encoders def text_token text, kind style = @span_for_kinds[@last_opened ? [kind, *@opened] : kind] - text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } if text =~ /#{HTML_ESCAPE_PATTERN}/o + text = @escape_cache[text] if text.size <= 1 || text =~ /#{HTML_ESCAPE_PATTERN}/o text = break_lines(text, style) if @break_lines && (style || @opened.size > 0) && text.index("\n") if style -- cgit v1.2.1 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(-) (limited to 'lib/coderay/encoders/html.rb') 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 From bc4854fa426a39c2bd51b2fd1cfa0322a5a9a412 Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Wed, 20 Nov 2013 15:54:41 +0100 Subject: don't escape " in HTML --- lib/coderay/encoders/html.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/coderay/encoders/html.rb') diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index c21b19d..7e83eb8 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -129,7 +129,6 @@ module Encoders def self.make_html_escape_hash { '&' => '&', - '"' => '"', '>' => '>', '<' => '<', "\t" => ' ' * DEFAULT_OPTIONS[:tab_width], @@ -140,7 +139,7 @@ module Encoders end HTML_ESCAPE = make_html_escape_hash - HTML_ESCAPE_PATTERN = /[\t"&><\0-\x8\xB-\x1F]/ + HTML_ESCAPE_PATTERN = /[\t&><\0-\x8\xB-\x1F]/ TOKEN_KIND_TO_INFO = Hash.new do |h, kind| h[kind] = kind.to_s.gsub(/_/, ' ').gsub(/\b\w/) { $&.capitalize } -- cgit v1.2.1