drnic-liquid 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. data/CHANGELOG +44 -0
  2. data/History.txt +44 -0
  3. data/MIT-LICENSE +20 -0
  4. data/Manifest.txt +34 -0
  5. data/README.rdoc +44 -0
  6. data/Rakefile +30 -0
  7. data/example/server/example_servlet.rb +37 -0
  8. data/example/server/liquid_servlet.rb +28 -0
  9. data/example/server/server.rb +12 -0
  10. data/example/server/templates/index.liquid +6 -0
  11. data/example/server/templates/products.liquid +45 -0
  12. data/init.rb +8 -0
  13. data/lib/extras/liquid_view.rb +51 -0
  14. data/lib/liquid.rb +70 -0
  15. data/lib/liquid/block.rb +102 -0
  16. data/lib/liquid/condition.rb +120 -0
  17. data/lib/liquid/context.rb +221 -0
  18. data/lib/liquid/document.rb +17 -0
  19. data/lib/liquid/drop.rb +51 -0
  20. data/lib/liquid/errors.rb +11 -0
  21. data/lib/liquid/extensions.rb +56 -0
  22. data/lib/liquid/file_system.rb +62 -0
  23. data/lib/liquid/htmltags.rb +74 -0
  24. data/lib/liquid/module_ex.rb +62 -0
  25. data/lib/liquid/standardfilters.rb +209 -0
  26. data/lib/liquid/strainer.rb +51 -0
  27. data/lib/liquid/tag.rb +26 -0
  28. data/lib/liquid/tags/assign.rb +33 -0
  29. data/lib/liquid/tags/capture.rb +35 -0
  30. data/lib/liquid/tags/case.rb +83 -0
  31. data/lib/liquid/tags/comment.rb +9 -0
  32. data/lib/liquid/tags/cycle.rb +59 -0
  33. data/lib/liquid/tags/for.rb +136 -0
  34. data/lib/liquid/tags/if.rb +79 -0
  35. data/lib/liquid/tags/ifchanged.rb +20 -0
  36. data/lib/liquid/tags/include.rb +55 -0
  37. data/lib/liquid/tags/unless.rb +33 -0
  38. data/lib/liquid/template.rb +147 -0
  39. data/lib/liquid/variable.rb +49 -0
  40. data/liquid.gemspec +40 -0
  41. data/performance/shopify.rb +92 -0
  42. data/performance/shopify/comment_form.rb +33 -0
  43. data/performance/shopify/database.rb +45 -0
  44. data/performance/shopify/json_filter.rb +7 -0
  45. data/performance/shopify/liquid.rb +18 -0
  46. data/performance/shopify/money_filter.rb +18 -0
  47. data/performance/shopify/paginate.rb +93 -0
  48. data/performance/shopify/shop_filter.rb +98 -0
  49. data/performance/shopify/tag_filter.rb +25 -0
  50. data/performance/shopify/vision.database.yml +945 -0
  51. data/performance/shopify/weight_filter.rb +11 -0
  52. data/performance/tests/dropify/article.liquid +74 -0
  53. data/performance/tests/dropify/blog.liquid +33 -0
  54. data/performance/tests/dropify/cart.liquid +66 -0
  55. data/performance/tests/dropify/collection.liquid +22 -0
  56. data/performance/tests/dropify/index.liquid +47 -0
  57. data/performance/tests/dropify/page.liquid +8 -0
  58. data/performance/tests/dropify/product.liquid +68 -0
  59. data/performance/tests/dropify/theme.liquid +105 -0
  60. data/performance/tests/ripen/article.liquid +74 -0
  61. data/performance/tests/ripen/blog.liquid +13 -0
  62. data/performance/tests/ripen/cart.liquid +54 -0
  63. data/performance/tests/ripen/collection.liquid +29 -0
  64. data/performance/tests/ripen/index.liquid +32 -0
  65. data/performance/tests/ripen/page.liquid +4 -0
  66. data/performance/tests/ripen/product.liquid +75 -0
  67. data/performance/tests/ripen/theme.liquid +85 -0
  68. data/performance/tests/tribble/404.liquid +56 -0
  69. data/performance/tests/tribble/article.liquid +98 -0
  70. data/performance/tests/tribble/blog.liquid +41 -0
  71. data/performance/tests/tribble/cart.liquid +134 -0
  72. data/performance/tests/tribble/collection.liquid +70 -0
  73. data/performance/tests/tribble/index.liquid +94 -0
  74. data/performance/tests/tribble/page.liquid +56 -0
  75. data/performance/tests/tribble/product.liquid +116 -0
  76. data/performance/tests/tribble/search.liquid +51 -0
  77. data/performance/tests/tribble/theme.liquid +90 -0
  78. data/performance/tests/vogue/article.liquid +66 -0
  79. data/performance/tests/vogue/blog.liquid +32 -0
  80. data/performance/tests/vogue/cart.liquid +58 -0
  81. data/performance/tests/vogue/collection.liquid +19 -0
  82. data/performance/tests/vogue/index.liquid +22 -0
  83. data/performance/tests/vogue/page.liquid +3 -0
  84. data/performance/tests/vogue/product.liquid +62 -0
  85. data/performance/tests/vogue/theme.liquid +122 -0
  86. data/test/assign_test.rb +11 -0
  87. data/test/block_test.rb +58 -0
  88. data/test/condition_test.rb +109 -0
  89. data/test/context_test.rb +482 -0
  90. data/test/drop_test.rb +162 -0
  91. data/test/error_handling_test.rb +89 -0
  92. data/test/extra/breakpoint.rb +547 -0
  93. data/test/extra/caller.rb +80 -0
  94. data/test/file_system_test.rb +30 -0
  95. data/test/filter_test.rb +95 -0
  96. data/test/helper.rb +20 -0
  97. data/test/html_tag_test.rb +31 -0
  98. data/test/if_else_test.rb +131 -0
  99. data/test/include_tag_test.rb +115 -0
  100. data/test/module_ex_test.rb +89 -0
  101. data/test/output_test.rb +121 -0
  102. data/test/parsing_quirks_test.rb +41 -0
  103. data/test/regexp_test.rb +45 -0
  104. data/test/security_test.rb +41 -0
  105. data/test/standard_filter_test.rb +161 -0
  106. data/test/standard_tag_test.rb +400 -0
  107. data/test/statements_test.rb +137 -0
  108. data/test/strainer_test.rb +21 -0
  109. data/test/template_test.rb +26 -0
  110. data/test/test_helper.rb +20 -0
  111. data/test/unless_else_test.rb +27 -0
  112. data/test/variable_test.rb +172 -0
  113. metadata +187 -0
