From 00eeb882c592fc2ee883630357dc082e5c7b2ad7 Mon Sep 17 00:00:00 2001
From: Kornelius Kalnbach <murphy@rubychan.de>
Date: Sun, 14 Jul 2013 02:25:39 +0200
Subject: [PATCH 1/5] use EscapeUtils to escape HTML

---
 coderay.gemspec              |  2 ++
 lib/coderay/encoders/html.rb | 27 +++++++++------------------
 2 files changed, 11 insertions(+), 18 deletions(-)

diff --git a/coderay.gemspec b/coderay.gemspec
index 328b94c1..5a5ef609 100644
--- a/coderay.gemspec
+++ b/coderay.gemspec
@@ -29,6 +29,8 @@ Gem::Specification.new do |s|
   s.executables   = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
   s.require_paths = ['lib']
   
+  s.add_dependency "escape_utils", "~> 0.3"
+  
   s.rubyforge_project = s.name
   s.rdoc_options      = '-SNw2', "-m#{readme_file}", '-t CodeRay Documentation'
   s.extra_rdoc_files  = readme_file
diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb
index 20f24095..ad26aa2c 100644
--- a/lib/coderay/encoders/html.rb
+++ b/lib/coderay/encoders/html.rb
@@ -1,4 +1,5 @@
 require 'set'
+require 'escape_utils'
 
 module CodeRay
 module Encoders
@@ -126,22 +127,6 @@ class HTML < Encoder
     
   protected
     
-    def self.make_html_escape_hash
-      {
-        '&' => '&amp;',
-        '"' => '&quot;',
-        '>' => '&gt;',
-        '<' => '&lt;',
-        # "\t" => will be set to ' ' * options[:tab_width] during setup
-      }.tap do |hash|
-        # Escape ASCII control codes except \x9 == \t and \xA == \n.
-        (Array(0x00..0x8) + Array(0xB..0x1F)).each { |invalid| hash[invalid.chr] = ' ' }
-      end
-    end
-    
-    HTML_ESCAPE = make_html_escape_hash
-    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 }
     end
@@ -180,7 +165,7 @@ def setup options
       
       @break_lines = (options[:break_lines] == true)
       
-      @HTML_ESCAPE = HTML_ESCAPE.merge("\t" => ' ' * options[:tab_width])
+      @expand_tab = ' ' * options[:tab_width]
       
       @opened = []
       @last_opened = nil
@@ -218,7 +203,13 @@ def finish options
     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 = EscapeUtils.escape_html(text)
+      if text.index(/[\0-\t\xB-\x1F]/)
+        # Escape ASCII control codes except \x9 == \t and \xA == \n.
+        text.tr!("\0-\x8\xB-\x1F", ' ') if text.index(/[\0-\x8\xB-\x1F]/)
+        text.gsub!("\t", @expand_tab)   if text.index("\t")
+      end
+      
       text = break_lines(text, style) if @break_lines && (style || @opened.size > 0) && text.index("\n")
       
       if style

From bb5cacf51d3c7a9bcd41c78571c12c5ba281c33f Mon Sep 17 00:00:00 2001
From: Kornelius Kalnbach <murphy@rubychan.de>
Date: Sun, 14 Jul 2013 02:27:53 +0200
Subject: [PATCH 2/5] remove control codes escaping?

---
 lib/coderay/encoders/html.rb | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb
index ad26aa2c..9489a911 100644
--- a/lib/coderay/encoders/html.rb
+++ b/lib/coderay/encoders/html.rb
@@ -204,11 +204,11 @@ def text_token text, kind
       style = @span_for_kinds[@last_opened ? [kind, *@opened] : kind]
       
       text = EscapeUtils.escape_html(text)
-      if text.index(/[\0-\t\xB-\x1F]/)
-        # Escape ASCII control codes except \x9 == \t and \xA == \n.
-        text.tr!("\0-\x8\xB-\x1F", ' ') if text.index(/[\0-\x8\xB-\x1F]/)
+      # if text.index(/[\0-\t\xB-\x1F]/)
+      #   # Escape ASCII control codes except \x9 == \t and \xA == \n.
+      #   text.tr!("\0-\x8\xB-\x1F", ' ') if text.index(/[\0-\x8\xB-\x1F]/)
         text.gsub!("\t", @expand_tab)   if text.index("\t")
