summaryrefslogtreecommitdiff
path: root/lib/coderay
diff options
context:
space:
mode:
Diffstat (limited to 'lib/coderay')
-rw-r--r--lib/coderay/encoders/html.rb27
1 files changed, 9 insertions, 18 deletions
diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb
index 20f2409..ad26aa2 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 @@ module Encoders
protected
- def self.make_html_escape_hash
- {
- '&' => '&',
- '"' => '"',
- '>' => '>',
- '<' => '&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 @@ module Encoders
@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 @@ 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 = 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