diff options
author | Kornelius Kalnbach <murphy@rubychan.de> | 2016-06-13 01:07:56 +0200 |
---|---|---|
committer | Kornelius Kalnbach <murphy@rubychan.de> | 2016-06-13 01:07:56 +0200 |
commit | e01ccf7c5f1da22d2d131a84d36eeebdebe92f1c (patch) | |
tree | 0faa2550f16eb8899a1681a11a56dd8c7c5d07ab /lib/coderay | |
parent | 545398fac57b5949359298e1f258e1746adfa3e5 (diff) | |
parent | c999deb1e932b3290561a58923603eb87809c8df (diff) | |
download | coderay-e01ccf7c5f1da22d2d131a84d36eeebdebe92f1c.tar.gz |
Merge branch 'possible-speedups' into dsl
Diffstat (limited to 'lib/coderay')
-rw-r--r-- | lib/coderay/encoders/html.rb | 40 | ||||
-rw-r--r-- | lib/coderay/helpers/plugin_host.rb | 20 |
2 files changed, 17 insertions, 43 deletions
diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index 55b1291..0365022 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 @@ -127,21 +128,6 @@ module Encoders protected - def self.make_html_escape_hash - { - '&' => '&', - '>' => '>', - '<' => '<', - "\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] = ' ' } - 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,8 +166,6 @@ module Encoders @break_lines = (options[:break_lines] == true) - @escape_cache = make_escape_cache(options) - @opened = [] @last_opened = nil @css = CSS.new options[:style] @@ -220,7 +204,7 @@ module Encoders def text_token text, kind style = @span_for_kinds[@last_opened ? [kind, *@opened] : kind] - text = @escape_cache[text] if text.size <= 1 || text =~ /#{HTML_ESCAPE_PATTERN}/o + text = EscapeUtils.escape_html text, false text = break_lines(text, style) if @break_lines && (style || @opened.size > 0) && text.index("\n") if style @@ -276,26 +260,6 @@ 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] ? ' ' * options[:tab_width] : "\t") - 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 diff --git a/lib/coderay/helpers/plugin_host.rb b/lib/coderay/helpers/plugin_host.rb index e9bc17c..12ee29d 100644 --- a/lib/coderay/helpers/plugin_host.rb +++ b/lib/coderay/helpers/plugin_host.rb @@ -47,11 +47,21 @@ module CodeRay # Example: # yaml_plugin = MyPluginHost[:yaml] def [] id, *args, &blk - plugin = validate_id(id) - begin - plugin = plugin_hash.[](plugin, *args, &blk) - end while plugin.is_a? String - plugin + if !args.empty? || blk + plugin = validate_id(id) + begin + plugin = plugin_hash.[](plugin, *args, &blk) + end while plugin.is_a? String + plugin + else + (@cache ||= Hash.new do |cache, key| + plugin = validate_id(key) + begin + plugin = plugin_hash.[](plugin) + end while plugin.is_a? String + cache[key] = plugin + end)[id] + end end alias load [] |