temple 0.6.10 → 0.7.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.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +0 -2
  3. data/CHANGES +15 -0
  4. data/README.md +6 -6
  5. data/Rakefile +1 -1
  6. data/lib/temple.rb +5 -4
  7. data/lib/temple/engine.rb +1 -1
  8. data/lib/temple/erb/engine.rb +2 -2
  9. data/lib/temple/filters/encoding.rb +1 -1
  10. data/lib/temple/filters/eraser.rb +1 -1
  11. data/lib/temple/filters/escapable.rb +2 -2
  12. data/lib/temple/filters/remove_bom.rb +2 -9
  13. data/lib/temple/filters/static_freezer.rb +11 -0
  14. data/lib/temple/filters/validator.rb +1 -1
  15. data/lib/temple/generator.rb +4 -4
  16. data/lib/temple/generators/rails_output_buffer.rb +3 -3
  17. data/lib/temple/html/attribute_merger.rb +1 -1
  18. data/lib/temple/html/attribute_remover.rb +1 -1
  19. data/lib/temple/html/attribute_sorter.rb +1 -1
  20. data/lib/temple/html/fast.rb +42 -42
  21. data/lib/temple/html/pretty.rb +30 -39
  22. data/lib/temple/map.rb +105 -0
  23. data/lib/temple/mixins/engine_dsl.rb +11 -9
  24. data/lib/temple/mixins/options.rb +26 -24
  25. data/lib/temple/mixins/template.rb +3 -3
  26. data/lib/temple/templates/rails.rb +14 -37
  27. data/lib/temple/templates/tilt.rb +4 -4
  28. data/lib/temple/utils.rb +23 -26
  29. data/lib/temple/version.rb +1 -1
  30. data/temple.gemspec +2 -0
  31. data/test/filters/test_eraser.rb +4 -4
  32. data/test/filters/test_escapable.rb +7 -5
  33. data/test/filters/test_static_freezer.rb +25 -0
  34. data/test/html/test_attribute_sorter.rb +1 -1
  35. data/test/html/test_fast.rb +6 -6
  36. data/test/html/test_pretty.rb +2 -8
  37. data/test/test_engine.rb +1 -1
  38. data/test/test_erb.rb +2 -2
  39. data/test/test_filter.rb +2 -2
  40. data/test/test_generator.rb +4 -4
  41. data/test/test_map.rb +39 -0
  42. metadata +7 -5
  43. data/lib/temple/hash.rb +0 -105
  44. data/test/test_hash.rb +0 -39
@@ -32,14 +32,16 @@ describe Temple::Filters::Escapable do
32
32
  end
33
33
 
34
34
  it 'should have use_html_safe option' do
35
- filter = Temple::Filters::Escapable.new(:use_html_safe => true)
36
- filter.call([:escape, true,
37
- [:static, Temple::HTML::SafeString.new("a < b")]
38
- ]).should.equal [:static, "a < b"]
35
+ with_html_safe do
36
+ filter = Temple::Filters::Escapable.new(use_html_safe: true)
37
+ filter.call([:escape, true,
38
+ [:static, Temple::HTML::SafeString.new("a < b")]
39
+ ]).should.equal [:static, "a < b"]
40
+ end
39
41
  end
40
42
 
41
43
  it 'should support censoring' do
42
- filter = Temple::Filters::Escapable.new(:escape_code => '(%s).gsub("Temple sucks", "Temple rocks")')
44
+ filter = Temple::Filters::Escapable.new(escape_code: '(%s).gsub("Temple sucks", "Temple rocks")')
43
45
  filter.call([:escape, true,
44
46
  [:static, "~~ Temple sucks ~~"]
45
47
  ]).should.equal [:static, "~~ Temple rocks ~~"]
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+
3
+ describe Temple::Filters::StaticFreezer do
4
+ if RUBY_VERSION >= '2.1'
5
+ it 'should freeze static on new ruby' do
6
+ filter = Temple::Filters::StaticFreezer.new
7
+ filter.call([:static, 'hi']).should.equal [:dynamic, '"hi".freeze']
8
+ end
9
+ else
10
+ it 'should not freeze static on old ruby' do
11
+ filter = Temple::Filters::StaticFreezer.new
12
+ filter.call([:static, 'hi']).should.equal [:static, 'hi']
13
+ end
14
+ end
15
+
16
+ it 'should freeze static if free_static==true' do
17
+ filter = Temple::Filters::StaticFreezer.new(freeze_static: true)
18
+ filter.call([:static, 'hi']).should.equal [:dynamic, '"hi".freeze']
19
+ end
20
+
21
+ it 'should not freeze static if free_static==false' do
22
+ filter = Temple::Filters::StaticFreezer.new(freeze_static: false)
23
+ filter.call([:static, 'hi']).should.equal [:static, 'hi']
24
+ end
25
+ end
@@ -3,7 +3,7 @@ require 'helper'
3
3
  describe Temple::HTML::AttributeSorter do