@@ -0,0 +1,51 @@
1
+ module Liquid
2
+
3
+ # A drop in liquid is a class which allows you to to export DOM like things to liquid
4
+ # Methods of drops are callable.
5
+ # The main use for liquid drops is the implement lazy loaded objects.
6
+ # If you would like to make data available to the web designers which you don't want loaded unless needed then
7
+ # a drop is a great way to do that
8
+ #
9
+ # Example:
10
+ #
11
+ # class ProductDrop < Liquid::Drop
12
+ # def top_sales
13
+ # Shop.current.products.find(:all, :order => 'sales', :limit => 10 )
14
+ # end
15
+ # end
16
+ #
17
+ # tmpl = Liquid::Template.parse( ' {% for product in product.top_sales %} {{ product.name }} {%end%} ' )
18
+ # tmpl.render('product' => ProductDrop.new ) # will invoke top_sales query.
19
+ #
20
+ # Your drop can either implement the methods sans any parameters or implement the before_method(name) method which is a
21
+ # catch all
22
+ class Drop
23
+ attr_writer :context
24
+
25
+ # Catch all for the method
26
+ def before_method(method)
27
+ nil
28
+ end
29
+
30
+ # called by liquid to invoke a drop
31
+ def invoke_drop(method)
32
+ # for backward compatibility with Ruby 1.8
33
+ methods = self.class.public_instance_methods.map { |m| m.to_s }
34
+ if methods.include?(method.to_s)
35
+ send(method.to_sym)
36
+ else
37
+ before_method(method)
38
+ end
39
+ end
40
+
41
+ def has_key?(name)
42
+ true
43
+ end
44
+
45
+ def to_liquid
46
+ self
47
+ end
48
+
49
+ alias :[] :invoke_drop
50
+ end
51
+ end
@@ -0,0 +1,11 @@
1
+ module Liquid
2
+ class Error < ::StandardError; end
3
+
4
+ class ArgumentError < Error; end
5
+ class ContextError < Error; end
6
+ class FilterNotFound < Error; end
7
+ class FileSystemError < Error; end
8
+ class StandardError < Error; end
9
+ class SyntaxError < Error; end
10
+ class StackLevelError < Error; end
11
+ end
@@ -0,0 +1,56 @@
1
+ require 'time'
2
+ require 'date'
3
+
4
+ class String # :nodoc:
5
+ def to_liquid
6
+ self
7
+ end
8
+ end
9
+
10
+ class Array # :nodoc:
11
+ def to_liquid
12
+ self
13
+ end
14
+ end
15
+
16
+ class Hash # :nodoc:
17
+ def to_liquid
18
+ self
19
+ end
20
+ end
21
+
22
+ class Numeric # :nodoc:
23
+ def to_liquid
24
+ self
25
+ end
26
+ end
27
+
28
+ class Time # :nodoc:
29
+ def to_liquid
30
+ self
31
+ end
32
+ end
33
+
34
+ class DateTime < Date # :nodoc:
35
+ def to_liquid
36
+ self
37
+ end
38
+ end
39
+
40
+ class Date # :nodoc:
41
+ def to_liquid
42
+ self
43
+ end
44
+ end
45
+
46
+ def true.to_liquid # :nodoc:
47
+ self
48
+ end
49
+
50
+ def false.to_liquid # :nodoc:
51
+ self
52
+ end
53
+
54
+ def nil.to_liquid # :nodoc:
55
+ self
56
+ end
@@ -0,0 +1,62 @@
1
+ module Liquid
2
+ # A Liquid file system is way to let your templates retrieve other templates for use with the include tag.
3
+ #
4
+ # You can implement subclasses that retrieve templates from the database, from the file system using a different
5
+ # path structure, you can provide them as hard-coded inline strings, or any manner that you see fit.
6
+ #
7
+ # You can add additional instance variables, arguments, or methods as needed.
8
+ #
9
+ # Example:
10
+ #
11
+ # Liquid::Template.file_system = Liquid::LocalFileSystem.new(template_path)
12
+ # liquid = Liquid::Template.parse(template)
13
+ #
14
+ # This will parse the template with a LocalFileSystem implementation rooted at 'template_path'.
15
+ class BlankFileSystem
16
+ # Called by Liquid to retrieve a template file
17
+ def read_template_file(template_path)
18
+ raise FileSystemError, "This liquid context does not allow includes."
19
+ end
20
+ end
21
+
22
+ # This implements an abstract file system which retrieves template files named in a manner similar to Rails partials,
23
+ # ie. with the template name prefixed with an underscore. The extension ".liquid" is also added.
24
+ #
25
+ # For security reasons, template paths are only allowed to contain letters, numbers, and underscore.
26
+ #
27
+ # Example:
28
+ #
29
+ # file_system = Liquid::LocalFileSystem.new("/some/path")
30
+ #
31
+ # file_system.full_path("mypartial") # => "/some/path/_mypartial.liquid"
32
+ # file_system.full_path("dir/mypartial") # => "/some/path/dir/_mypartial.liquid"
33
+ #
34
+ class LocalFileSystem
35
+ attr_accessor :root
36
+
37
+ def initialize(root)
38
+ @root = root
39
+ end
40
+
41
+ def read_template_file(template_path)
42
+ full_path = full_path(template_path)
43
+ raise FileSystemError, "No such template '#{template_path}'" unless File.exists?(full_path)
44
+
45
+ File.read(full_path)
46
+ end
47
+
48
+ def full_path(template_path)
49
+ raise FileSystemError, "Illegal template name '#{template_path}'" unless template_path =~ /^[^.\/][a-zA-Z0-9_\/]+$/
50
+
51
+ full_path = if template_path.include?('/')
52
+ File.join(root, File.dirname(template_path), "_#{File.basename(template_path)}.liquid")
53
+ else
54
+ File.join(root, "_#{template_path}.liquid")
55
+ end
56
+
57
+ raise FileSystemError, "Illegal template path '#{File.expand_path(full_path)}'" unless File.expand_path(full_path) =~ /^#{File.expand_path(root)}/
58
+
59
+ full_path
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,74 @@
1
+ module Liquid
2
+ class TableRow < Block
3
+ Syntax = /(\w+)\s+in\s+(#{VariableSignature}+)/
4
+
5
+ def initialize(tag_name, markup, tokens)
6
+ if markup =~ Syntax
7
+ @variable_name = $1
8
+ @collection_name = $2
9
+ @attributes = {}
10
+ markup.scan(TagAttributes) do |key, value|
11
+ @attributes[key] = value
12
+ end
13
+ else
14
+ raise SyntaxError.new("Syntax Error in 'table_row loop' - Valid syntax: table_row [item] in [collection] cols=3")
15
+ end
16
+
17
+ super
18
+ end
19
+
20
+ def render(context)
21
+ collection = context[@collection_name] or return ''
22
+
23
+ if @attributes['limit'] or @attributes['offset']
24
+ limit = context[@attributes['limit']] || -1
25
+ offset = context[@attributes['offset']] || 0
26
+ collection = collection[offset.to_i..(limit.to_i + offset.to_i - 1)]
27
+ end
28
+
29
+ length = collection.length
30
+
31
+ cols = context[@attributes['cols']].to_i
32
+
33
+ row = 1
34
+ col = 0
35
+
36
+ result = ["<tr class=\"row1\">\n"]
37
+ context.stack do
38
+
39
+ collection.each_with_index do |item, index|
40
+ context[@variable_name] = item
41
+ context['tablerowloop'] = {
42
+ 'length' => length,
43
+ 'index' => index + 1,
44
+ 'index0' => index,
45
+ 'col' => col + 1,
46
+ 'col0' => col,
47
+ 'index0' => index,
48
+ 'rindex' => length - index,
49
+ 'rindex0' => length - index -1,
50
+ 'first' => (index == 0),
51
+ 'last' => (index == length - 1),
52
+ 'col_first' => (col == 0),
53
+ 'col_last' => (col == cols - 1)
54
+ }
55
+
56
+
57
+ col += 1
58
+
59
+ result << ["<td class=\"col#{col}\">"] + render_all(@nodelist, context) + ['</td>']
60
+
61
+ if col == cols and not (index == length - 1)
62
+ col = 0
63
+ row += 1
64
+ result << ["</tr>\n<tr class=\"row#{row}\">"]
65
+ end
66
+
67
+ end
68
+ end
69
+ result + ["</tr>\n"]
70
+ end
71
+ end
72
+
73
+ Template.register_tag('tablerow', TableRow)
74
+ end
@@ -0,0 +1,62 @@
1
+ # Copyright 2007 by Domizio Demichelis
2
+ # This library is free software. It may be used, redistributed and/or modified
3
+ # under the same terms as Ruby itself
4
+ #
5
+ # This extension is usesd in order to expose the object of the implementing class
6
+ # to liquid as it were a Drop. It also limits the liquid-callable methods of the instance
7
+ # to the allowed method passed with the liquid_methods call
8
+ # Example:
9
+ #
10
+ # class SomeClass
11
+ # liquid_methods :an_allowed_method
12
+ #
13
+ # def an_allowed_method
14
+ # 'this comes from an allowed method'
15
+ # end
16
+ # def unallowed_method
17
+ # 'this will never be an output'
18
+ # end
19
+ # end
20
+ #
21
+ # if you want to extend the drop to other methods you can defines more methods
22
+ # in the class <YourClass>::LiquidDropClass
23
+ #
24
+ # class SomeClass::LiquidDropClass
25
+ # def another_allowed_method
26
+ # 'and this from another allowed method'
27
+ # end
28
+ # end
29
+ # end
30
+ #
31
+ # usage:
32
+ # @something = SomeClass.new
33
+ #
34
+ # template:
35
+ # {{something.an_allowed_method}}{{something.unallowed_method}} {{something.another_allowed_method}}
36
+ #
37
+ # output:
38
+ # 'this comes from an allowed method and this from another allowed method'
39
+ #
40
+ # You can also chain associations, by adding the liquid_method call in the
41
+ # association models.
42
+ #
43
+ class Module
44
+
45
+ def liquid_methods(*allowed_methods)
46
+ drop_class = eval "class #{self.to_s}::LiquidDropClass < Liquid::Drop; self; end"
47
+ define_method :to_liquid do
48
+ drop_class.new(self)
49
+ end
50
+ drop_class.class_eval do
51
+ def initialize(object)
52
+ @object = object
53
+ end
54
+ allowed_methods.each do |sym|
55
+ define_method sym do
56
+ @object.send sym
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,209 @@
1
+ require 'cgi'
2
+
3
+ module Liquid
4
+
5
+ module StandardFilters
6
+
7
+ # Return the size of an array or of an string
8
+ def size(input)
9
+
10
+ input.respond_to?(:size) ? input.size : 0
11
+ end
12
+
13
+ # convert a input string to DOWNCASE
14
+ def downcase(input)
15
+ input.to_s.downcase
16
+ end
17
+
18
+ # convert a input string to UPCASE
19
+ def upcase(input)
20
+ input.to_s.upcase
21
+ end
22
+
23
+ # capitalize words in the input centence
24
+ def capitalize(input)
25
+ input.to_s.capitalize
26
+ end
27
+
28
+ def escape(input)
29
+ CGI.escapeHTML(input) rescue input
30
+ end
31
+
32
+ alias_method :h, :escape
33
+
34
+ # Truncate a string down to x characters
35
+ def truncate(input, length = 50, truncate_string = "...")
36
+ if input.nil? then return end
37
+ l = length.to_i - truncate_string.length
38
+ l = 0 if l < 0
39
+ input.length > length.to_i ? input[0...l] + truncate_string : input
40
+ end
41
+
42
+ def truncatewords(input, words = 15, truncate_string = "...")
43
+ if input.nil? then return end
44
+ wordlist = input.to_s.split
45
+ l = words.to_i - 1
46
+ l = 0 if l < 0
47
+ wordlist.length > l ? wordlist[0..l].join(" ") + truncate_string : input
48
+ end
49
+
50
+ def strip_html(input)
51
+ input.to_s.gsub(/<.*?>/, '')
52
+ end
53
+
54
+ # Remove all newlines from the string
55
+ def strip_newlines(input)
56
+ input.to_s.gsub(/\n/, '')
57
+ end
58
+
59
+
60
+ # Join elements of the array with certain character between them
61
+ def join(input, glue = ' ')
62
+ [input].flatten.join(glue)
63
+ end
64
+
65
+ # Sort elements of the array
66
+ # provide optional property with which to sort an array of hashes or drops
67
+ def sort(input, property = nil)
68
+ ary = [input].flatten
69
+ if property.nil?
70
+ ary.sort
71
+ elsif ary.first.respond_to?('[]') and !ary.first[property].nil?
72
+ ary.sort {|a,b| a[property] <=> b[property] }
73
+ elsif ary.first.respond_to?(property)
74
+ ary.sort {|a,b| a.send(property) <=> b.send(property) }
75
+ end
76
+ end
77
+
78
+ # map/collect on a given property
79
+ def map(input, property)
80
+ ary = [input].flatten
81
+ if ary.first.respond_to?('[]') and !ary.first[property].nil?
82
+ ary.map {|e| e[property] }
83
+ elsif ary.first.respond_to?(property)
84
+ ary.map {|e| e.send(property) }
85
+ end
86
+ end
87
+
88
+ # Replace occurrences of a string with another
89
+ def replace(input, string, replacement = '')
90
+ input.to_s.gsub(string, replacement)
91
+ end
92
+
93
+ # Replace the first occurrences of a string with another
94
+ def replace_first(input, string, replacement = '')
95
+ input.to_s.sub(string, replacement)
96
+ end
97
+
98
+ # remove a substring
99
+ def remove(input, string)
100
+ input.to_s.gsub(string, '')
101
+ end
102
+
103
+ # remove the first occurrences of a substring
104
+ def remove_first(input, string)
105
+ input.to_s.sub(string, '')
106
+ end
107
+
108
+ # add one string to another
109
+ def append(input, string)
110
+ input.to_s + string.to_s
111
+ end
112
+
113
+ # prepend a string to another
114
+ def prepend(input, string)
115
+ string.to_s + input.to_s
116
+ end
117
+
118
+ # Add <br /> tags in front of all newlines in input string
119
+ def newline_to_br(input)
120
+ input.to_s.gsub(/\n/, "<br />\n")
121
+ end
122
+
123
+ # Reformat a date
124
+ #
125
+ # %a - The abbreviated weekday name (``Sun'')
126
+ # %A - The full weekday name (``Sunday'')
127
+ # %b - The abbreviated month name (``Jan'')
128
+ # %B - The full month name (``January'')
129
+ # %c - The preferred local date and time representation
130
+ # %d - Day of the month (01..31)
131
+ # %H - Hour of the day, 24-hour clock (00..23)
132
+ # %I - Hour of the day, 12-hour clock (01..12)
133
+ # %j - Day of the year (001..366)
134
+ # %m - Month of the year (01..12)
135
+ # %M - Minute of the hour (00..59)
136
+ # %p - Meridian indicator (``AM'' or ``PM'')
137
+ # %S - Second of the minute (00..60)
138
+ # %U - Week number of the current year,
139
+ # starting with the first Sunday as the first
140
+ # day of the first week (00..53)
141
+ # %W - Week number of the current year,
142
+ # starting with the first Monday as the first
143
+ # day of the first week (00..53)
144
+ # %w - Day of the week (Sunday is 0, 0..6)
145
+ # %x - Preferred representation for the date alone, no time
146
+ # %X - Preferred representation for the time alone, no date
147
+ # %y - Year without a century (00..99)
148
+ # %Y - Year with century
149
+ # %Z - Time zone name
150
+ # %% - Literal ``%'' character
151
+ def date(input, format)
152
+
153
+ if format.to_s.empty?
154
+ return input.to_s
155
+ end
156
+
157
+ date = input.is_a?(String) ? Time.parse(input) : input
158
+
159
+ if date.respond_to?(:strftime)
160
+ date.strftime(format.to_s)
161
+ else
162
+ input
163
+ end
164
+ rescue => e
165
+ input
166
+ end
167
+
168
+ # Get the first element of the passed in array
169
+ #
170
+ # Example:
171
+ # {{ product.images | first | to_img }}
172
+ #
173
+ def first(array)
174
+ array.first if array.respond_to?(:first)
175
+ end
176
+
177
+ # Get the last element of the passed in array
178
+ #
179
+ # Example:
180
+ # {{ product.images | last | to_img }}
181
+ #
182
+ def last(array)
183
+ array.last if array.respond_to?(:last)
184
+ end
185
+
186
+ # addition
187
+ def plus(input, operand)
188
+ input + operand if input.respond_to?('+')
189
+ end
190
+
191
+ # subtraction
192
+ def minus(input, operand)
193
+ input - operand if input.respond_to?('-')
194
+ end
195
+
196
+ # multiplication
197
+ def times(input, operand)
198
+ input * operand if input.respond_to?('*')
199
+ end
200
+
201
+ # division
202
+ def divided_by(input, operand)
203
+ input / operand if input.respond_to?('/')
204
+ end
205
+
206
+ end
207
+
208
+ Template.register_filter(StandardFilters)
209
+ end