Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Escape Utils? #191

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions coderay.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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", "~> 1.2"

s.rubyforge_project = s.name
s.rdoc_options = '-SNw2', "-m#{readme_file}", '-t CodeRay Documentation'
s.extra_rdoc_files = readme_file
Expand Down
27 changes: 9 additions & 18 deletions lib/coderay/encoders/html.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'set'
require 'escape_utils'

module CodeRay
module Encoders
Expand Down Expand Up @@ -127,22 +128,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
Expand Down Expand Up @@ -181,7 +166,7 @@ def setup options

@break_lines = (options[:break_lines] == true)

@HTML_ESCAPE = HTML_ESCAPE.merge("\t" => options[:tab_width] ? ' ' * options[:tab_width] : "\t")
@expand_tab = options[:tab_width] ? ' ' * options[:tab_width] : "\t"

@opened = []
@last_opened = nil
Expand Down Expand Up @@ -221,7 +206,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
Expand Down