faithfulgeek-haml_splash 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Joe Fiorini
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,7 @@
1
+ = haml_splash
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Joe Fiorini. See LICENSE for details.
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "haml_splash"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "joe@faithfulgeek.org"
10
+ gem.homepage = "http://github.com/faithfulgeek/haml_splash"
11
+ gem.authors = ["Joe Fiorini"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "haml_splash #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,52 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{haml_splash}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Joe Fiorini"]
9
+ s.date = %q{2009-06-22}
10
+ s.email = %q{joe@faithfulgeek.org}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "haml_splash.gemspec",
23
+ "lib/core_ext/hash.rb",
24
+ "lib/haml/splash.rb",
25
+ "lib/haml_splash.rb",
26
+ "lib/haml_splash/haml_hacks.rb",
27
+ "lib/haml_splash/liquid_hacks.rb",
28
+ "test/haml_liquid_if_tags_test.rb",
29
+ "test/haml_splash_test.rb",
30
+ "test/test_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/faithfulgeek/haml_splash}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.3}
36
+ s.summary = %q{TODO}
37
+ s.test_files = [
38
+ "test/haml_liquid_if_tags_test.rb",
39
+ "test/haml_splash_test.rb",
40
+ "test/test_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ else
49
+ end
50
+ else
51
+ end
52
+ end
@@ -0,0 +1,13 @@
1
+ class Hash
2
+ def stringify_keys
3
+ inject({}) do |acc,(key, value)|
4
+ if value.is_a?(Hash)
5
+ acc[key.to_s] = value.stringify_keys
6
+ else
7
+ acc[key.to_s] = value
8
+ end
9
+ acc
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,3 @@
1
+ require 'haml'
2
+ require 'liquid'
3
+ require File.dirname(__FILE__) + '/../haml_splash'
@@ -0,0 +1,10 @@
1
+ module HamlSplash
2
+ def self.register!
3
+ HamlSplash::HamlHacks.register!
4
+ HamlSplash::LiquidHacks.register!
5
+ end
6
+ end
7
+
8
+ require File.dirname(__FILE__) + '/core_ext/hash'
9
+ require File.dirname(__FILE__) + '/haml_splash/haml_hacks'
10
+ require File.dirname(__FILE__) + '/haml_splash/liquid_hacks'
@@ -0,0 +1,75 @@
1
+ class HamlSplash::HamlHacks
2
+
3
+ def self.register!
4
+ hacker = self.new
5
+ hacker.override_push_script!
6
+ hacker.override_push_silent!
7
+ hacker.create_render_liquid_method!
8
+ end
9
+
10
+ def override_push_script!
11
+ Haml::Precompiler.module_eval do
12
+ def push_script(text, preserve_script, in_tag = false,
13
+ preserve_tag = false, escape_html = false,
14
+ nuke_inner_whitespace = false)
15
+ push_merged_text '' unless in_tag
16
+
17
+ flush_merged_text
18
+ newline_now
19
+ if block_opened?
20
+ push_and_tabulate([:loud, out])
21
+ else
22
+ @precompiled << "_hamlout.push_script('{{#{text.lstrip}}}', [])"
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def override_push_silent!
29
+ Haml::Precompiler.module_eval do
30
+ def push_silent(text, can_suppress = false)
31
+ flush_merged_text
32
+ @precompiled << "{% #{text.lstrip} %}"
33
+ end
34
+ end
35
+ end
36
+
37
+ def create_render_liquid_method!
38
+ Haml::Engine.class_eval do
39
+ def render_liquid(locals={}, &block)
40
+ scope = Object.new
41
+ buffer = Haml::Buffer.new(scope, options_for_buffer)
42
+
43
+ if scope.is_a?(Binding) || scope.is_a?(Proc)
44
+ scope_object = eval("self", scope)
45
+ scope = scope_object.instance_eval{binding} if block_given?
46
+ else
47
+ scope_object = scope
48
+ scope = scope_object.instance_eval{binding}
49
+ end
50
+
51
+
52
+ set_locals(locals.merge(:_hamlout => buffer, :_erbout => buffer.buffer), scope, scope_object)
53
+ scope_object.instance_eval do
54
+ extend Haml::Helpers
55
+ @haml_buffer = buffer
56
+ end
57
+
58
+ template = Liquid::Template.parse(@precompiled)
59
+ output = template.render(locals.stringify_keys)
60
+ eval(output, scope)
61
+
62
+ # Get rid of the current buffer
63
+ scope_object.instance_eval do
64
+ @haml_buffer = buffer.upper
65
+ end
66
+
67
+ buffer.buffer
68
+
69
+ end
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+
@@ -0,0 +1,15 @@
1
+ class HamlSplash::LiquidHacks
2
+ def self.register!
3
+ hacker = self.new
4
+ hacker.override_block_delimiter!
5
+ end
6
+ def override_block_delimiter!
7
+ Liquid::Block.class_eval do
8
+ def block_delimiter
9
+ "end"
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+
@@ -0,0 +1,195 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class HamlLiquidTagsTest < Test::Unit::TestCase
4
+ context 'If we hack Haml' do
5
+ setup do
6
+ HamlSplash.register!
7
+ end
8
+
9
+ context "when evaluating Liquid {% if ... %} tags" do
10
+
11
+ should 'not display anything for false' do
12
+ t = haml("- if false\n this text should not go in output")
13
+ assert t == ""
14
+ end
15
+
16
+ should 'display text for true' do
17
+ t = haml("- if true\n this text should go in output")
18
+ assert t == "this text should go in output\n"
19
+ end
20
+
21
+ should 'display text for true with two different if statements' do
22
+ t = haml("- if false\n you suck\n- if true\n you rock\n?")
23
+ assert t == "you rock\n?\n"
24
+ end
25
+ end
26
+
27
+ context "when evaluating Liquid {% if ... %} ... {% else %} ... tags" do
28
+
29
+ should 'branch to else when if clause is false' do
30
+ t = haml("- if false\n NO\n- else\n YES")
31
+ assert t == "YES\n"
32
+ end
33
+
34
+ should 'use if clause when else clause is false' do
35
+ t = haml("- if true\n YES\n- else\n NO")
36
+ assert t == "YES\n"
37
+ end
38
+
39
+ should 'use if clause with non-null value when else clause is false' do
40
+ t = haml("- if \"foo\"\n YES\n- else\n NO")
41
+ assert t == "YES\n"
42
+ end
43
+
44
+ end
45
+
46
+ context 'when evaluating with a boolean variable' do
47
+
48
+ should 'use if clause with true local variable' do
49
+ t = haml("- if var\n YES", :var => true)
50
+ assert t == "YES\n"
51
+ end
52
+
53
+ end
54
+
55
+ should 'evaluate with a boolean "or"' do
56
+ t = "- if a or b\n YES"
57
+ assert haml(t, :a => true, :b => true) == "YES\n"
58
+ assert haml(t, :a => true, :b => false) == "YES\n"
59
+ assert haml(t, :a => false, :b => true) == "YES\n"
60
+ assert haml(t, :a => false, :b => false) == ""
61
+
62
+ t = "- if a or b or c\n YES"
63
+ assert haml(t, :a => false, :b => false, :c => true) == "YES\n"
64
+ assert haml(t, :a => false, :b => false, :c => false) == ""
65
+ end
66
+
67
+ should 'evaluate with a boolean "or" with operators' do
68
+ assert haml("- if a == true or b == true\n YES", :a => true, :b => true) == "YES\n"
69
+ assert haml("- if a == true or b == false\n YES", :a => true, :b => true) == "YES\n"
70
+ assert haml("- if a == false or b == false\n YES", :a => true, :b => true) == ""
71
+ end
72
+
73
+ should 'evaluate with a boolean "and"' do
74
+ assert haml("- if true and true\n YES") == "YES\n"
75
+ assert haml("- if false and true\n YES") == ""
76
+ assert haml("- if false and false\n YES") == ""
77
+ end
78
+
79
+ should 'evaluate false with an empty hash' do
80
+ assert haml("- if foo.bar\n NO", :foo => {}) == ""
81
+ end
82
+
83
+ should 'evaluate if statements from a variable' do
84
+ assert haml("- if var\n NO", :var => false) == ''
85
+ assert haml("- if var\n NO", :var => nil) == ''
86
+ assert haml("- if foo.bar\n NO", :foo => {:bar => false}) == ''
87
+ assert haml("- if foo.bar\n NO", :foo => {}) == ''
88
+ assert haml("- if foo.bar\n NO", :foo => nil) == ''
89
+ assert haml("- if foo.bar\n NO", :foo => true) == ''
90
+
91
+ assert haml("- if var\n YES", :var => "text") == "YES\n"
92
+ assert haml("- if var\n YES", :var => true) == "YES\n"
93
+ assert haml("- if var\n YES", :var => 1) == "YES\n"
94
+ assert haml("- if var\n YES", :var => {}) == "YES\n"
95
+ assert haml("- if var\n YES", :var => []) == "YES\n"
96
+ assert haml("- if 'foo'\n YES") == "YES\n"
97
+ assert haml("- if foo.bar\n YES", :foo => {:bar => true}) == "YES\n"
98
+ assert haml("- if foo.bar\n YES", :foo => {:bar => "text"}) == "YES\n"
99
+ assert haml("- if foo.bar\n YES", :foo => {:bar => 1 }) == "YES\n"
100
+ assert haml("- if foo.bar\n YES", :foo => {:bar => {} }) == "YES\n"
101
+ assert haml("- if foo.bar\n YES", :foo => {:bar => [] }) == "YES\n"
102
+
103
+ assert haml("- if var\n NO\n- else\n YES", :var => false) == "YES\n"
104
+ assert haml("- if var\n NO\n- else\n YES", :var => nil) == "YES\n"
105
+ assert haml("- if var\n YES\n- else\n NO", :var => true) == "YES\n"
106
+ assert haml("- if 'foo'\n YES\n- else\n NO", :var => "text") == "YES\n"
107
+
108
+ assert haml("- if foo.bar\n NO\n- else\n YES",
109
+ :foo => {:bar => false}) == "YES\n"
110
+ assert haml("- if foo.bar\n YES\n- else\n NO",
111
+ :foo => {:bar => true}) == "YES\n"
112
+ assert haml("- if foo.bar\n YES\n- else\n NO",
113
+ :foo => {:bar => "text"}) == "YES\n"
114
+ assert haml("- if foo.bar\n NO\n- else\n YES",
115
+ :foo => {:notbar => true}) == "YES\n"
116
+ assert haml("- if foo.bar\n NO\n- else\n YES",
117
+ :foo => {}) == "YES\n"
118
+ assert haml("- if foo.bar\n NO\n- else\n YES",
119
+ :notfoo => {:bar => true}) == "YES\n"
120
+
121
+ end
122
+
123
+ should 'evaluate nested if statements' do
124
+ assert haml("- if false\n - if false\n NO") == ""
125
+ assert haml("- if false\n - if true\n NO") == ""
126
+ assert haml("- if true\n - if false\n NO") == ""
127
+ assert haml(<<-EHAML) == "YES\n"
128
+ - if true
129
+ - if true
130
+ YES
131
+ EHAML
132
+ assert haml(<<-EHAML) =="YES\n"
133
+ - if true
134
+ - if true
135
+ YES
136
+ - else
137
+ NO
138
+ - else
139
+ NO
140
+ EHAML
141
+ assert haml(<<-EHAML) == "YES\n"
142
+ - if false
143
+ - if true
144
+ NO
145
+ - else
146
+ NONO
147
+ - else
148
+ YES
149
+ EHAML
150
+ assert haml(<<-EHAML) == "YES\n"
151
+ - if false
152
+ - if true
153
+ NO
154
+ - else
155
+ NONO
156
+ - else
157
+ YES
158
+ EHAML
159
+ end
160
+
161
+ should 'evaluate comparisons on null' do
162
+ assert haml("- if null < 10\n NO") == ""
163
+ assert haml("- if null <= 10\n NO") == ""
164
+ assert haml("- if null >= 10\n NO") == ""
165
+ assert haml("- if null > 10\n NO") == ""
166
+
167
+ assert haml("- if 10 < null\n NO") == ""
168
+ assert haml("- if 10 <= null\n NO") == ""
169
+ assert haml("- if 10 >= null\n NO") == ""
170
+ assert haml("- if 10 > null\n NO") == ""
171
+ end
172
+
173
+ should 'evaluate Liquid elseif' do
174
+ assert haml("- if 0 == 0\n 0\n- elsif 1 == 1\n 1\n- else\n 2") == "0\n"
175
+ assert haml("- if 0 != 0\n 0\n- elsif 1 == 1\n 1\n- else\n 2") == "1\n"
176
+ assert haml("- if 0 != 0\n 0\n- elsif 1 != 1\n 1\n- else\n 2") == "2\n"
177
+ assert haml("- if false\n if\n- elsif true\n elsif") == "elsif\n"
178
+ end
179
+
180
+ should 'raise syntax error with no variable' do
181
+ assert_raise(Liquid::SyntaxError) { haml("- if jerry == 1") }
182
+ end
183
+
184
+ should 'raise syntax error with no expression' do
185
+ assert_raise(Liquid::SyntaxError) { haml("- if") }
186
+ end
187
+
188
+ should 'evaluate with custom conditions' do
189
+ Liquid::Condition.operators['contains'] = :[]
190
+ assert haml("- if 'bob' contains 'o'\n YES") == "YES\n"
191
+ assert haml("- if 'bob' contains 'f'\n NO") == ""
192
+ end
193
+ end
194
+ end
195
+
@@ -0,0 +1,56 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class HamlTestsRenderWithLiquid < Test::Unit::TestCase
4
+ context 'If we hack Haml' do
5
+ setup do
6
+ HamlSplash.register!
7
+ end
8
+
9
+ context 'render_liquid' do
10
+ should 'render Hello World with liquid' do
11
+ assert haml("= \"Hello World\"") == "Hello World\n"
12
+ end
13
+
14
+ should 'render Hello World with liquid filter' do
15
+ assert haml("= \"hello world\" | upcase") == "HELLO WORLD\n"
16
+ end
17
+
18
+ should 'render with variable' do
19
+ t = haml("= name", :name => "joe")
20
+ assert t == "joe\n"
21
+ end
22
+
23
+ should 'not evaluate Ruby' do
24
+ assert_raises(Liquid::SyntaxError) do
25
+ haml("- [1,2,3,4,5].each do |i|\n = i")
26
+ end
27
+ end
28
+
29
+ should 'still evaluate html' do
30
+ assert haml("%h1 Hello World") == "<h1>Hello World</h1>\n"
31
+ end
32
+
33
+ should 'evaluate script with html' do
34
+ assert haml("%h1= \"Hello World\"") == "<h1>Hello World\n</h1>\n"
35
+ end
36
+
37
+ should 'evaluate script with variable' do
38
+ t = haml("%h1= name", :name => 'joe')
39
+ assert t == "<h1>joe\n</h1>\n"
40
+ end
41
+
42
+ should 'evaluate script with html and filter' do
43
+ t = haml("%h1= name | capitalize", :name => 'joe')
44
+ assert t == "<h1>Joe\n</h1>\n"
45
+ end
46
+
47
+ should 'evaluate liquid {% for . in ... %} tag' do
48
+ assert haml("- for i in array\n = i", :array => [1,2,3,4,5]) ==
49
+ "1\n2\n3\n4\n5\n"
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'haml/splash'
8
+
9
+ class Test::Unit::TestCase
10
+ def haml(t, locals={})
11
+ Haml::Engine.new(t).render_liquid(locals)
12
+ end
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faithfulgeek-haml_splash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joe Fiorini
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: joe@faithfulgeek.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - haml_splash.gemspec
33
+ - lib/core_ext/hash.rb
34
+ - lib/haml/splash.rb
35
+ - lib/haml_splash.rb
36
+ - lib/haml_splash/haml_hacks.rb
37
+ - lib/haml_splash/liquid_hacks.rb
38
+ - test/haml_liquid_if_tags_test.rb
39
+ - test/haml_splash_test.rb
40
+ - test/test_helper.rb
41
+ has_rdoc: false
42
+ homepage: http://github.com/faithfulgeek/haml_splash
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: TODO
67
+ test_files:
68
+ - test/haml_liquid_if_tags_test.rb
69
+ - test/haml_splash_test.rb
70
+ - test/test_helper.rb