temple 0.6.4 → 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 62248df85fb8bb676f19fbf68f2b91f5fd2ee88b
4
- data.tar.gz: aad55078fc4f2d236b6573cb3a1f7dc1311c4a41
3
+ metadata.gz: ef295d25d34ef11abf1640d7240eedb8643e8107
4
+ data.tar.gz: a721340521044f953fec55f2b75b0d467b2bdab4
5
5
  SHA512:
6
- metadata.gz: 359fd5b972b0a423f0f28d35f806e635126900ec9a17675d69d8e0a8d8aa502070e298871d68a41b6d041480cb763e2af1026e74b3f306b7c67cc57e67102017
7
- data.tar.gz: 51706ada25d75c7de46cbc060d0930678efc6f98382eba2d60f65c868c59b7e7c93bcbcf7c98fba59dc5132825d37a289e053fbf12ca6c90bc53f69c59b3bfae
6
+ metadata.gz: e99dbeb234856873da69adb1460a123876f219e7c34d3c12bba379ee86fd60017cd56d17b5e5da6d2eff7d3e825135cfa57002232dc3e50d3676c59745b85dc9
7
+ data.tar.gz: 22c4e6d503c7a3272ba39a153b3e7cf0ea92cb0a4937c5e6e05121ac5fe1efdd3d0ca7e3650d4a6633358542bcf4322ea27e0638e119f991db44bf58cf779810
data/CHANGES CHANGED
@@ -1,3 +1,10 @@
1
+ 0.6.5
2
+
3
+ * Added Filters::CodeMerger
4
+ * Added Filters::Encoding
5
+ * Added Filters::RemoveBOM
6
+ * Added Generators::ERB
7
+
1
8
  0.6.4
2
9
 
