agilitic-liquid 2.0.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.
@@ -0,0 +1,20 @@
1
+ module Liquid
2
+ class Ifchanged < Block
3
+
4
+ def render(context)
5
+ context.stack do
6
+
7
+ output = render_all(@nodelist, context)
8
+
9
+ if output != context.registers[:ifchanged]
10
+ context.registers[:ifchanged] = output
11
+ output
12
+ else
13
+ ''
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ Template.register_tag('ifchanged', Ifchanged)
20
+ end
@@ -0,0 +1,55 @@
1
+ module Liquid
2
+ class Include < Tag
3
+ Syntax = /(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?/
4
+
5
+ def initialize(tag_name, markup, tokens)
6
+ if markup =~ Syntax
7
+
8
+ @template_name = $1
9
+ @variable_name = $3
10
+ @attributes = {}
11
+
12
+ markup.scan(TagAttributes) do |key, value|
13
+ @attributes[key] = value
14
+ end
15
+
16
+ else
17
+ raise SyntaxError.new("Error in tag 'include' - Valid syntax: include '[template]' (with|for) [object|collection]")
18
+ end
19
+
20
+ super
21
+ end
22
+
23
+ def parse(tokens)
24
+ end
25
+
26
+ def render(context)
27
+ source = Liquid::Template.file_system.read_template_file(context[@template_name])
28
+ partial = Liquid::Template.parse(source)
29
+
30
+ variable = context[@variable_name || @template_name[1..-2]]
31
+
32
+ context.stack do
33
+ @attributes.each do |key, value|
34
+ context[key] = context[value]
35
+ end
36
+
37
+ if variable.is_a?(Array)
38
+
39
+ variable.collect do |variable|
40
+ context[@template_name[1..-2]] = variable
41
+ partial.render(context)
42
+ end
43
+
44
+ else
45
+
46
+ context[@template_name[1..-2]] = variable
47
+ partial.render(context)
48
+
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ Template.register_tag('include', Include)
55
+ end
@@ -0,0 +1,33 @@
1
+ require File.dirname(__FILE__) + '/if'
2
+
3
+ module Liquid
4
+
5
+ # Unless is a conditional just like 'if' but works on the inverse logic.
6
+ #
7
+ # {% unless x < 0 %} x is greater than zero {% end %}
8
+ #
9
+ class Unless < If
10
+ def render(context)
11
+ context.stack do
12
+
13
+ # First condition is interpreted backwards ( if not )
14
+ block = @blocks.first
15
+ unless block.evaluate(context)
16
+ return render_all(block.attachment, context)
17
+ end
18
+
19
+ # After the first condition unless works just like if
20
+ @blocks[1..-1].each do |block|
21
+ if block.evaluate(context)
22
+ return render_all(block.attachment, context)
23
+ end
24
+ end
25
+
26
+ ''
27
+ end
28
+ end
29
+ end
30
+
31
+
32
+ Template.register_tag('unless', Unless)
33
+ end
@@ -0,0 +1,147 @@
1
+ module Liquid
2
+
3
+ # Templates are central to liquid.
4
+ # Interpretating templates is a two step process. First you compile the
5
+ # source code you got. During compile time some extensive error checking is performed.
6
+ # your code should expect to get some SyntaxErrors.
7
+ #
8
+ # After you have a compiled template you can then <tt>render</tt> it.
9
+ # You can use a compiled template over and over again and keep it cached.
10
+ #
11
+ # Example:
12
+ #
13
+ # template = Liquid::Template.parse(source)
14
+ # template.render('user_name' => 'bob')
15
+ #
16
+ class Template
17
+ attr_accessor :root
18
+ @@file_system = BlankFileSystem.new
19
+
20
+ class << self
21
+ def file_system
22
+ @@file_system
23
+ end
24
+
25
+ def file_system=(obj)
26
+ @@file_system = obj
27
+ end
28
+
29
+ def register_tag(name, klass)
30
+ tags[name.to_s] = klass
31
+ end
32
+
33
+ def tags
34
+ @tags ||= {}
35
+ end
36
+
37
+ # Pass a module with filter methods which should be available
38
+ # to all liquid views. Good for registering the standard library
39
+ def register_filter(mod)
40
+ Strainer.global_filter(mod)
41
+ end
42
+
43
+ # creates a new <tt>Template</tt> object from liquid source code
44
+ def parse(source)
45
+ template = Template.new
46
+ template.parse(source)
47
+ template
48
+ end
49
+ end
50
+
51
+ # creates a new <tt>Template</tt> from an array of tokens. Use <tt>Template.parse</tt> instead
52
+ def initialize
53
+ end
54
+
55
+ # Parse source code.
56
+ # Returns self for easy chaining
57
+ def parse(source)
58
+ @root = Document.new(tokenize(source))
59
+ self
60
+ end
61
+
62
+ def registers
63
+ @registers ||= {}
64
+ end
65
+
66
+ def assigns
67
+ @assigns ||= {}
68
+ end
69
+
70
+ def errors
71
+ @errors ||= []
72
+ end
73
+
74
+ # Render takes a hash with local variables.
75
+ #
76
+ # if you use the same filters over and over again consider registering them globally
77
+ # with <tt>Template.register_filter</tt>
78
+ #
79
+ # Following options can be passed:
80
+ #
81
+ # * <tt>filters</tt> : array with local filters
82
+ # * <tt>registers</tt> : hash with register variables. Those can be accessed from
83
+ # filters and tags and might be useful to integrate liquid more with its host application
84
+ #
85
+ def render(*args)
86
+ return '' if @root.nil?
87
+
88
+ context = case args.first
89
+ when Liquid::Context
90
+ args.shift
91
+ when Hash
92
+ a = args.shift
93
+ assigns.each { |k,v| a[k] = v unless a.has_key?(k) }
94
+ Context.new(a, registers, @rethrow_errors)
95
+ when nil
96
+ Context.new(assigns.dup, registers, @rethrow_errors)
97
+ else
98
+ raise ArgumentError, "Expect Hash or Liquid::Context as parameter"
99
+ end
100
+
101
+ case args.last
102
+ when Hash
103
+ options = args.pop
104
+
105
+ if options[:registers].is_a?(Hash)
106
+ self.registers.merge!(options[:registers])
107
+ end
108
+
109
+ if options[:filters]
110
+ context.add_filters(options[:filters])
111
+ end
112
+
113
+ when Module
114
+ context.add_filters(args.pop)
115
+ when Array
116
+ context.add_filters(args.pop)
117
+ end
118
+
119
+ begin
120
+ # render the nodelist.
121
+ # for performance reasons we get a array back here. join will make a string out of it
122
+ @root.render(context).join
123
+ ensure
124
+ @errors = context.errors
125
+ end
126
+ end
127
+
128
+ def render!(*args)
129
+ @rethrow_errors = true; render(*args)
130
+ end
131
+
132
+ private
133
+
134
+ # Uses the <tt>Liquid::TemplateParser</tt> regexp to tokenize the passed source
135
+ def tokenize(source)
136
+ source = source.source if source.respond_to?(:source)
137
+ return [] if source.to_s.empty?
138
+ tokens = source.split(TemplateParser)
139
+
140
+ # removes the rogue empty element at the beginning of the array
141
+ tokens.shift if tokens[0] and tokens[0].empty?
142
+
143
+ tokens
144
+ end
145
+
146
+ end
147
+ end
@@ -0,0 +1,54 @@
1
+ require 'pp'
2
+
3
+ module Liquid
4
+
5
+ # Holds variables. Variables are only loaded "just in time"
6
+ # and are not evaluated as part of the render stage
7
+ #
8
+ # {{ monkey }}
9
+ # {{ user.name }}
10
+ #
11
+ # Variables can be combined with filters:
12
+ #
13
+ # {{ user | link }}
14
+ #
15
+ class Variable
16
+ attr_accessor :filters, :name
17
+
18
+ def initialize(markup)
19
+ @markup = markup
20
+ @name = nil
21
+ @filters = []
22
+ if match = markup.match(/\s*(#{QuotedFragment})/)
23
+ @name = match[1]
24
+ if markup.match(/#{FilterSeparator}\s*(.*)/)
25
+ filters = Regexp.last_match(1).split(/#{FilterSeparator}/)
26
+ filters.each do |f|
27
+ if matches = f.match(/\s*(\w+)/)
28
+ filtername = matches[1]
29
+ filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*(#{QuotedFragment})/).flatten
30
+ @filters << [filtername.to_sym, filterargs]
31
+ end
32
+ end
33
+ end
34
+ end
35
+ @filters << [:escape, []] unless @filters.detect {|f| f.first == :raw }
36
+ end
37
+
38
+ def render(context)
39
+ return '' if @name.nil?
40
+ @filters.inject(context[@name]) do |output, filter|
41
+ filterargs = filter[1].to_a.collect do |a|
42
+ context[a]
43
+ end
44
+ begin
45
+ context.invoke(filter[0], output, *filterargs)
46
+ rescue FilterNotFound
47
+ raise FilterNotFound, "Error - filter '#{filter[0]}' in '#{@markup.strip}' could not be found."
48
+ end
49
+ end
50
+ end
51
+
52
+
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: agilitic-liquid
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tobias Luetke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-13 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A secure non evaling end user template engine with aesthetic markup.
17
+ email: tobi@leetsoft.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - History.txt
24
+ - Manifest.txt
25
+ - README.txt
26
+ files:
27
+ - CHANGELOG
28
+ - History.txt
29
+ - MIT-LICENSE
30
+ - Manifest.txt
31
+ - README.txt
32
+ - Rakefile
33
+ - lib/extras/liquid_view.rb
34
+ - lib/liquid.rb
35
+ - lib/liquid/block.rb
36
+ - lib/liquid/condition.rb
37
+ - lib/liquid/context.rb
38
+ - lib/liquid/document.rb
39
+ - lib/liquid/drop.rb
40
+ - lib/liquid/errors.rb
41
+ - lib/liquid/extensions.rb
42
+ - lib/liquid/file_system.rb
43
+ - lib/liquid/htmltags.rb
44
+ - lib/liquid/module_ex.rb
45
+ - lib/liquid/standardfilters.rb
46
+ - lib/liquid/strainer.rb
47
+ - lib/liquid/tag.rb
48
+ - lib/liquid/tags/assign.rb
49
+ - lib/liquid/tags/capture.rb
50
+ - lib/liquid/tags/case.rb
51
+ - lib/liquid/tags/comment.rb
52
+ - lib/liquid/tags/cycle.rb
53
+ - lib/liquid/tags/for.rb
54
+ - lib/liquid/tags/if.rb
55
+ - lib/liquid/tags/ifchanged.rb
56
+ - lib/liquid/tags/include.rb
57
+ - lib/liquid/tags/unless.rb
58
+ - lib/liquid/template.rb
59
+ - lib/liquid/variable.rb
60
+ has_rdoc: true
61
+ homepage: http://www.liquidmarkup.org
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --main
67
+ - README.txt
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ requirements: []
83
+
84
+ rubyforge_project: liquid
85
+ rubygems_version: 1.3.5
86
+ signing_key:
87
+ specification_version: 2
88
+ summary: A secure non evaling end user template engine with aesthetic markup.
89
+ test_files: []
90
+