erubis-cached-text 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ <tbody>
2
+ <%
3
+ n = 0
4
+ for item in list
5
+ n += 1
6
+ %>
7
+ <tr class="<%= n % 2 == 0 ? 'even' : 'odd' %>">
8
+ <td style="text-align: center"><%= n %></td>
9
+ <td>
10
+ <a href="/stocks/<%= item['symbol'] %>"><%= item['symbol'] %></a>
11
+ </td>
12
+ <td>
13
+ <a href="<%= item['url'] %>"><%= item['name'] %></a>
14
+ </td>
15
+ <td>
16
+ <strong><%= item['price'] %></strong>
17
+ </td>
18
+ <% if item['change'] < 0.0 %>
19
+ <td class="minus"><%= item['change'] %></td>
20
+ <td class="minus"><%= item['ratio'] %></td>
21
+ <% else %>
22
+ <td><%= item['change'] %></td>
23
+ <td><%= item['ratio'] %></td>
24
+ <% end %>
25
+ </tr>
26
+ <%
27
+ end
28
+ %>
29
+ </tbody>
@@ -0,0 +1,45 @@
1
+ require 'erubis'
2
+ require 'digest/sha1'
3
+
4
+ module Erubis
5
+ class ErubyCachedText < Basic::Engine
6
+ include RubyEvaluator
7
+ include RubyGenerator
8
+
9
+ @minimum_string_size = nil
10
+ class << self
11
+ attr_accessor :minimum_string_size
12
+ end
13
+
14
+ TEXT_CACHE = {}
15
+
16
+ def self.clear_cache
17
+ TEXT_CACHE.clear
18
+ end
19
+
20
+ # @overridden
21
+ def add_text(src, text)
22
+ min_string_size = self.class.minimum_string_size
23
+ if min_string_size && min_string_size > 0 && text.size < min_string_size
24
+ return super
25
+ end
26
+
27
+ unless text.empty?
28
+ escaped = escape_text(text)
29
+ digest = Digest::SHA1.hexdigest(escaped)
30
+ key = :"#{digest}"
31
+ unless TEXT_CACHE.has_key?(key)
32
+ TEXT_CACHE[key] = escaped.freeze
33
+ end
34
+ src << " #{@bufvar} << " << generate_lookup_text_code(digest) << ";"
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def generate_lookup_text_code(digest)
41
+ "Erubis::ErubyCachedText::TEXT_CACHE[:#{digest.inspect}]"
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,119 @@
1
+ require 'erubis'
2
+ require 'digest/sha1'
3
+ require 'action_view/template/handlers/erb'
4
+
5
+ module ActionView
6
+ class Template
7
+ module Handlers
8
+ class ErubyCachedText < ::Erubis::Eruby
9
+
10
+ @minimum_string_size = nil
11
+ class << self
12
+ attr_accessor :minimum_string_size
13
+ end
14
+
15
+ TEXT_CACHE = {}
16
+
17
+ def self.clear_cache
18
+ TEXT_CACHE.clear
19
+ end
20
+
21
+ def add_preamble(src)
22
+ @newline_pending = 0
23
+ src << "@output_buffer = output_buffer || ActionView::OutputBuffer.new;"
24
+ end
25
+
26
+ def add_text(src, text)
27
+ return if text.empty?
28
+ if text == "\n"
29
+ @newline_pending += 1
30
+ return
31
+ end
32
+
33
+ min_string_size = self.class.minimum_string_size
34
+
35
+ # don't cache the string in memory
36
+ if min_string_size && min_string_size > 0 && text.size < min_string_size
37
+ src << "@output_buffer.safe_append='"
38
+ src << "\n" * @newline_pending if @newline_pending > 0
39
+ src << escape_text(text)
40
+ src << "'.freeze;"
41
+ @newline_pending = 0
42
+ # cache the string in memory
43
+ else
44
+ if @newline_pending > 0
45
+ nls = "\n" * @newline_pending
46
+ text = nls << text
47
+ end
48
+ escaped = escape_text(text)
49
+ digest = Digest::SHA1.hexdigest(escaped)
50
+ key = :"#{digest}"
51
+ unless TEXT_CACHE.has_key?(key)
52
+ TEXT_CACHE[key] = escaped.freeze
53
+ end
54
+ src << "@output_buffer.safe_append="
55
+ src << generate_lookup_text_code(digest) << ";"
56
+ end
57
+ end
58
+
59
+ # Erubis toggles <%= and <%== behavior when escaping is enabled.
60
+ # We override to always treat <%== as escaped.
61
+ def add_expr(src, code, indicator)
62
+ case indicator
63
+ when '=='
64
+ add_expr_escaped(src, code)
65
+ else
66
+ super
67
+ end
68
+ end
69
+
70
+ BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/
71
+
72
+ def add_expr_literal(src, code)
73
+ flush_newline_if_pending(src)
74
+ if code =~ BLOCK_EXPR
75
+ src << '@output_buffer.append= ' << code
76
+ else
77
+ src << '@output_buffer.append=(' << code << ');'
78
+ end
79
+ end
80
+
81
+ def add_expr_escaped(src, code)
82
+ flush_newline_if_pending(src)
83
+ if code =~ BLOCK_EXPR
84
+ src << "@output_buffer.safe_append= " << code
85
+ else
86
+ src << "@output_buffer.safe_append=(" << code << ");"
87
+ end
88
+ end
89
+
90
+ def add_stmt(src, code)
91
+ flush_newline_if_pending(src)
92
+ super
93
+ end
94
+
95
+ def add_postamble(src)
96
+ flush_newline_if_pending(src)
97
+ src << '@output_buffer.to_s'
98
+ end
99
+
100
+ def flush_newline_if_pending(src)
101
+ if @newline_pending > 0
102
+ src << "@output_buffer.safe_append='#{"\n" * @newline_pending}'.freeze;"
103
+ @newline_pending = 0
104
+ end
105
+ end
106
+
107
+ private
108
+
109
+ def generate_lookup_text_code(digest)
110
+ "ActionView::Template::Handlers::ErubyCachedText::TEXT_CACHE[:#{digest.inspect}]"
111
+ end
112
+
113
+ end
114
+ end
115
+
116
+ ERB.erb_implementation = ErubyCachedText
117
+
118
+ end
119
+ end
@@ -0,0 +1 @@
1
+ require 'erubis/engine/eruby_cached_text'
@@ -0,0 +1 @@
1
+ require File.expand_path('../erubis/engine/rails/eruby_cached_text', __FILE__)
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: erubis-cached-text
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Luke Gruber
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: erubis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.0
27
+ description:
28
+ email: luke.gru@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - LICENSE
36
+ - README.md
37
+ - VERSION
38
+ - benchmark/MIT-LICENSE
39
+ - benchmark/Makefile
40
+ - benchmark/bench.rb
41
+ - benchmark/bench_context.yaml
42
+ - benchmark/bench_erb.rhtml
43
+ - benchmark/bench_erb.rhtml.cache
44
+ - benchmark/bench_erubis.rhtml
45
+ - benchmark/bench_erubis.rhtml.eruby.cache
46
+ - benchmark/bench_erubis.rhtml.erubycachedtext.cache
47
+ - benchmark/bench_erubis.rhtml.fasteruby.cache
48
+ - benchmark/bench_eruby.rhtml
49
+ - benchmark/templates/_footer.html
50
+ - benchmark/templates/_header.html
51
+ - benchmark/templates/bench_erb.rhtml
52
+ - benchmark/templates/bench_erubis.rhtml
53
+ - benchmark/templates/bench_eruby.rhtml
54
+ - lib/erubis/engine/eruby_cached_text.rb
55
+ - lib/erubis/engine/rails/eruby_cached_text.rb
56
+ - lib/erubis_cached_text.rb
57
+ - lib/erubis_cached_text_rails.rb
58
+ homepage: https://github.com/luke-gru/erubis-cached-text
59
+ licenses: []
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.2.0
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Eruby Engine that caches text strings in memory
81
+ test_files: []