4
4
  before do
5
5
  @ordered = Temple::HTML::AttributeSorter.new
6
- @unordered = Temple::HTML::AttributeSorter.new :sort_attrs => false
6
+ @unordered = Temple::HTML::AttributeSorter.new sort_attrs: false
7
7
  end
8
8
 
9
9
  it 'should sort html attributes by name by default, when :sort_attrs is true' do
@@ -21,15 +21,15 @@ describe Temple::HTML::Fast do
21
21
  end
22
22
 
23
23
  it 'should compile js wrapped in comments' do
24
- Temple::HTML::Fast.new(:js_wrapper => nil).call([:html, :js, [:static, 'test']]).should.equal [:static, "test"]
25
- Temple::HTML::Fast.new(:js_wrapper => :comment).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "<!--\n"], [:static, "test"], [:static, "\n//-->"]]
26
- Temple::HTML::Fast.new(:js_wrapper => :cdata).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "\n//<![CDATA[\n"], [:static, "test"], [:static, "\n//]]>\n"]]
27
- Temple::HTML::Fast.new(:js_wrapper => :both).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "<!--\n//<![CDATA[\n"], [:static, "test"], [:static, "\n//]]>\n//-->"]]
24
+ Temple::HTML::Fast.new(js_wrapper: nil).call([:html, :js, [:static, 'test']]).should.equal [:static, "test"]
25
+ Temple::HTML::Fast.new(js_wrapper: :comment).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "<!--\n"], [:static, "test"], [:static, "\n//-->"]]
26
+ Temple::HTML::Fast.new(js_wrapper: :cdata).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "\n//<![CDATA[\n"], [:static, "test"], [:static, "\n//]]>\n"]]
27
+ Temple::HTML::Fast.new(js_wrapper: :both).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "<!--\n//<![CDATA[\n"], [:static, "test"], [:static, "\n//]]>\n//-->"]]
28
28
  end
29
29
 
30
30
  it 'should guess default js comment' do
31
- Temple::HTML::Fast.new(:js_wrapper => :guess, :format => :xhtml).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "\n//<![CDATA[\n"], [:static, "test"], [:static, "\n//]]>\n"]]
32
- Temple::HTML::Fast.new(:js_wrapper => :guess, :format => :html).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "<!--\n"], [:static, "test"], [:static, "\n//-->"]]
31
+ Temple::HTML::Fast.new(js_wrapper: :guess, format: :xhtml).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "\n//<![CDATA[\n"], [:static, "test"], [:static, "\n//]]>\n"]]
32
+ Temple::HTML::Fast.new(js_wrapper: :guess, format: :html).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "<!--\n"], [:static, "test"], [:static, "\n//-->"]]
33
33
  end
34
34
 
35
35
  it 'should compile autoclosed html tag' do
@@ -20,10 +20,7 @@ describe Temple::HTML::Pretty do
20
20
  [:static, ">"],
21
21
  [:multi,
22
22
  [:static, "\n text"],
23
- [:multi,
24
- [:code, "_temple_html_pretty2 = (code).to_s"],
25
- [:code, "if _temple_html_pretty1 !~ _temple_html_pretty2; _temple_html_pretty2 = _temple_html_pretty2.gsub(\"\\n\", \"\\n \"); end"],
26
- [:dynamic, "_temple_html_pretty2"]]],
23
+ [:dynamic, "::Temple::Utils.indent_dynamic((code), false, \"\\n \", _temple_html_pretty1)"]],
27
24
  [:static, "\n </p>"]],
28
25
  [:static, "\n</div>"]]]
29
26
  end
@@ -52,10 +49,7 @@ describe Temple::HTML::Pretty do
52
49
  [:dynamic, '"text<".html_safe']
