nested_liquid 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +26 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +36 -0
- data/Rakefile +44 -0
- data/VERSION +1 -0
- data/lib/nested_liquid.rb +50 -0
- data/spec/nested_liquid_spec.rb +105 -0
- data/spec/spec_helper.rb +18 -0
- metadata +217 -0
data/.document
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activesupport (3.0.3)
|
5
|
+
bacon (1.1.0)
|
6
|
+
git (1.2.5)
|
7
|
+
jeweler (1.5.1)
|
8
|
+
bundler (~> 1.0.0)
|
9
|
+
git (>= 1.2.5)
|
10
|
+
rake
|
11
|
+
liquid (2.2.2)
|
12
|
+
rake (0.8.7)
|
13
|
+
rcov (0.9.9)
|
14
|
+
yard (0.6.3)
|
15
|
+
|
16
|
+
PLATFORMS
|
17
|
+
ruby
|
18
|
+
|
19
|
+
DEPENDENCIES
|
20
|
+
activesupport (>= 2.3.0)
|
21
|
+
bacon
|
22
|
+
bundler (~> 1.0.0)
|
23
|
+
jeweler (~> 1.5.1)
|
24
|
+
liquid (>= 2.0.0)
|
25
|
+
rcov
|
26
|
+
yard (~> 0.6.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Mark Turner & G5 Search Marketing
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
= nested_liquid
|
2
|
+
|
3
|
+
Allows you to bypass liquids sanity checks on method calls for particular namespaces as well and render the liquid templates recursively.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install nested_liquid
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
You have to set the allowed namespaces. In the example below I set the allowed namespace to 'address'. (Note: If you're on a rails app, set this in an initializer)
|
12
|
+
|
13
|
+
|
14
|
+
Liquid.allowd_namespaces = "address"
|
15
|
+
|
16
|
+
Now your liquid templates can have code like the following, without error
|
17
|
+
|
18
|
+
{{person.address.combined}}
|
19
|
+
|
20
|
+
And as a bonus the liquid templates can return additional liquid templates and they will be parsed recursively until the liquid context limit (currently 100) is hit.
|
21
|
+
|
22
|
+
== contributing to nested_liquid
|
23
|
+
|
24
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
25
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
26
|
+
* Fork the project
|
27
|
+
* Start a feature/bugfix branch
|
28
|
+
* Commit and push until you are happy with your contribution
|
29
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
30
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
31
|
+
|
32
|
+
== Copyright
|
33
|
+
|
34
|
+
Copyright (c) 2010 Mark Turner & G5 Search Marketing Inc. See LICENSE.txt for
|
35
|
+
further details.
|
36
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "nested_liquid"
|
16
|
+
gem.homepage = "http://github.com/amerine/nested_liquid"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = "Bypass liquids saniziation and render nested liquid templates"
|
19
|
+
gem.description = "Bypass liquids saniziation and render nested liquid templates inside certian namespaces"
|
20
|
+
gem.email = "mark@amerine.net"
|
21
|
+
gem.authors = ["Mark Turner"]
|
22
|
+
gem.add_runtime_dependency 'liquid', '> 2.0.0'
|
23
|
+
gem.add_runtime_dependency 'activesupport', '> 2.3.0'
|
24
|
+
end
|
25
|
+
Jeweler::RubygemsDotOrgTasks.new
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
Rake::TestTask.new(:spec) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.verbose = true
|
32
|
+
end
|
33
|
+
|
34
|
+
require 'rcov/rcovtask'
|
35
|
+
Rcov::RcovTask.new do |spec|
|
36
|
+
spec.libs << 'spec'
|
37
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
38
|
+
spec.verbose = true
|
39
|
+
end
|
40
|
+
|
41
|
+
task :default => :spec
|
42
|
+
|
43
|
+
require 'yard'
|
44
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
2
|
+
|
3
|
+
# nodoc
|
4
|
+
module Liquid
|
5
|
+
mattr_accessor :allowed_namespaces
|
6
|
+
Liquid.allowed_namespaces = []
|
7
|
+
|
8
|
+
# nodoc
|
9
|
+
class Context
|
10
|
+
|
11
|
+
alias_method :liquid_variable, :variable
|
12
|
+
|
13
|
+
# Recursively renders the liquid calls with the same context until all liquid tags have been rendered
|
14
|
+
def render_nested_liquid(liquid_template, context)
|
15
|
+
parsed_template = Liquid::Template.parse(liquid_template)
|
16
|
+
output = parsed_template.render(context)
|
17
|
+
# output.scan(/^#{VariableStart}(.*)#{VariableEnd}$/) do |o|
|
18
|
+
# return render_nested_liquid(o, context)
|
19
|
+
# end
|
20
|
+
output
|
21
|
+
end
|
22
|
+
|
23
|
+
# Determine if we're able to call the method. If not we pass the call back to Liquid's standard method
|
24
|
+
#
|
25
|
+
# This is dependant on the Liquid.allowed_namespaces being set
|
26
|
+
def variable(markup)
|
27
|
+
parts = markup.scan(VariableParser)
|
28
|
+
first_part = parts.shift
|
29
|
+
|
30
|
+
if Liquid.allowed_namespaces.to_a.include? parts[0]
|
31
|
+
#Liquid 2.2 uses environments
|
32
|
+
if @environments
|
33
|
+
@environments.each do |environment|
|
34
|
+
return render_nested_liquid(environment[first_part].send(parts[0]).send(parts[1]), self) if environment.has_key? first_part
|
35
|
+
end
|
36
|
+
else
|
37
|
+
# Liquid < 2.2 uses scopes
|
38
|
+
@scopes.each do |scope|
|
39
|
+
return render_nested_liquid(scope[first_part].send(parts[0]).send(parts[1]), self) if scope.has_key? first_part
|
40
|
+
end
|
41
|
+
end
|
42
|
+
else
|
43
|
+
# If the namespace isn't allowed send the call back to the default Liquid::Context#variable we aliased as liquid_variable above
|
44
|
+
liquid_variable(markup)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
class BaseObject
|
5
|
+
def to_liquid
|
6
|
+
self.instance_variables
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class Person < BaseObject
|
11
|
+
attr_accessor :name, :age
|
12
|
+
attr_accessor :address
|
13
|
+
attr_accessor :friend
|
14
|
+
|
15
|
+
def initialize(name="John", age=21)
|
16
|
+
@name = name
|
17
|
+
@age = age
|
18
|
+
@friend = Friend.new
|
19
|
+
@address = Address.new
|
20
|
+
end
|
21
|
+
|
22
|
+
liquid_methods :name, :age, :friend, :address
|
23
|
+
|
24
|
+
def start_nested
|
25
|
+
"{{person.address.one}}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Friend < BaseObject
|
30
|
+
def cant_touch_this
|
31
|
+
"nah nah nah na"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Address < BaseObject
|
36
|
+
attr_accessor :street, :zip
|
37
|
+
|
38
|
+
def initialize(street="123 St.", zip="97701")
|
39
|
+
@street = street
|
40
|
+
@zip = zip
|
41
|
+
end
|
42
|
+
|
43
|
+
def combined
|
44
|
+
@street +" "+ @zip
|
45
|
+
end
|
46
|
+
|
47
|
+
def street_and_age
|
48
|
+
"{{person.address.street}} {{person.age}}"
|
49
|
+
end
|
50
|
+
|
51
|
+
def one
|
52
|
+
"{{person.address.two}}"
|
53
|
+
end
|
54
|
+
|
55
|
+
def two
|
56
|
+
"{{person.address.three}}"
|
57
|
+
end
|
58
|
+
|
59
|
+
def three
|
60
|
+
"{{person.address.four}}"
|
61
|
+
end
|
62
|
+
|
63
|
+
def four
|
64
|
+
"four"
|
65
|
+
end
|
66
|
+
|
67
|
+
liquid_methods :one, :two, :three, :four
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "NestedLiquid" do
|
71
|
+
before do
|
72
|
+
Liquid.allowed_namespaces = "address"
|
73
|
+
@person = Person.new
|
74
|
+
@template = Liquid::Template.parse("{{person.address.combined}} is far away")
|
75
|
+
end
|
76
|
+
|
77
|
+
it "Sanity check for fake objects" do
|
78
|
+
@person.address.combined.should.eql "123 St. 97701"
|
79
|
+
end
|
80
|
+
|
81
|
+
it "can render nested methods in the allowed namespace" do
|
82
|
+
result = @template.render('person' => @person)
|
83
|
+
result.should.eql "123 St. 97701 is far away"
|
84
|
+
end
|
85
|
+
|
86
|
+
it "shouldn't be able to call methods that are not allowed" do
|
87
|
+
template = Liquid::Template.parse("{{person.friend.cant_touch_this}}")
|
88
|
+
result = template.render('person' => @person)
|
89
|
+
result.should.eql ""
|
90
|
+
end
|
91
|
+
|
92
|
+
it "renders recusive liquid templates" do
|
93
|
+
template = Liquid::Template.parse("{{person.name}} {{person.address.street_and_age}}")
|
94
|
+
result = template.render('person' => @person)
|
95
|
+
result.should.eql "John 123 St. 21"
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
it "renders really nested liquid statements" do
|
100
|
+
template = Liquid::Template.parse("{{person.address.one}}")
|
101
|
+
result = template.render('person' => @person)
|
102
|
+
result.should.eql "four"
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
begin
|
5
|
+
Bundler.setup(:default, :development)
|
6
|
+
rescue Bundler::BundlerError => e
|
7
|
+
$stderr.puts e.message
|
8
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
9
|
+
exit e.status_code
|
10
|
+
end
|
11
|
+
require 'liquid'
|
12
|
+
require 'bacon'
|
13
|
+
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
16
|
+
require 'nested_liquid'
|
17
|
+
|
18
|
+
Bacon.summary_on_exit
|
metadata
ADDED
@@ -0,0 +1,217 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nested_liquid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Mark Turner
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-12-09 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
name: liquid
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 15
|
31
|
+
segments:
|
32
|
+
- 2
|
33
|
+
- 0
|
34
|
+
- 0
|
35
|
+
version: 2.0.0
|
36
|
+
requirement: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
name: activesupport
|
41
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 3
|
47
|
+
segments:
|
48
|
+
- 2
|
49
|
+
- 3
|
50
|
+
- 0
|
51
|
+
version: 2.3.0
|
52
|
+
requirement: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
name: bacon
|
57
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirement: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
name: yard
|
71
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 7
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
- 6
|
80
|
+
- 0
|
81
|
+
version: 0.6.0
|
82
|
+
requirement: *id004
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
type: :development
|
85
|
+
prerelease: false
|
86
|
+
name: bundler
|
87
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ~>
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
hash: 23
|
93
|
+
segments:
|
94
|
+
- 1
|
95
|
+
- 0
|
96
|
+
- 0
|
97
|
+
version: 1.0.0
|
98
|
+
requirement: *id005
|
99
|
+
- !ruby/object:Gem::Dependency
|
100
|
+
type: :development
|
101
|
+
prerelease: false
|
102
|
+
name: jeweler
|
103
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ~>
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
hash: 1
|
109
|
+
segments:
|
110
|
+
- 1
|
111
|
+
- 5
|
112
|
+
- 1
|
113
|
+
version: 1.5.1
|
114
|
+
requirement: *id006
|
115
|
+
- !ruby/object:Gem::Dependency
|
116
|
+
type: :development
|
117
|
+
prerelease: false
|
118
|
+
name: rcov
|
119
|
+
version_requirements: &id007 !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
hash: 3
|
125
|
+
segments:
|
126
|
+
- 0
|
127
|
+
version: "0"
|
128
|
+
requirement: *id007
|
129
|
+
- !ruby/object:Gem::Dependency
|
130
|
+
type: :runtime
|
131
|
+
prerelease: false
|
132
|
+
name: liquid
|
133
|
+
version_requirements: &id008 !ruby/object:Gem::Requirement
|
134
|
+
none: false
|
135
|
+
requirements:
|
136
|
+
- - ">"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
hash: 15
|
139
|
+
segments:
|
140
|
+
- 2
|
141
|
+
- 0
|
142
|
+
- 0
|
143
|
+
version: 2.0.0
|
144
|
+
requirement: *id008
|
145
|
+
- !ruby/object:Gem::Dependency
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
name: activesupport
|
149
|
+
version_requirements: &id009 !ruby/object:Gem::Requirement
|
150
|
+
none: false
|
151
|
+
requirements:
|
152
|
+
- - ">"
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
hash: 3
|
155
|
+
segments:
|
156
|
+
- 2
|
157
|
+
- 3
|
158
|
+
- 0
|
159
|
+
version: 2.3.0
|
160
|
+
requirement: *id009
|
161
|
+
description: Bypass liquids saniziation and render nested liquid templates inside certian namespaces
|
162
|
+
email: mark@amerine.net
|
163
|
+
executables: []
|
164
|
+
|
165
|
+
extensions: []
|
166
|
+
|
167
|
+
extra_rdoc_files:
|
168
|
+
- LICENSE.txt
|
169
|
+
- README.rdoc
|
170
|
+
files:
|
171
|
+
- .document
|
172
|
+
- Gemfile
|
173
|
+
- Gemfile.lock
|
174
|
+
- LICENSE.txt
|
175
|
+
- README.rdoc
|
176
|
+
- Rakefile
|
177
|
+
- VERSION
|
178
|
+
- lib/nested_liquid.rb
|
179
|
+
- spec/nested_liquid_spec.rb
|
180
|
+
- spec/spec_helper.rb
|
181
|
+
has_rdoc: true
|
182
|
+
homepage: http://github.com/amerine/nested_liquid
|
183
|
+
licenses:
|
184
|
+
- MIT
|
185
|
+
post_install_message:
|
186
|
+
rdoc_options: []
|
187
|
+
|
188
|
+
require_paths:
|
189
|
+
- lib
|
190
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
191
|
+
none: false
|
192
|
+
requirements:
|
193
|
+
- - ">="
|
194
|
+
- !ruby/object:Gem::Version
|
195
|
+
hash: 3
|
196
|
+
segments:
|
197
|
+
- 0
|
198
|
+
version: "0"
|
199
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
200
|
+
none: false
|
201
|
+
requirements:
|
202
|
+
- - ">="
|
203
|
+
- !ruby/object:Gem::Version
|
204
|
+
hash: 3
|
205
|
+
segments:
|
206
|
+
- 0
|
207
|
+
version: "0"
|
208
|
+
requirements: []
|
209
|
+
|
210
|
+
rubyforge_project:
|
211
|
+
rubygems_version: 1.3.7
|
212
|
+
signing_key:
|
213
|
+
specification_version: 3
|
214
|
+
summary: Bypass liquids saniziation and render nested liquid templates
|
215
|
+
test_files:
|
216
|
+
- spec/nested_liquid_spec.rb
|
217
|
+
- spec/spec_helper.rb
|