temple 0.1.8 → 0.2.0

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.
@@ -6,22 +6,22 @@ describe Temple::HTML::Fast do
6
6
  end
7
7
 
8
8
  it 'should compile html doctype' do
9
- @html.compile([:multi, [:html, :doctype, '5']]).should.equal [:multi, [:static, '<!DOCTYPE html>']]
10
- @html.compile([:multi, [:html, :doctype, 'html']]).should.equal [:multi, [:static, '<!DOCTYPE html>']]
11
- @html.compile([:multi, [:html, :doctype, '1.1']]).should.equal [:multi,
9
+ @html.call([:multi, [:html, :doctype, '5']]).should.equal [:multi, [:static, '<!DOCTYPE html>']]
10
+ @html.call([:multi, [:html, :doctype, 'html']]).should.equal [:multi, [:static, '<!DOCTYPE html>']]
11
+ @html.call([:multi, [:html, :doctype, '1.1']]).should.equal [:multi,
12
12
  [:static, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">']]
13
13
  end
14
14
 
15
15
  it 'should compile xml encoding' do
16
- @html.compile([:html, :doctype, 'xml latin1']).should.equal [:static, "<?xml version='1.0' encoding='latin1' ?>"]
16
+ @html.call([:html, :doctype, 'xml latin1']).should.equal [:static, "<?xml version='1.0' encoding='latin1' ?>"]
17
17
  end
18
18
 
19
19
  it 'should compile html comment' do
20
- @html.compile([:html, :comment, 'test']).should.equal [:multi, [:static, "<!--"], "test", [:static, "-->"]]
20
+ @html.call([:html, :comment, 'test']).should.equal [:multi, [:static, "<!--"], "test", [:static, "-->"]]
21
21
  end
22
22
 
23
23
  it 'should compile autoclosed html tag' do
24
- @html.compile([:html, :tag,
24
+ @html.call([:html, :tag,
25
25
  'img', [:attrs],
26
26
  false, [:multi]
27
27
  ]).should.equal [:multi,
@@ -32,7 +32,7 @@ describe Temple::HTML::Fast do
32
32
  end
33
33
 
34
34
  it 'should compile explicitly closed html tag' do
35
- @html.compile([:html, :tag,
35
+ @html.call([:html, :tag,
36
36
  'closed', [:attrs],
37
37
  true, [:multi]
38
38
  ]).should.equal [:multi,
@@ -44,7 +44,7 @@ describe Temple::HTML::Fast do
44
44
 
45
45
  it 'should raise error on closed tag with content' do
46
46
  lambda {
47
- @html.compile([:html, :tag,
47
+ @html.call([:html, :tag,
48
48
  'img', [:attrs],
49
49
  false, [:content]
50
50
  ])
@@ -52,7 +52,7 @@ describe Temple::HTML::Fast do
52
52
  end
53
53
 
54
54
  it 'should compile html with content' do
55
- @html.compile([:html, :tag,
55
+ @html.call([:html, :tag,
56
56
  'div', [:attrs],
57
57
  false, [:content]
58
58
  ]).should.equal [:multi,
@@ -64,7 +64,7 @@ describe Temple::HTML::Fast do
64
64
  end
65
65
 
66
66
  it 'should compile html with static attrs' do
67
- @html.compile([:html, :tag,
67
+ @html.call([:html, :tag,
68
68
  'div',
69
69
  [:html, :staticattrs,
70
70
  ['id', [:static, 'test']],
@@ -75,12 +75,17 @@ describe Temple::HTML::Fast do
75
75
  "<div"],
76
76
  [:multi,
77
77
  [:multi,
78
- [:static, " "],
79
- [:static, "class"],
80
- [:static, "="],
81
- [:static, "'"],
82
- [:dynamic, "block"],
83
- [:static, "'"]],
78
+ [:capture, "_temple_htmlattr1",
79
+ [:dynamic, "block"]],
80
+ [:block, "unless _temple_htmlattr1.empty?"],
81
+ [:multi,
82
+ [:static, " "],
83
+ [:static, "class"],
84
+ [:static, "="],
85
+ [:static, "'"],
86
+ [:dynamic, "_temple_htmlattr1"],
87
+ [:static, "'"]],
88
+ [:block, "end"]],
84
89
  [:multi,
85
90
  [:static, " "],
86
91
  [:static, "id"],
@@ -94,7 +99,7 @@ describe Temple::HTML::Fast do
94
99
  end
95
100
 
96
101
  it 'should compile html with merged ids' do
97
- @html.compile([:html, :tag,
102
+ @html.call([:html, :tag,
98
103
  'div', [:html, :staticattrs, ['id', [:static, 'a']], ['id', [:dynamic, 'b']]],
99
104
  false, [:content]
100
105
  ]).should.equal [:multi,
@@ -116,7 +121,7 @@ describe Temple::HTML::Fast do
116
121
  end
117
122
 
118
123
  it 'should compile html with merged classes' do
119
- @html.compile([:html, :tag,
124
+ @html.call([:html, :tag,
120
125
  'div', [:html, :staticattrs, ['class', [:static, 'a']], ['class', [:dynamic, 'b']]],
121
126
  false, [:content]
122
127
  ]).should.equal [:multi,
@@ -139,16 +144,16 @@ describe Temple::HTML::Fast do
139
144
 
140
145
  it 'should keep blocks intact' do
141
146
  exp = [:multi, [:block, 'foo']]
142
- @html.compile(exp).should.equal exp
147
+ @html.call(exp).should.equal exp
143
148
  end
144
149
 
145
150
  it 'should keep statics intact' do
146
151
  exp = [:multi, [:static, '<']]
147
- @html.compile(exp).should.equal exp
152
+ @html.call(exp).should.equal exp
148
153
  end
149
154
 
150
155
  it 'should keep dynamic intact' do
151
156
  exp = [:multi, [:dynamic, 'foo']]
152
- @html.compile(exp).should.equal exp
157
+ @html.call(exp).should.equal exp
153
158
  end
154
159
  end
@@ -6,7 +6,7 @@ describe Temple::HTML::Pretty do
6
6
  end
7
7
 
8
8
  it 'should indent nested tags' do
9
- @html.compile([:html, :tag, 'div', [:multi], false,
9
+ @html.call([:html, :tag, 'div', [:multi], false,
10
10
  [:html, :tag, 'p', [:multi], false, [:multi, [:static, 'text'], [:dynamic, 'code']]]
11
11
  ]).should.equal [:multi,
12
12
  [:block, "_temple_pre_tags = /<pre|<textarea/"],
@@ -27,7 +27,7 @@ describe Temple::HTML::Pretty do
27
27
 
28
28
 
29
29
  it 'should not indent preformatted tags' do
30
- @html.compile([:html, :tag, 'pre', [:multi], false,
30
+ @html.call([:html, :tag, 'pre', [:multi], false,
31
31
  [:html, :tag, 'p', [:multi], false, [:static, 'text']]
32
32
  ]).should.equal [:multi,
33
33
  [:block, "_temple_pre_tags = /<pre|<textarea/"],
@@ -0,0 +1,144 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class OtherCallable
5
+ def call(exp)
6
+ exp
7
+ end
8
+ end
9
+
10
+ class TestEngine < Temple::Engine
11
+ use(:Parser) do |input|
12
+ [:static, input]
13
+ end
14
+ use :MyFilter1, proc {|exp| exp }
15
+ use :MyFilter2, proc {|exp| exp }
16
+ use Temple::HTML::Pretty, :format, :pretty => true
17
+ filter :MultiFlattener
18
+ generator :ArrayBuffer
19
+ use :FinalFilter, OtherCallable.new
20
+ end
21
+
22
+ describe Temple::Engine do
23
+ it 'should build chain' do
24
+ TestEngine.chain.size.should.equal 7
25
+
26
+ TestEngine.chain[0].first.should.equal :Parser
27
+ TestEngine.chain[0].size.should.equal 2
28
+ TestEngine.chain[0].last.should.be.instance_of UnboundMethod
29
+
30
+ TestEngine.chain[1].first.should.equal :MyFilter1
31
+ TestEngine.chain[1].size.should.equal 2
32
+ TestEngine.chain[1].last.should.be.instance_of UnboundMethod
33
+
34
+ TestEngine.chain[2].first.should.equal :MyFilter2
35
+ TestEngine.chain[2].size.should.equal 2
36
+ TestEngine.chain[2].last.should.be.instance_of UnboundMethod
37
+
38
+ TestEngine.chain[3].size.should.equal 4
39
+ TestEngine.chain[3].should.equal [:'Temple::HTML::Pretty', Temple::HTML::Pretty, [:format], {:pretty => true}]
40
+
41
+ TestEngine.chain[4].size.should.equal 4
42
+ TestEngine.chain[4].should.equal [:MultiFlattener, Temple::Filters::MultiFlattener, [], nil]
43
+
44
+ TestEngine.chain[5].size.should.equal 4
45
+ TestEngine.chain[5].should.equal [:ArrayBuffer, Temple::Generators::ArrayBuffer, [], nil]
46
+
47
+ TestEngine.chain[6].size.should.equal 2
48
+ TestEngine.chain[6][0].should.equal :FinalFilter
49
+ TestEngine.chain[6][1].should.be.instance_of OtherCallable
50
+ end
51
+
52
+ it 'should instantiate chain' do
53
+ e = TestEngine.new
54
+ e.chain[0].should.be.instance_of Method
55
+ e.chain[1].should.be.instance_of Method
56
+ e.chain[2].should.be.instance_of Method
57
+ e.chain[3].should.be.instance_of Temple::HTML::Pretty
58
+ e.chain[4].should.be.instance_of Temple::Filters::MultiFlattener
59
+ e.chain[5].should.be.instance_of Temple::Generators::ArrayBuffer
60
+ e.chain[6].should.be.instance_of OtherCallable
61
+ end
62
+
63
+ it 'should have #append' do
64
+ e = TestEngine.new do |engine|
65
+ engine.append :MyFilter3 do |exp|
66
+ exp
67
+ end
68
+ engine.chain.size.should.equal 8
69
+ engine.chain[7].first.should.equal :MyFilter3
70
+ engine.chain[7].size.should.equal 2
71
+ engine.chain[7].last.should.be.instance_of Method
72
+ end
73
+ e.chain.size.should.equal 8
74
+ e.chain[7].should.be.instance_of Method
75
+ end
76
+
77
+ it 'should have #prepend' do
78
+ e = TestEngine.new do |engine|
79
+ engine.prepend :MyFilter0 do |exp|
80
+ exp
81
+ end
82
+ engine.chain.size.should.equal 8
83
+ engine.chain[0].first.should.equal :MyFilter0
84
+ engine.chain[0].size.should.equal 2
85
+ engine.chain[0].last.should.be.instance_of Method
86
+ engine.chain[1].first.should.equal :Parser
87
+ end
88
+ e.chain.size.should.equal 8
89
+ e.chain[0].should.be.instance_of Method
90
+ end
91
+
92
+ it 'should have #after' do
93
+ e = TestEngine.new do |engine|
94
+ engine.after :Parser, :MyFilter0 do |exp|
95
+ exp
96
+ end
97
+ engine.chain.size.should.equal 8
98
+ engine.chain[0].first.should.equal :Parser
99
+ engine.chain[1].first.should.equal :MyFilter0
100
+ engine.chain[2].first.should.equal :MyFilter1
101
+ end
102
+ end
103
+
104
+ it 'should have #before' do
105
+ e = TestEngine.new do |engine|
106
+ engine.before :MyFilter1, :MyFilter0 do |exp|
107
+ exp
108
+ end
109
+ engine.chain.size.should.equal 8
110
+ engine.chain[0].first.should.equal :Parser
111
+ engine.chain[1].first.should.equal :MyFilter0
112
+ engine.chain[2].first.should.equal :MyFilter1
113
+ end
114
+ end
115
+
116
+ it 'should have #remove' do
117
+ e = TestEngine.new do |engine|
118
+ engine.remove :MyFilter1
119
+ engine.chain.size.should.equal 6
120
+ engine.chain[0].first.should.equal :Parser
121
+ engine.chain[1].first.should.equal :MyFilter2
122
+ end
123
+ end
124
+
125
+ it 'should have #replace' do
126
+ e = TestEngine.new do |engine|
127
+ engine.before :Parser, :MyParser do |exp|
128
+ exp
129
+ end
130
+ engine.chain.size.should.equal 8
131
+ engine.chain[0].first.should.equal :MyParser
132
+ end
133
+ end
134
+
135
+ it 'should work with inheritance' do
136
+ inherited_engine = Class.new(TestEngine)
137
+ inherited_engine.chain.size.should.equal 7
138
+ inherited_engine.append :MyFilter3 do |exp|
139
+ exp
140
+ end
141
+ inherited_engine.chain.size.should.equal 8
142
+ TestEngine.chain.size.should.equal 7
143
+ end
144
+ end
data/test/test_erb.rb CHANGED
@@ -1,13 +1,14 @@
1
1
  require 'helper'
2
2
  require 'erb'
3
+ require 'tilt'
3
4
 
4
5
  class Bacon::Context
5
6
  def erb(src, options = {})
6
- Temple::ERB::Template.new(options) { src }.render
7
+ Temple::Templates::Tilt(Temple::ERB::Engine).new(options) { src }.render
7
8
  end
8
9
  end
9
10
 
10
- describe Temple::ERB::Template do
11
+ describe Temple::ERB::Engine do
11
12
 
12
13
  it 'should compile erb' do
13
14
  src = %q{
@@ -26,14 +26,14 @@ describe Temple::Generator do
26
26
  it 'should compile simple expressions' do
27
27
  gen = SimpleGenerator.new
28
28
 
29
- gen.compile([:static, "test"]).should.match(/ << \(S:test\)/)
30
- gen.compile([:dynamic, "test"]).should.match(/ << \(D:test\)/)
31
- gen.compile([:block, "test"]).should.match(/B:test/)
29
+ gen.call([:static, "test"]).should.match(/ << \(S:test\)/)
30
+ gen.call([:dynamic, "test"]).should.match(/ << \(D:test\)/)
31
+ gen.call([:block, "test"]).should.match(/B:test/)
32
32
  end
33
33
 
34
34
  it 'should compile multi expression' do
35
35
  gen = SimpleGenerator.new(:buffer => "VAR")
36
- str = gen.compile([:multi,
36
+ str = gen.call([:multi,
37
37
  [:static, "static"],
38
38
  [:dynamic, "dynamic"],
39
39
  [:block, "block"]
@@ -47,7 +47,7 @@ describe Temple::Generator do
47
47
 
48
48
  it 'should compile capture' do
49
49
  gen = SimpleGenerator.new(:buffer => "VAR", :capture_generator => SimpleGenerator)
50
- str = gen.compile([:capture, "foo", [:static, "test"]])
50
+ str = gen.call([:capture, "foo", [:static, "test"]])
51
51
 
52
52
  str.should.match(/foo = BUFFER/)
53
53
  str.should.match(/foo << \(S:test\)/)
@@ -56,7 +56,7 @@ describe Temple::Generator do
56
56
 
57
57
  it 'should compile capture with multi' do
58
58
  gen = SimpleGenerator.new(:buffer => "VAR", :capture_generator => SimpleGenerator)
59
- str = gen.compile([:multi,
59
+ str = gen.call([:multi,
60
60
  [:static, "before"],
61
61
 
62
62
  [:capture, "foo", [:multi,
@@ -78,7 +78,7 @@ describe Temple::Generator do
78
78
 
79
79
  it 'should compile newlines' do
80
80
  gen = SimpleGenerator.new(:buffer => "VAR")
81
- str = gen.compile([:multi,
81
+ str = gen.call([:multi,
82
82
  [:static, "static"],
83
83
  [:newline],
84
84
  [:dynamic, "dynamic"],
data/test/test_utils.rb CHANGED
@@ -26,3 +26,41 @@ describe Temple::Utils do
26
26
  end
27
27
  end
28
28
  end
29
+
30
+ describe Temple::Utils::ImmutableHash do
31
+ it 'has read accessor' do
32
+ hash = Temple::Utils::ImmutableHash.new({:a => 1},{:b => 2, :a => 3})
33
+ hash[:a].should.equal 1
34
+ hash[:b].should.equal 2
35
+ end
36
+
37
+ it 'has include?' do
38
+ hash = Temple::Utils::ImmutableHash.new({:a => 1},{:b => 2, :a => 3})
39
+ hash.should.include :a
40
+ hash.should.include :b
41
+ hash.should.not.include :c
42
+ end
43
+
44
+ it 'has values' do
45
+ Temple::Utils::ImmutableHash.new({:a => 1},{:b => 2, :a => 3}).values.sort.should.equal [1,2]
46
+ end
47
+
48
+ it 'has keys' do
49
+ Temple::Utils::ImmutableHash.new({:a => 1},{:b => 2, :a => 3}).keys.should.equal [:a,:b]
50
+ end
51
+
52
+ it 'has to_a' do
53
+ Temple::Utils::ImmutableHash.new({:a => 1},{:b => 2, :a => 3}).to_a.should.equal [[:a, 1], [:b, 2]]
54
+ end
55
+ end
56
+
57
+ describe Temple::Utils::MutableHash do
58
+ it 'has write accessor' do
59
+ parent = {:a => 1}
60
+ hash = Temple::Utils::MutableHash.new(parent)
61
+ hash[:a].should.equal 1
62
+ hash[:a] = 2
63
+ hash[:a].should.equal 2
64
+ parent[:a].should.equal 1
65
+ end
66
+ end
metadata CHANGED
@@ -2,15 +2,16 @@
2
2
  name: temple
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.8
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Magnus Holm
9
+ - Daniel Mendler
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
13
 
13
- date: 2011-03-10 00:00:00 +01:00
14
+ date: 2011-03-30 00:00:00 +02:00
14
15
  default_executable:
15
16
  dependencies:
16
17
  - !ruby/object:Gem::Dependency
@@ -36,7 +37,9 @@ dependencies:
36
37
  type: :development
37
38
  version_requirements: *id002
38
39
  description:
39
- email: judofyr@gmail.com
40
+ email:
41
+ - judofyr@gmail.com
42
+ - mail@daniel-mendler.de
40
43
  executables: []
41
44
 
42
45
  extensions: []
@@ -53,7 +56,6 @@ files:
53
56
  - lib/temple/engine.rb
54
57
  - lib/temple/erb/engine.rb
55
58
  - lib/temple/erb/parser.rb
56
- - lib/temple/erb/template.rb
57
59
  - lib/temple/erb/trimming.rb
58
60
  - lib/temple/filter.rb
59
61
  - lib/temple/filters/debugger.rb
@@ -62,10 +64,13 @@ files:
62
64
  - lib/temple/filters/multi_flattener.rb
63
65
  - lib/temple/filters/static_merger.rb
64
66
  - lib/temple/generators.rb
67
+ - lib/temple/html/dispatcher.rb
65
68
  - lib/temple/html/fast.rb
66
69
  - lib/temple/html/pretty.rb
67
70
  - lib/temple/mixins.rb
68
- - lib/temple/template.rb
71
+ - lib/temple/templates.rb
72
+ - lib/temple/templates/rails.rb
73
+ - lib/temple/templates/tilt.rb
69
74
  - lib/temple/utils.rb
70
75
  - lib/temple/version.rb
71
76
  - temple.gemspec
@@ -76,6 +81,7 @@ files:
76
81
  - test/helper.rb
77
82
  - test/html/test_fast.rb
78
83
  - test/html/test_pretty.rb
84
+ - test/test_engine.rb
79
85
  - test/test_erb.rb
80
86
  - test/test_generator.rb
81
87
  - test/test_utils.rb
@@ -115,6 +121,7 @@ test_files:
115
121
  - test/helper.rb
116
122
  - test/html/test_fast.rb
117
123
  - test/html/test_pretty.rb
124
+ - test/test_engine.rb
118
125
  - test/test_erb.rb
119
126
  - test/test_generator.rb
120
127
  - test/test_utils.rb
@@ -1,7 +0,0 @@
1
- module Temple
2
- module ERB
3
- class Template < Temple::Template
4
- engine Temple::ERB::Engine
5
- end
6
- end
7
- end
@@ -1,35 +0,0 @@
1
- require 'tilt'
2
-
3
- module Temple
4
- # Tilt template implementation for Temple
5
- class Template < Tilt::Template
6
- class << self
7
- def engine(engine = nil)
8
- if engine
9
- @engine = engine
10
- elsif @engine
11
- @engine
12
- else
13
- raise 'No engine configured'
14
- end
15
- end
16
- end
17
-
18
- # Prepare Temple template
19
- #
20
- # Called immediately after template data is loaded.
21
- #
22
- # @return [void]
23
- def prepare
24
- @src = self.class.engine.new(options.merge(:file => eval_file)).compile(data)
25
- end
26
-
27
- # A string containing the (Ruby) source code for the template.
28
- #
29
- # @param [Hash] locals Local variables
30
- # @return [String] Compiled template ruby code
31
- def precompiled_template(locals = {})
32
- @src
33
- end
34
- end
35
- end