-      end
+      # end
       
       text = break_lines(text, style) if @break_lines && (style || @opened.size > 0) && text.index("\n")
       

From 4e13ac40b21a9cb272fc7475a9976e1e0c0d8b11 Mon Sep 17 00:00:00 2001
From: Kornelius Kalnbach <murphy@rubychan.de>
Date: Sun, 14 Jul 2013 02:37:50 +0200
Subject: [PATCH 3/5] imagine escape_html does all we need

---
 lib/coderay/encoders/html.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb
index 9489a911..486d561c 100644
--- a/lib/coderay/encoders/html.rb
+++ b/lib/coderay/encoders/html.rb
@@ -207,7 +207,7 @@ def text_token text, kind
       # if text.index(/[\0-\t\xB-\x1F]/)
       #   # Escape ASCII control codes except \x9 == \t and \xA == \n.
       #   text.tr!("\0-\x8\xB-\x1F", ' ') if text.index(/[\0-\x8\xB-\x1F]/)
-        text.gsub!("\t", @expand_tab)   if text.index("\t")
+        # text.gsub!("\t", @expand_tab)   if text.index("\t")
       # end
       
       text = break_lines(text, style) if @break_lines && (style || @opened.size > 0) && text.index("\n")

From d79c6ba2008bff7b65029bc50717ca679c43fb5f Mon Sep 17 00:00:00 2001
From: Kornelius Kalnbach <murphy@rubychan.de>
Date: Fri, 30 Aug 2013 17:19:55 +0200
Subject: [PATCH 4/5] enable necessary replacements in HTML encoder

---
 lib/coderay/encoders/html.rb | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb
index 37dfa813..629950c1 100644
--- a/lib/coderay/encoders/html.rb
+++ b/lib/coderay/encoders/html.rb
@@ -206,11 +206,11 @@ def text_token text, kind
       style = @span_for_kinds[@last_opened ? [kind, *@opened] : kind]
       
       text = EscapeUtils.escape_html(text)
-      # if text.index(/[\0-\t\xB-\x1F]/)
-      #   # Escape ASCII control codes except \x9 == \t and \xA == \n.
-      #   text.tr!("\0-\x8\xB-\x1F", ' ') if text.index(/[\0-\x8\xB-\x1F]/)
-        # text.gsub!("\t", @expand_tab)   if text.index("\t")
-      # end
+      if text.index(/[\0-\t\xB-\x1F]/)
+        # Escape ASCII control codes except \x9 == \t and \xA == \n.
+        text.tr!("\0-\x8\xB-\x1F", ' ') if text.index(/[\0-\x8\xB-\x1F]/)
+        text.gsub!("\t", @expand_tab)   if text.index("\t")
+      end
       
       text = break_lines(text, style) if @break_lines && (style || @opened.size > 0) && text.index("\n")
       

From 2d96fc48eee1bc24d4443062c05ec10939274859 Mon Sep 17 00:00:00 2001
From: Kornelius Kalnbach <murphy@rubychan.de>
Date: Sat, 13 Feb 2016 16:07:46 +0100
Subject: [PATCH 5/5] update escape_utils

---
 coderay.gemspec | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/coderay.gemspec b/coderay.gemspec
index 5a5ef609..3879543c 100644
--- a/coderay.gemspec
+++ b/coderay.gemspec
@@ -29,7 +29,7 @@ Gem::Specification.new do |s|
   s.executables   = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
   s.require_paths = ['lib']
   
-  s.add_dependency "escape_utils", "~> 0.3"
+  s.add_dependency "escape_utils", "~> 1.2"
   
   s.rubyforge_project = s.name
   s.rdoc_options      = '-SNw2', "-m#{readme_file}", '-t CodeRay Documentation'