53
50
  ).should.equal [:multi,
54
51
  [:code, "_temple_html_pretty1 = /<code|<pre|<textarea/"],
55
- [:multi,
56
- [:code, "_temple_html_pretty2 = (\"text<\".html_safe).to_s"],
57
- [:code, "if _temple_html_pretty1 !~ _temple_html_pretty2; _temple_html_pretty3 = _temple_html_pretty2.html_safe?; _temple_html_pretty2 = _temple_html_pretty2.gsub(\"\\n\", \"\\n\"); _temple_html_pretty2 = _temple_html_pretty2.html_safe if _temple_html_pretty3; end"],
58
- [:dynamic, "_temple_html_pretty2"]]]
52
+ [:dynamic, "::Temple::Utils.indent_dynamic((\"text<\".html_safe), nil, \"\\n\", _temple_html_pretty1)"]]
59
53
  end
60
54
  end
61
55
  end
@@ -19,7 +19,7 @@ class TestEngine < Temple::Engine
19
19
  end
20
20
  use :MyFilter1, proc {|exp| exp }
21
21
  use :MyFilter2, proc {|exp| exp }
22
- use Temple::HTML::Pretty, :format, :pretty => true
22
+ use Temple::HTML::Pretty, pretty: true
23
23
  filter :MultiFlattener
24
24
  generator :ArrayBuffer
25
25
  use :BeforeLast, Callable1.new
@@ -56,7 +56,7 @@ world}
56
56
  <% end %>
57
57
  }
58
58
 
59
- erb(src, :trim_mode => '>').should.equal ERB.new(src, nil, '>').result
60
- erb(src, :trim_mode => '<>').should.equal ERB.new(src, nil, '<>').result
59
+ erb(src, trim_mode: '>').should.equal ERB.new(src, nil, '>').result
60
+ erb(src, trim_mode: '<>').should.equal ERB.new(src, nil, '<>').result
61
61
  end
62
62
  end
@@ -13,8 +13,8 @@ describe Temple::Filter do
13
13
  Temple::Filter.should.respond_to :default_options
14
14
  Temple::Filter.should.respond_to :set_default_options
15
15
  Temple::Filter.should.respond_to :define_options
16
- Temple::Filter.new.options.should.be.instance_of Temple::ImmutableHash
17
- SimpleFilter.new(:key => 3).options[:key].should.equal 3
16
+ Temple::Filter.new.options.should.be.instance_of Temple::ImmutableMap
17
+ SimpleFilter.new(key: 3).options[:key].should.equal 3
18
18
  end
19
19
 
20
20
  it 'should implement call' do
@@ -32,7 +32,7 @@ describe Temple::Generator do
32
32
  end
33
33
 
34
34
  it 'should compile multi expression' do