3
10
  * Check for ActionView instead of Rails (#72)
data/lib/temple.rb CHANGED
@@ -3,8 +3,7 @@ require 'temple/version'
3
3
  module Temple
4
4
  autoload :InvalidExpression, 'temple/exceptions'
5
5
  autoload :FilterError, 'temple/exceptions'
6
- autoload :Generator, 'temple/generators'
7
- autoload :Generators, 'temple/generators'
6
+ autoload :Generator, 'temple/generator'
8
7
  autoload :Parser, 'temple/parser'
9
8
  autoload :Engine, 'temple/engine'
10
9
  autoload :Utils, 'temple/utils'
@@ -32,7 +31,16 @@ module Temple
32
31
  autoload :Template, 'temple/erb/template'
33
32
  end
34
33
 
34
+ module Generators
35
+ autoload :ERB, 'temple/generators/erb'
36
+ autoload :Array, 'temple/generators/array'
37
+ autoload :ArrayBuffer, 'temple/generators/array_buffer'
38
+ autoload :StringBuffer, 'temple/generators/string_buffer'
39
+ autoload :RailsOutputBuffer, 'temple/generators/rails_output_buffer'
40
+ end
41
+
35
42
  module Filters
43
+ autoload :CodeMerger, 'temple/filters/code_merger'
36
44
  autoload :ControlFlow, 'temple/filters/control_flow'
37
45
  autoload :MultiFlattener, 'temple/filters/multi_flattener'
38
46
  autoload :StaticMerger, 'temple/filters/static_merger'
@@ -40,6 +48,8 @@ module Temple
40
48
  autoload :Escapable, 'temple/filters/escapable'
41
49
  autoload :Eraser, 'temple/filters/eraser'
42
50
  autoload :Validator, 'temple/filters/validator'
51
+ autoload :Encoding, 'temple/filters/encoding'
52
+ autoload :RemoveBOM, 'temple/filters/remove_bom'
43
53
  end
44
54
 
45
55
  module HTML
@@ -0,0 +1,30 @@
1
+ module Temple
2
+ module Filters
3
+ # @api public
4
+ class CodeMerger < Filter
5
+ def on_multi(*exps)
6
+ result = [:multi]
7
+ code = nil
8
+
9
+ exps.each do |exp|
10
+ if exp.first == :code
11
+ if code
12
+ code << '; ' unless code =~ /\n\Z/
13
+ code << exp.last
14
+ else
15
+ code = exp.last.dup
16
+ result << [:code, code]
17
+ end
18
+ elsif code && exp.first == :newline
19
+ code << "\n"
20
+ else
21
+ result << compile(exp)
22
+ code = nil
23
+ end
24
+ end
25
+
26
+ result.size == 2 ? result[1] : result
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,24 @@
1
+ module Temple
2
+ module Filters
3
+ # Try to encode input string
4
+ #
5
+ # @api public
6
+ class Encoding < Parser
7
+ define_options :encoding
8
+
9
+ def call(s)
10
+ if options[:encoding] && s.respond_to?(:encoding)
11
+ old_enc = s.encoding
12
+ s = s.dup if s.frozen?
13
+ s.force_encoding(options[:encoding])
14
+ # Fall back to old encoding if new encoding is invalid
15
+ unless s.valid_encoding?
16
+ s.force_encoding(old_enc)
17
+ s.force_encoding(::Encoding::BINARY) unless s.valid_encoding?
18
+ end
19
+ end
20
+ s
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ module Temple
2
+ module Filters
3
+ # Remove BOM from input string
4
+ #
5
+ # @api public
6
+ class RemoveBOM < Parser
7
+ def call(s)
8
+ if s.respond_to?(:encoding)
9
+ if s.encoding.name =~ /^UTF-(8|16|32)(BE|LE)?/
10
+ s.gsub(Regexp.new("\\A\uFEFF".encode(s.encoding.name)), '')
11
+ else
12
+ s
13
+ end
14
+ else
15
+ s.gsub(/\A\xEF\xBB\xBF/, '')
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,62 @@
1
+ module Temple
2
+ # Abstract generator base class
3
+ # Generators should inherit this class and
4
+ # compile the Core Abstraction to ruby code.
5
+ #
6
+ # @api public
7
+ class Generator
8
+ include Mixins::CompiledDispatcher
9
+ include Mixins::Options
10
+
11
+ define_options :capture_generator => 'StringBuffer',
12
+ :buffer => '_buf'
13
+
14
+ def call(exp)
15
+ [preamble, compile(exp), postamble].join('; ')
16
+ end
17
+
18
+ def on(*exp)
19
+ raise InvalidExpression, "Generator supports only core expressions - found #{exp.inspect}"
20
+ end
21
+
22
+ def on_multi(*exp)
23
+ exp.map {|e| compile(e) }.join('; ')
24
+ end
25
+
26
+ def on_newline
27
+ "\n"
28
+ end
29
+
30
+ def on_capture(name, exp)
31
+ capture_generator.new(:buffer => name).call(exp)
32
+ end
33
+
34
+ def on_static(text)
35
+ concat(text.inspect)
36
+ end
37
+
38
+ def on_dynamic(code)
39
+ concat(code)
40
+ end
41
+
42
+ def on_code(code)
43
+ code
44
+ end
45
+
46
+ protected
47
+
48
+ def buffer
49
+ options[:buffer]
50
+ end
51
+
52
+ def capture_generator
53
+ @capture_generator ||= Class === options[:capture_generator] ?
54
+ options[:capture_generator] :
55
+ Generators.const_get(options[:capture_generator])
56
+ end
57
+
58
+ def concat(str)
59
+ "#{buffer} << (#{str})"
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,21 @@
1
+ module Temple
2
+ module Generators
3
+ # Implements an array buffer.
4
+ #
5
+ # _buf = []
6
+ # _buf << "static"
7
+ # _buf << dynamic
8
+ # _buf
9
+ #
10
+ # @api public
11
+ class Array < Generator
12
+ def preamble
13
+ "#{buffer} = []"
14
+ end
15
+
16
+ def postamble
17
+ buffer
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,28 @@
1
+ module Temple
2
+ module Generators
3
+ # Just like Array, but calls #join on the array.
4
+ #
5
+ # _buf = []
6
+ # _buf << "static"
7
+ # _buf << dynamic
8
+ # _buf.join
9
+ #
10
+ # @api public
11
+ class ArrayBuffer < Array
12
+ def call(exp)
13
+ case exp.first
14
+ when :static
15
+ "#{buffer} = #{exp.last.inspect}"
16
+ when :dynamic
17
+ "#{buffer} = (#{exp.last}).to_s"
18
+ else
19
+ super
20
+ end
21
+ end
22
+
23
+ def postamble
24
+ "#{buffer} = #{buffer}.join"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,36 @@
1
+ module Temple
2
+ module Generators
3
+ # Implements an ERB generator.
4
+ #
5
+ # @api public
6
+ class ERB < Generator
7
+ def call(exp)
8
+ compile(exp)
9
+ end
10
+
11
+ def on_multi(*exp)
12
+ exp.map {|e| compile(e) }.join
13
+ end
14
+
15
+ def on_capture(name, exp)
16
+ on_code(super)
17
+ end
18
+
19
+ def on_static(text)
20
+ text
21
+ end
22
+
23
+ def on_newline
24
+ "<%\n%>"
25
+ end
26
+
27
+ def on_dynamic(code)
28
+ "<%= #{code} %>"
29
+ end
30
+
31
+ def on_code(code)
32
+ "<% #{code} %>"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,35 @@
1
+ module Temple
2
+ module Generators
3
+ # Implements a rails output buffer.
4
+ #
5
+ # @output_buffer = ActiveSupport::SafeBuffer
6
+ # @output_buffer.safe_concat "static"
7
+ # @output_buffer.safe_concat dynamic.to_s
8
+ # @output_buffer
9
+ #
10
+ # @api public
11
+ class RailsOutputBuffer < StringBuffer
12
+ define_options :streaming,
13
+ :buffer_class => 'ActiveSupport::SafeBuffer',
14
+ :buffer => '@output_buffer',
15
+ # output_buffer is needed for Rails 3.1 Streaming support
16
+ :capture_generator => RailsOutputBuffer
17
+
18
+ def call(exp)
19
+ [preamble, compile(exp), postamble].join('; ')
20
+ end
21
+
22
+ def preamble
23
+ if options[:streaming] && options[:buffer] == '@output_buffer'
24
+ "#{buffer} = output_buffer || #{options[:buffer_class]}.new"
25
+ else
26
+ "#{buffer} = #{options[:buffer_class]}.new"
27
+ end
28
+ end
29
+
30
+ def concat(str)
31
+ "#{buffer}.safe_concat((#{str}))"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,25 @@
1
+ module Temple
2
+ module Generators
3
+ # Implements a string buffer.
4
+ #
5
+ # _buf = ''
6
+ # _buf << "static"
7
+ # _buf << dynamic.to_s
8
+ # _buf
9
+ #
10
+ # @api public
11
+ class StringBuffer < ArrayBuffer
12
+ def preamble
13
+ "#{buffer} = ''"
14
+ end
15
+
16
+ def postamble
17
+ buffer
18
+ end
19
+
20
+ def on_dynamic(code)
21
+ concat("(#{code}).to_s")
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Temple
2
- VERSION = '0.6.4'
2
+ VERSION = '0.6.5'
3
3
  end
@@ -0,0 +1,38 @@
1
+ require 'helper'
2
+
3
+ describe Temple::Filters::CodeMerger do
4
+ before do
5
+ @filter = Temple::Filters::CodeMerger.new
6
+ end
7
+
8
+ it 'should merge serveral codes' do
9
+ @filter.call([:multi,
10
+ [:code, "a"],
11
+ [:code, "b"],
12
+ [:code, "c"]
13
+ ]).should.equal [:code, "a; b; c"]
14
+ end
15
+
16
+ it 'should merge serveral codes around static' do
17
+ @filter.call([:multi,
18
+ [:code, "a"],
19
+ [:code, "b"],
20
+ [:static, "123"],
21
+ [:code, "a"],
22
+ [:code, "b"]
23
+ ]).should.equal [:multi,
24
+ [:code, "a; b"],
25
+ [:static, "123"],
26
+ [:code, "a; b"]
27
+ ]
28
+ end
29
+
30
+ it 'should merge serveral codes with newlines' do
31
+ @filter.call([:multi,
32
+ [:code, "a"],
33
+ [:code, "b"],
34
+ [:newline],
35
+ [:code, "c"]
36
+ ]).should.equal [:code, "a; b\nc"]
37
+ end
38
+ end
@@ -111,6 +111,18 @@ describe Temple::Generators::StringBuffer do
111
111
  end
112
112
  end
113
113
 
114
+ describe Temple::Generators::ERB do
115
+ it 'should compile simple expressions' do
116
+ gen = Temple::Generators::ERB.new
117
+ gen.call([:static, 'test']).should.equal 'test'
118
+ gen.call([:dynamic, 'test']).should.equal '<%= test %>'
119
+ gen.call([:code, 'test']).should.equal '<% test %>'
120
+
121
+ gen.call([:multi, [:static, 'a'], [:static, 'b']]).should.equal 'ab'
122
+ gen.call([:multi, [:static, 'a'], [:dynamic, 'b']]).should.equal 'a<%= b %>'
123
+ end
124
+ end
125
+
114
126
  describe Temple::Generators::RailsOutputBuffer do
115
127
  it 'should compile simple expressions' do
116
128
  gen = Temple::Generators::RailsOutputBuffer.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: temple
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Magnus Holm
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-16 00:00:00.000000000 Z
12
+ date: 2013-05-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: tilt
@@ -79,14 +79,22 @@ files:
79
79
  - lib/temple/erb/trimming.rb
80
80
  - lib/temple/exceptions.rb
81
81
  - lib/temple/filter.rb
82
+ - lib/temple/filters/code_merger.rb
82
83
  - lib/temple/filters/control_flow.rb
83
84
  - lib/temple/filters/dynamic_inliner.rb
85
+ - lib/temple/filters/encoding.rb
84
86
  - lib/temple/filters/eraser.rb
85
87
  - lib/temple/filters/escapable.rb
86
88
  - lib/temple/filters/multi_flattener.rb
89
+ - lib/temple/filters/remove_bom.rb
87
90
  - lib/temple/filters/static_merger.rb
88
91
  - lib/temple/filters/validator.rb
89
- - lib/temple/generators.rb
92
+ - lib/temple/generator.rb
93
+ - lib/temple/generators/array.rb
94
+ - lib/temple/generators/array_buffer.rb
95
+ - lib/temple/generators/erb.rb
96
+ - lib/temple/generators/rails_output_buffer.rb
97
+ - lib/temple/generators/string_buffer.rb
90
98
  - lib/temple/grammar.rb
91
99
  - lib/temple/hash.rb
92
100
  - lib/temple/html/attribute_merger.rb
@@ -108,6 +116,7 @@ files:
108
116
  - lib/temple/utils.rb
109
117
  - lib/temple/version.rb
110
118
  - temple.gemspec
119
+ - test/filters/test_code_merger.rb
111
120
  - test/filters/test_control_flow.rb
112
121
  - test/filters/test_dynamic_inliner.rb
113
122
  - test/filters/test_eraser.rb
@@ -153,6 +162,7 @@ signing_key:
153
162
  specification_version: 4
154
163
  summary: Template compilation framework in Ruby
155
164
  test_files:
165
+ - test/filters/test_code_merger.rb
156
166
  - test/filters/test_control_flow.rb
157
167
  - test/filters/test_dynamic_inliner.rb
158
168
  - test/filters/test_eraser.rb
@@ -1,163 +0,0 @@
1
- module Temple
2
- # Exception raised if invalid temple expression is found
3
- #
4
- # @api public
5
- class InvalidExpression < RuntimeError
6
- end
7
-
8
- # Abstract generator base class
9
- # Generators should inherit this class and
10
- # compile the Core Abstraction to ruby code.
11
- #
12
- # @api public
13
- class Generator
14
- include Mixins::CompiledDispatcher
15
- include Mixins::Options
16
-
17
- define_options :capture_generator,
18
- :buffer => '_buf'
19
-
20
- def call(exp)
21
- [preamble, compile(exp), postamble].join('; ')
22
- end
23
-
24
- def on(*exp)
25
- raise InvalidExpression, "Generator supports only core expressions - found #{exp.inspect}"
26
- end
27
-
28
- def on_multi(*exp)
29
- exp.map {|e| compile(e) }.join('; ')
30
- end
31
-
32
- def on_newline
33
- "\n"
34
- end
35
-
36
- def on_capture(name, exp)
37
- options[:capture_generator].new(:buffer => name).call(exp)
38
- end
39
-
40
- def on_static(text)
41
- concat(text.inspect)
42
- end
43
-
44
- def on_dynamic(code)
45
- concat(code)
46
- end
47
-
48
- def on_code(code)
49
- code
50
- end
51
-
52
- protected
53
-
54
- def buffer
55
- options[:buffer]
56
- end
57
-
58
- def concat(str)
59
- "#{buffer} << (#{str})"
60
- end
61
- end
62
-
63
- module Generators
64
- # Implements an array buffer.
65
- #
66
- # _buf = []
67
- # _buf << "static"
68
- # _buf << dynamic
69
- # _buf
70
- #
71
- # @api public
72
- class Array < Generator
73
- def preamble
74
- "#{buffer} = []"
75
- end
76
-
77
- def postamble
78
- buffer
79
- end
80
- end
81
-
82
- # Just like Array, but calls #join on the array.
83
- #
84
- # _buf = []
85
- # _buf << "static"
86
- # _buf << dynamic
87
- # _buf.join
88
- #
89
- # @api public
90
- class ArrayBuffer < Array
91
- def call(exp)
92
- case exp.first
93
- when :static
94
- "#{buffer} = #{exp.last.inspect}"
95
- when :dynamic
96
- "#{buffer} = (#{exp.last}).to_s"
97
- else
98
- super
99
- end
100
- end
101
-
102
- def postamble
103
- "#{buffer} = #{buffer}.join"
104
- end
105
- end
106
-
107
- # Implements a string buffer.
108
- #
109
- # _buf = ''
110
- # _buf << "static"
111
- # _buf << dynamic.to_s
112
- # _buf
113
- #
114
- # @api public
115
- class StringBuffer < ArrayBuffer
116
- def preamble
117
- "#{buffer} = ''"
118
- end
119
-
120
- def postamble
121
- buffer
122
- end
123
-
124
- def on_dynamic(code)
125
- concat("(#{code}).to_s")
126
- end
127
- end
128
-
129
- # Implements a rails output buffer.
130
- #
131
- # @output_buffer = ActiveSupport::SafeBuffer
132
- # @output_buffer.safe_concat "static"
133
- # @output_buffer.safe_concat dynamic.to_s
134
- # @output_buffer
135
- #
136
- # @api public
137
- class RailsOutputBuffer < StringBuffer
138
- define_options :streaming,
139
- :buffer_class => 'ActiveSupport::SafeBuffer',
140
- :buffer => '@output_buffer',
141
- # output_buffer is needed for Rails 3.1 Streaming support
142
- :capture_generator => RailsOutputBuffer
143
-
144
- def call(exp)
145
- [preamble, compile(exp), postamble].join('; ')
146
- end
147
-
148
- def preamble
149
- if options[:streaming] && options[:buffer] == '@output_buffer'
150
- "#{buffer} = output_buffer || #{options[:buffer_class]}.new"
151
- else
152
- "#{buffer} = #{options[:buffer_class]}.new"
153
- end
154
- end
155
-
156
- def concat(str)
157
- "#{buffer}.safe_concat((#{str}))"
158
- end
159
- end
160
- end
161
-
162
- Generator.default_options[:capture_generator] = Generators::StringBuffer
163
- end