mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 15:30:39 -07:00
Merge branch 'experimental' of git://github.com/jamesmacaulay/liquid into jamesmacaulay/experimental
Conflicts: lib/liquid.rb lib/liquid/variable.rb
This commit is contained in:
+6
-1
@@ -33,7 +33,12 @@ module Liquid
|
||||
VariableStart = /\{\{/
|
||||
VariableEnd = /\}\}/
|
||||
VariableIncompleteEnd = /\}\}?/
|
||||
QuotedFragment = /"[^"]+"|'[^']+'|[^\s,|]+/
|
||||
QuotedFragment = /"[^"]+"|'[^']+'|[^\s,\|]+/
|
||||
StrictQuotedFragment = /"[^"]+"|'[^']+'|[^\s,\|,\:,\,]+/
|
||||
FirstFilterArgument = /#{FilterArgumentSeparator}(?:#{StrictQuotedFragment})/
|
||||
OtherFilterArgument = /#{ArgumentSeparator}(?:#{StrictQuotedFragment})/
|
||||
SpacelessFilter = /#{FilterSeparator}(?:#{StrictQuotedFragment})(?:#{FirstFilterArgument}(?:#{OtherFilterArgument})*)?/
|
||||
Expression = /(?:#{QuotedFragment}(?:#{SpacelessFilter})*)/
|
||||
TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/
|
||||
AnyStartingTag = /\{\{|\{\%/
|
||||
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/
|
||||
|
||||
@@ -133,6 +133,9 @@ module Liquid
|
||||
:blank?
|
||||
when 'empty'
|
||||
:empty?
|
||||
# filtered variables
|
||||
when SpacelessFilter
|
||||
filtered_variable(key)
|
||||
# Single quoted strings
|
||||
when /^'(.*)'$/
|
||||
$1.to_s
|
||||
@@ -221,5 +224,9 @@ module Liquid
|
||||
|
||||
object
|
||||
end
|
||||
|
||||
def filtered_variable(markup)
|
||||
Variable.new(markup).render(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -63,9 +63,27 @@ module Liquid
|
||||
end
|
||||
|
||||
# Sort elements of the array
|
||||
def sort(input)
|
||||
[input].flatten.sort
|
||||
# provide optional property with which to sort an array of hashes or drops
|
||||
def sort(input, property = nil)
|
||||
ary = [input].flatten
|
||||
if property.nil?
|
||||
ary.sort
|
||||
elsif ary.first.respond_to?('[]') and !ary.first[property].nil?
|
||||
ary.sort {|a,b| a[property] <=> b[property] }
|
||||
elsif ary.first.respond_to?(property)
|
||||
ary.sort {|a,b| a.send(property) <=> b.send(property) }
|
||||
end
|
||||
end
|
||||
|
||||
# map/collect on a given property
|
||||
def map(input, property)
|
||||
ary = [input].flatten
|
||||
if ary.first.respond_to?('[]') and !ary.first[property].nil?
|
||||
ary.map {|e| e[property] }
|
||||
elsif ary.first.respond_to?(property)
|
||||
ary.map {|e| e.send(property) }
|
||||
end
|
||||
end
|
||||
|
||||
# Replace occurrences of a string with another
|
||||
def replace(input, string, replacement = '')
|
||||
@@ -155,6 +173,26 @@ module Liquid
|
||||
array.last if array.respond_to?(:last)
|
||||
end
|
||||
|
||||
# addition
|
||||
def plus(input, operand)
|
||||
input + operand if input.respond_to?('+')
|
||||
end
|
||||
|
||||
# subtraction
|
||||
def minus(input, operand)
|
||||
input - operand if input.respond_to?('-')
|
||||
end
|
||||
|
||||
# multiplication
|
||||
def times(input, operand)
|
||||
input * operand if input.respond_to?('*')
|
||||
end
|
||||
|
||||
# division
|
||||
def divided_by(input, operand)
|
||||
input / operand if input.respond_to?('/')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Template.register_filter(StandardFilters)
|
||||
|
||||
@@ -9,7 +9,7 @@ module Liquid
|
||||
# {{ monkey }}
|
||||
#
|
||||
class Assign < Tag
|
||||
Syntax = /(#{VariableSignature}+)\s*=\s*(#{QuotedFragment}+)/
|
||||
Syntax = /(#{VariableSignature}+)\s*=\s*(#{Expression}+)/
|
||||
|
||||
def initialize(tag_name, markup, tokens)
|
||||
if markup =~ Syntax
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
module Liquid
|
||||
class Case < Block
|
||||
Syntax = /(#{QuotedFragment})/
|
||||
WhenSyntax = /(#{QuotedFragment})(?:(?:\s+or\s+|\s*\,\s*)(#{QuotedFragment}.*))?/
|
||||
Syntax = /(#{Expression})/
|
||||
WhenSyntax = /(#{Expression})(?:(?:\s+or\s+|\s*\,\s*)(#{Expression}.*))?/
|
||||
|
||||
def initialize(tag_name, markup, tokens)
|
||||
@blocks = []
|
||||
|
||||
@@ -13,8 +13,8 @@ module Liquid
|
||||
# <div class="green"> Item five</div>
|
||||
#
|
||||
class Cycle < Tag
|
||||
SimpleSyntax = /#{QuotedFragment}/
|
||||
NamedSyntax = /(#{QuotedFragment})\s*\:\s*(.*)/
|
||||
SimpleSyntax = /#{Expression}/
|
||||
NamedSyntax = /(#{Expression})\s*\:\s*(.*)/
|
||||
|
||||
def initialize(tag_name, markup, tokens)
|
||||
case markup
|
||||
@@ -49,7 +49,7 @@ module Liquid
|
||||
|
||||
def variables_from_string(markup)
|
||||
markup.split(',').collect do |var|
|
||||
var =~ /\s*(#{QuotedFragment})\s*/
|
||||
var =~ /\s*(#{Expression})\s*/
|
||||
$1 ? $1 : nil
|
||||
end.compact
|
||||
end
|
||||
|
||||
@@ -42,7 +42,7 @@ module Liquid
|
||||
# forloop.last:: Returns true if the item is the last item.
|
||||
#
|
||||
class For < Block
|
||||
Syntax = /(\w+)\s+in\s+(#{VariableSignature}+)\s*(reversed)?/
|
||||
Syntax = /(\w+)\s+in\s+(#{Expression}+)\s*(reversed)?/
|
||||
|
||||
def initialize(tag_name, markup, tokens)
|
||||
if markup =~ Syntax
|
||||
|
||||
@@ -13,7 +13,7 @@ module Liquid
|
||||
#
|
||||
class If < Block
|
||||
SyntaxHelp = "Syntax Error in tag 'if' - Valid syntax: if [expression]"
|
||||
Syntax = /(#{QuotedFragment})\s*([=!<>a-z_]+)?\s*(#{QuotedFragment})?/
|
||||
Syntax = /(#{Expression})\s*([=!<>a-z_]+)?\s*(#{Expression})?/
|
||||
|
||||
def initialize(tag_name, markup, tokens)
|
||||
|
||||
|
||||
@@ -21,8 +21,7 @@ module Liquid
|
||||
@name = match[1]
|
||||
if markup.match(/#{FilterSeparator}\s*(.*)/)
|
||||
filters = Regexp.last_match(1).split(/#{FilterSeparator}/)
|
||||
|
||||
filters.each do |f|
|
||||
filters.each do |f|
|
||||
if matches = f.match(/\s*(\w+)/)
|
||||
filtername = matches[1]
|
||||
filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*(#{QuotedFragment})/).flatten
|
||||
|
||||
Reference in New Issue
Block a user