35
- gen = SimpleGenerator.new(:buffer => "VAR")
35
+ gen = SimpleGenerator.new(buffer: "VAR")
36
36
  gen.call([:multi,
37
37
  [:static, "static"],
38
38
  [:dynamic, "dynamic"],
@@ -41,14 +41,14 @@ describe Temple::Generator do
41
41
  end
42
42
 
43
43
  it 'should compile capture' do
44
- gen = SimpleGenerator.new(:buffer => "VAR", :capture_generator => SimpleGenerator)
44
+ gen = SimpleGenerator.new(buffer: "VAR", capture_generator: SimpleGenerator)
45
45
  gen.call([:capture, "foo",
46
46
  [:static, "test"]
47
47
  ]).should.equal 'VAR = BUFFER; foo = BUFFER; foo << (S:test); foo; VAR'
48
48
  end
49
49
 
50
50
  it 'should compile capture with multi' do
51
- gen = SimpleGenerator.new(:buffer => "VAR", :capture_generator => SimpleGenerator)
51
+ gen = SimpleGenerator.new(buffer: "VAR", capture_generator: SimpleGenerator)
52
52
  gen.call([:multi,
53
53
  [:static, "before"],
54
54
 
@@ -63,7 +63,7 @@ describe Temple::Generator do
63
63
  end
64
64
 
65
65
  it 'should compile newlines' do
66
- gen = SimpleGenerator.new(:buffer => "VAR")
66
+ gen = SimpleGenerator.new(buffer: "VAR")
67
67
  gen.call([:multi,
68
68
  [:static, "static"],
69
69
  [:newline],
@@ -0,0 +1,39 @@
1
+ require 'helper'
2
+
3
+ describe Temple::ImmutableMap do
4
+ it 'has read accessor' do
5
+ hash = Temple::ImmutableMap.new({a: 1},{b: 2, a: 3})
6
+ hash[:a].should.equal 1
7
+ hash[:b].should.equal 2
8
+ end
9
+
10
+ it 'has include?' do
11
+ hash = Temple::ImmutableMap.new({a: 1},{b: 2, a: 3})
12
+ hash.should.include :a
13
+ hash.should.include :b
14
+ hash.should.not.include :c
15
+ end
16
+
17
+ it 'has values' do
18
+ Temple::ImmutableMap.new({a: 1},{b: 2, a: 3}).values.sort.should.equal [1,2]
19
+ end
20
+
21
+ it 'has keys' do
22
+ Temple::ImmutableMap.new({a: 1},{b: 2, a: 3}).keys.should.equal [:a,:b]
23
+ end
24
+
25
+ it 'has to_a' do
26
+ Temple::ImmutableMap.new({a: 1},{b: 2, a: 3}).to_a.should.equal [[:a, 1], [:b, 2]]
27
+ end
28
+ end
29
+
30
+ describe Temple::MutableMap do
31
+ it 'has write accessor' do
32
+ parent = {a: 1}
33
+ hash = Temple::MutableMap.new(parent)
34
+ hash[:a].should.equal 1
35
+ hash[:a] = 2
36
+ hash[:a].should.equal 2
37
+ parent[:a].should.equal 1
38
+ end
39
+ end
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.10
4
+ version: 0.7.1
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: 2014-10-27 00:00:00.000000000 Z
12
+ date: 2014-12-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: tilt
@@ -86,6 +86,7 @@ files:
86
86
  - lib/temple/filters/escapable.rb
87
87
  - lib/temple/filters/multi_flattener.rb
88
88
  - lib/temple/filters/remove_bom.rb
89
+ - lib/temple/filters/static_freezer.rb
89
90
  - lib/temple/filters/static_merger.rb
90
91
  - lib/temple/filters/validator.rb
91
92
  - lib/temple/generator.rb
@@ -95,7 +96,6 @@ files:
95
96
  - lib/temple/generators/rails_output_buffer.rb
96
97
  - lib/temple/generators/string_buffer.rb
97
98
  - lib/temple/grammar.rb
98
- - lib/temple/hash.rb
99
99
  - lib/temple/html/attribute_merger.rb
100
100
  - lib/temple/html/attribute_remover.rb
101
101
  - lib/temple/html/attribute_sorter.rb
@@ -104,6 +104,7 @@ files:
104
104
  - lib/temple/html/filter.rb
105
105
  - lib/temple/html/pretty.rb
106
106
  - lib/temple/html/safe.rb
107
+ - lib/temple/map.rb
107
108
  - lib/temple/mixins/dispatcher.rb
108
109
  - lib/temple/mixins/engine_dsl.rb
109
110
  - lib/temple/mixins/grammar_dsl.rb
@@ -122,6 +123,7 @@ files:
122
123
  - test/filters/test_eraser.rb
123
124
  - test/filters/test_escapable.rb
124
125
  - test/filters/test_multi_flattener.rb
126
+ - test/filters/test_static_freezer.rb
125
127
  - test/filters/test_static_merger.rb
126
128
  - test/helper.rb
127
129
  - test/html/test_attribute_merger.rb
@@ -136,7 +138,7 @@ files:
136
138
  - test/test_filter.rb
137
139
  - test/test_generator.rb
138
140
  - test/test_grammar.rb
139
- - test/test_hash.rb
141
+ - test/test_map.rb
140
142
  - test/test_utils.rb
141
143
  homepage: https://github.com/judofyr/temple
142
144
  licenses:
@@ -150,7 +152,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
150
152
  requirements:
151
153
  - - ">="
152
154
  - !ruby/object:Gem::Version
153
- version: '0'
155
+ version: 1.9.2
154
156
  required_rubygems_version: !ruby/object:Gem::Requirement
155
157
  requirements:
156
158
  - - ">="
@@ -1,105 +0,0 @@
1
- module Temple
2
- # Immutable hash class which supports hash merging
3
- # @api public
4
- class ImmutableHash
5
- include Enumerable
6
-
7
- def initialize(*hash)
8
- @hash = hash.compact
9
- end
10
-
11
- def include?(key)
12
- @hash.any? {|h| h.include?(key) }
13
- end
14
-
15
- def [](key)
16
- @hash.each {|h| return h[key] if h.include?(key) }
17
- nil
18
- end
19
-
20
- def each
21
- keys.each {|k| yield(k, self[k]) }
22
- end
23
-
24
- def keys
25
- @hash.inject([]) {|keys, h| keys.concat(h.keys) }.uniq
26
- end
27
-
28
- def values
29
- keys.map {|k| self[k] }
30
- end
31
-
32
- def to_hash
33
- result = {}
34
- each {|k, v| result[k] = v }
35
- result
36
- end
37
- end
38
-
39
- # Mutable hash class which supports hash merging
40
- # @api public
41
- class MutableHash < ImmutableHash
42
- def initialize(*hash)
43
- super({}, *hash)
44
- end
45
-
46
- def []=(key, value)
47
- @hash.first[key] = value
48
- end
49
-
50
- def update(hash)
51
- @hash.first.update(hash)
52
- end
53
- end
54
-
55
- class OptionHash < MutableHash
56
- def initialize(*hash, &block)
57
- super(*hash)
58
- @handler = block
59
- @valid = {}
60
- @deprecated = {}
61
- end
62
-
63
- def []=(key, value)
64
- validate_key!(key)
65
- super
66
- end
67
-
68
- def update(hash)
69
- validate_hash!(hash)
70
- super
71
- end
72
-
73
- def valid_keys
74
- (keys + @valid.keys +
75
- @hash.map {|h| h.valid_keys if h.respond_to?(:valid_keys) }.compact.flatten).uniq
76
- end
77
-
78
- def add_valid_keys(*keys)
79
- keys.flatten.each { |key| @valid[key] = true }
80
- end
81
-
82
- def add_deprecated_keys(*keys)
83
- keys.flatten.each { |key| @valid[key] = @deprecated[key] = true }
84
- end
85
-
86
- def validate_hash!(hash)
87
- hash.to_hash.keys.each {|key| validate_key!(key) }
88
- end
89
-
90
- def validate_key!(key)
91
- @handler.call(self, key, true) if deprecated_key?(key)
92
- @handler.call(self, key, false) unless valid_key?(key)
93
- end
94
-
95
- def deprecated_key?(key)
96
- @deprecated.include?(key) ||
97
- @hash.any? {|h| h.deprecated_key?(key) if h.respond_to?(:deprecated_key?) }
98
- end
99
-
100
- def valid_key?(key)
101
- include?(key) || @valid.include?(key) ||
102
- @hash.any? {|h| h.valid_key?(key) if h.respond_to?(:valid_key?) }
103
- end
104
- end
105
- end
@@ -1,39 +0,0 @@
1
- require 'helper'
2
-
3
- describe Temple::ImmutableHash do
4
- it 'has read accessor' do
5
- hash = Temple::ImmutableHash.new({:a => 1},{:b => 2, :a => 3})
6
- hash[:a].should.equal 1
7
- hash[:b].should.equal 2
8
- end
9
-
10
- it 'has include?' do
11
- hash = Temple::ImmutableHash.new({:a => 1},{:b => 2, :a => 3})
12
- hash.should.include :a
13
- hash.should.include :b
14
- hash.should.not.include :c
15
- end
16
-
17
- it 'has values' do
18
- Temple::ImmutableHash.new({:a => 1},{:b => 2, :a => 3}).values.sort.should.equal [1,2]
19
- end
20
-
21
- it 'has keys' do
22
- Temple::ImmutableHash.new({:a => 1},{:b => 2, :a => 3}).keys.should.equal [:a,:b]
23
- end
24
-
25
- it 'has to_a' do
26
- Temple::ImmutableHash.new({:a => 1},{:b => 2, :a => 3}).to_a.should.equal [[:a, 1], [:b, 2]]
27
- end
28
- end
29
-
30
- describe Temple::MutableHash do
31
- it 'has write accessor' do
32
- parent = {:a => 1}
33
- hash = Temple::MutableHash.new(parent)
34
- hash[:a].should.equal 1
35
- hash[:a] = 2
36
- hash[:a].should.equal 2
37
- parent[:a].should.equal 1
38
- end
39
- end