Fix spacing

This commit is contained in:
Mike Angell
2019-09-20 02:25:35 +10:00
parent 05c0bcf609
commit 26eb27f79f
78 changed files with 508 additions and 500 deletions
+5 -5
View File
@@ -24,8 +24,8 @@
module Liquid
FILTER_SEPARATOR = /\|/
ARGUMENT_SEPARATOR = ','
FILTER_ARGUMENT_SEPARATOR = ':'
VARIABLE_ATTRIBUTE_SEPARATOR = '.'
FILTER_ARGUMENT_SEPARATOR = ':'
VARIABLE_ATTRIBUTE_SEPARATOR = '.'
WHITESPACE_CONTROL = '-'
TAG_START = /\{\%/
TAG_END = /\%\}/
@@ -33,12 +33,12 @@ module Liquid
VARIABLE_SEGMENT = /[\w\-]/
VARIABLE_START = /\{\{/
VARIABLE_END = /\}\}/
VARIABLE_INCOMPLETE_END = /\}\}?/
VARIABLE_INCOMPLETE_END = /\}\}?/
QUOTED_STRING = /"[^"]*"|'[^']*'/
QUOTED_FRAGMENT = /#{QUOTED_STRING}|(?:[^\s,\|'"]|#{QUOTED_STRING})+/o
TAG_ATTRIBUTES = /(\w+)\s*\:\s*(#{QUOTED_FRAGMENT})/o
ANY_STARTING_TAG = /#{TAG_START}|#{VARIABLE_START}/o
PARTIAL_TEMPLATE_PARSER = /#{TAG_START}.*?#{TAG_END}|#{VARIABLE_START}.*?#{VARIABLE_INCOMPLETE_END}/om
ANY_STARTING_TAG = /#{TAG_START}|#{VARIABLE_START}/o
PARTIAL_TEMPLATE_PARSER = /#{TAG_START}.*?#{TAG_END}|#{VARIABLE_START}.*?#{VARIABLE_INCOMPLETE_END}/om
TEMPLATE_PARSER = /(#{PARTIAL_TEMPLATE_PARSER}|#{ANY_STARTING_TAG})/om
VARIABLE_PARSER = /\[[^\]]+\]|#{VARIABLE_SEGMENT}+\??/o
+15 -15
View File
@@ -2,18 +2,18 @@
module Liquid
class BlockBody
LIQUID_TAG_TOKEN = /\A\s*(\w+)\s*(.*?)\z/o
FULL_TOKEN = /\A#{TAG_START}#{WHITESPACE_CONTROL}?(\s*)(\w+)(\s*)(.*?)#{WHITESPACE_CONTROL}?#{TAG_END}\z/om
CONTENT_OF_VARIABLE = /\A#{VARIABLE_START}#{WHITESPACE_CONTROL}?(.*?)#{WHITESPACE_CONTROL}?#{VARIABLE_END}\z/om
LIQUID_TAG_TOKEN = /\A\s*(\w+)\s*(.*?)\z/o
FULL_TOKEN = /\A#{TAG_START}#{WHITESPACE_CONTROL}?(\s*)(\w+)(\s*)(.*?)#{WHITESPACE_CONTROL}?#{TAG_END}\z/om
CONTENT_OF_VARIABLE = /\A#{VARIABLE_START}#{WHITESPACE_CONTROL}?(.*?)#{WHITESPACE_CONTROL}?#{VARIABLE_END}\z/om
WHITESPACE_OR_NOTHING = /\A\s*\z/
TAG_START_STRING = "{%"
VAR_START_STRING = "{{"
TAG_START_STRING = "{%"
VAR_START_STRING = "{{"
attr_reader :nodelist
def initialize
@nodelist = []
@blank = true
@blank = true
end
def parse(tokenizer, parse_context, &block)
@@ -34,15 +34,15 @@ module Liquid
# caller raise a syntax error
return yield token, token
end
tag_name = Regexp.last_match(1)
markup = Regexp.last_match(2)
tag_name = Regexp.last_match(1)
markup = Regexp.last_match(2)
unless (tag = registered_tags[tag_name])
# end parsing if we reach an unknown tag and let the caller decide
# determine how to proceed
return yield tag_name, markup
end
new_tag = tag.parse(tag_name, markup, tokenizer, parse_context)
@blank &&= new_tag.blank?
new_tag = tag.parse(tag_name, markup, tokenizer, parse_context)
@blank &&= new_tag.blank?
@nodelist << new_tag
end
parse_context.line_number = tokenizer.line_number
@@ -61,7 +61,7 @@ module Liquid
raise_missing_tag_terminator(token, parse_context)
end
tag_name = Regexp.last_match(2)
markup = Regexp.last_match(4)
markup = Regexp.last_match(4)
if parse_context.line_number
# newlines inside the tag should increase the line number,
@@ -79,8 +79,8 @@ module Liquid
# determine how to proceed
return yield tag_name, markup
end
new_tag = tag.parse(tag_name, markup, tokenizer, parse_context)
@blank &&= new_tag.blank?
new_tag = tag.parse(tag_name, markup, tokenizer, parse_context)
@blank &&= new_tag.blank?
@nodelist << new_tag
when token.start_with?(VAR_START_STRING)
whitespace_handler(token, parse_context)
@@ -92,7 +92,7 @@ module Liquid
end
parse_context.trim_whitespace = false
@nodelist << token
@blank &&= !!(token =~ WHITESPACE_OR_NOTHING)
@blank &&= !!(token =~ WHITESPACE_OR_NOTHING)
end
parse_context.line_number = tokenizer.line_number
end
@@ -121,7 +121,7 @@ module Liquid
def render_to_output_buffer(context, output)
context.resource_limits.render_score += @nodelist.length
idx = 0
idx = 0
while (node = @nodelist[idx])
previous_output_size = output.bytesize
+8 -8
View File
@@ -35,16 +35,16 @@ module Liquid
attr_accessor :left, :operator, :right
def initialize(left = nil, operator = nil, right = nil)
@left = left
@operator = operator
@right = right
@child_relation = nil
@left = left
@operator = operator
@right = right
@child_relation = nil
@child_condition = nil
end
def evaluate(context = Context.new)
condition = self
result = nil
result = nil
loop do
result = interpret_condition(condition.left, condition.right, condition.operator, context)
@@ -62,12 +62,12 @@ module Liquid
end
def or(condition)
@child_relation = :or
@child_relation = :or
@child_condition = condition
end
def and(condition)
@child_relation = :and
@child_relation = :and
@child_condition = condition
end
@@ -115,7 +115,7 @@ module Liquid
# return this as the result.
return context.evaluate(left) if op.nil?
left = context.evaluate(left)
left = context.evaluate(left)
right = context.evaluate(right)
operation = self.class.operators[op] || raise(Liquid::ArgumentError, "Unknown operator #{op}")
+11 -11
View File
@@ -41,8 +41,8 @@ module Liquid
self.exception_renderer = ->(_e) { raise }
end
@interrupts = []
@filters = []
@interrupts = []
@filters = []
@global_filter = nil
end
# rubocop:enable Metrics/ParameterLists
@@ -60,7 +60,7 @@ module Liquid
# Note that this does not register the filters with the main Template object. see <tt>Template.register_filter</tt>
# for that
def add_filters(filters)
filters = [filters].flatten.compact
filters = [filters].flatten.compact
@filters += filters
@strainer = nil
end
@@ -85,9 +85,9 @@ module Liquid
end
def handle_error(e, line_number = nil)
e = internal_error unless e.is_a?(Liquid::Error)
e = internal_error unless e.is_a?(Liquid::Error)
e.template_name ||= template_name
e.line_number ||= line_number
e.line_number ||= line_number
errors.push(e)
exception_renderer.call(e).to_s
end
@@ -138,12 +138,12 @@ module Liquid
static_environments: static_environments,
registers: StaticRegisters.new(registers)
).tap do |subcontext|
subcontext.base_scope_depth = base_scope_depth + 1
subcontext.base_scope_depth = base_scope_depth + 1
subcontext.exception_renderer = exception_renderer
subcontext.filters = @filters
subcontext.strainer = nil
subcontext.errors = errors
subcontext.warnings = warnings
subcontext.filters = @filters
subcontext.strainer = nil
subcontext.errors = errors
subcontext.warnings = warnings
end
end
@@ -188,7 +188,7 @@ module Liquid
try_variable_find_in_environments(key, raise_on_not_found: raise_on_not_found)
end
variable = variable.to_liquid
variable = variable.to_liquid
variable.context = self if variable.respond_to?(:context=)
variable
+13 -13
View File
@@ -40,19 +40,19 @@ module Liquid
end
end
ArgumentError = Class.new(Error)
ContextError = Class.new(Error)
FileSystemError = Class.new(Error)
StandardError = Class.new(Error)
SyntaxError = Class.new(Error)
StackLevelError = Class.new(Error)
TaintedError = Class.new(Error)
MemoryError = Class.new(Error)
ZeroDivisionError = Class.new(Error)
FloatDomainError = Class.new(Error)
UndefinedVariable = Class.new(Error)
ArgumentError = Class.new(Error)
ContextError = Class.new(Error)
FileSystemError = Class.new(Error)
StandardError = Class.new(Error)
SyntaxError = Class.new(Error)
StackLevelError = Class.new(Error)
TaintedError = Class.new(Error)
MemoryError = Class.new(Error)
ZeroDivisionError = Class.new(Error)
FloatDomainError = Class.new(Error)
UndefinedVariable = Class.new(Error)
UndefinedDropMethod = Class.new(Error)
UndefinedFilter = Class.new(Error)
UndefinedFilter = Class.new(Error)
MethodOverrideError = Class.new(Error)
InternalError = Class.new(Error)
InternalError = Class.new(Error)
end
+1 -1
View File
@@ -7,7 +7,7 @@ module Liquid
def initialize(method_name, to_s)
@method_name = method_name
@to_s = to_s
@to_s = to_s
end
def to_liquid
+1 -1
View File
@@ -47,7 +47,7 @@ module Liquid
attr_accessor :root
def initialize(root, pattern = "_%s.liquid")
@root = root
@root = root
@pattern = pattern
end
+3 -3
View File
@@ -3,10 +3,10 @@
module Liquid
class ForloopDrop < Drop
def initialize(name, length, parentloop)
@name = name
@length = length
@name = name
@length = length
@parentloop = parentloop
@index = 0
@index = 0
end
attr_reader :name, :length, :parentloop
+28 -28
View File
@@ -1,32 +1,32 @@
# frozen_string_literal: true
module Liquid
FilterSeparator = FILTER_SEPARATOR
ArgumentSeparator = ARGUMENT_SEPARATOR
FilterArgumentSeparator = FILTER_ARGUMENT_SEPARATOR
FilterSeparator = FILTER_SEPARATOR
ArgumentSeparator = ARGUMENT_SEPARATOR
FilterArgumentSeparator = FILTER_ARGUMENT_SEPARATOR
VariableAttributeSeparator = VARIABLE_ATTRIBUTE_SEPARATOR
WhitespaceControl = WHITESPACE_CONTROL
TagStart = TAG_START
TagEnd = TAG_END
VariableSignature = VARIABLE_SIGNATURE
VariableSegment = VARIABLE_SEGMENT
VariableStart = VARIABLE_START
VariableEnd = VARIABLE_END
VariableIncompleteEnd = VARIABLE_INCOMPLETE_END
QuotedString = QUOTED_STRING
QuotedFragment = QUOTED_FRAGMENT
TagAttributes = TAG_ATTRIBUTES
AnyStartingTag = ANY_STARTING_TAG
PartialTemplateParser = PARTIAL_TEMPLATE_PARSER
TemplateParser = TEMPLATE_PARSER
VariableParser = VARIABLE_PARSER
WhitespaceControl = WHITESPACE_CONTROL
TagStart = TAG_START
TagEnd = TAG_END
VariableSignature = VARIABLE_SIGNATURE
VariableSegment = VARIABLE_SEGMENT
VariableStart = VARIABLE_START
VariableEnd = VARIABLE_END
VariableIncompleteEnd = VARIABLE_INCOMPLETE_END
QuotedString = QUOTED_STRING
QuotedFragment = QUOTED_FRAGMENT
TagAttributes = TAG_ATTRIBUTES
AnyStartingTag = ANY_STARTING_TAG
PartialTemplateParser = PARTIAL_TEMPLATE_PARSER
TemplateParser = TEMPLATE_PARSER
VariableParser = VARIABLE_PARSER
class BlockBody
FullToken = FULL_TOKEN
ContentOfVariable = CONTENT_OF_VARIABLE
FullToken = FULL_TOKEN
ContentOfVariable = CONTENT_OF_VARIABLE
WhitespaceOrNothing = WHITESPACE_OR_NOTHING
TAGSTART = TAG_START_STRING
VARSTART = VAR_START_STRING
TAGSTART = TAG_START_STRING
VARSTART = VAR_START_STRING
end
class Assign < Tag
@@ -52,7 +52,7 @@ module Liquid
end
class If < Block
Syntax = SYNTAX
Syntax = SYNTAX
ExpressionsAndOperators = EXPRESSIONS_AND_OPERATORS
end
@@ -61,7 +61,7 @@ module Liquid
end
class Raw < Block
Syntax = SYNTAX
Syntax = SYNTAX
FullTokenPossiblyInvalid = FULL_TOKEN_POSSIBLY_INVALID
end
@@ -70,10 +70,10 @@ module Liquid
end
class Variable
FilterMarkupRegex = FILTER_MARKUP_REGEX
FilterParser = FILTER_PARSER
FilterArgsRegex = FILTER_ARGS_REGEX
JustTagAttributes = JUST_TAG_ATTRIBUTES
FilterMarkupRegex = FILTER_MARKUP_REGEX
FilterParser = FILTER_PARSER
FilterArgsRegex = FILTER_ARGS_REGEX
JustTagAttributes = JUST_TAG_ATTRIBUTES
MarkupWithQuotedFragment = MARKUP_WITH_QUOTED_FRAGMENT
end
end
+7 -6
View File
@@ -15,12 +15,13 @@ module Liquid
'?' => :question,
'-' => :dash,
}.freeze
IDENTIFIER = /[a-zA-Z_][\w-]*\??/
IDENTIFIER = /[a-zA-Z_][\w-]*\??/
SINGLE_STRING_LITERAL = /'[^\']*'/
DOUBLE_STRING_LITERAL = /"[^\"]*"/
NUMBER_LITERAL = /-?\d+(\.\d+)?/
DOTDOT = /\.\./
COMPARISON_OPERATOR = /==|!=|<>|<=?|>=?|contains(?=\s)/
NUMBER_LITERAL = /-?\d+(\.\d+)?/
DOTDOT = /\.\./
COMPARISON_OPERATOR = /==|!=|<>|<=?|>=?|contains(?=\s)/
WHITESPACE_OR_NOTHING = /\s*/
def initialize(input)
@@ -33,7 +34,7 @@ module Liquid
until @ss.eos?
@ss.skip(WHITESPACE_OR_NOTHING)
break if @ss.eos?
tok = if (t = @ss.scan(COMPARISON_OPERATOR))
tok = if (t = @ss.scan(COMPARISON_OPERATOR))
[:comparison, t]
elsif (t = @ss.scan(SINGLE_STRING_LITERAL))
[:string, t]
@@ -46,7 +47,7 @@ module Liquid
elsif (t = @ss.scan(DOTDOT))
[:dotdot, t]
else
c = @ss.getch
c = @ss.getch
if (s = SPECIALS[c])
[s, c]
else
+6 -6
View File
@@ -7,10 +7,10 @@ module Liquid
def initialize(options = {})
@template_options = options ? options.dup : {}
@locale = @template_options[:locale] ||= I18n.new
@warnings = []
self.depth = 0
self.partial = false
@locale = @template_options[:locale] ||= I18n.new
@warnings = []
self.depth = 0
self.partial = false
end
def [](option_key)
@@ -18,8 +18,8 @@ module Liquid
end
def partial=(value)
@partial = value
@options = value ? partial_options : @template_options
@partial = value
@options = value ? partial_options : @template_options
@error_mode = @options[:error_mode] || Template.error_mode
end
+4 -4
View File
@@ -11,14 +11,14 @@ module Liquid
end
def initialize(node, callbacks)
@node = node
@node = node
@callbacks = callbacks
end
def add_callback_for(*classes, &block)
callback = block
callback = ->(node, _) { yield node } if block.arity.abs == 1
callback = ->(_, _) { yield } if block.arity.zero?
callback = block
callback = ->(node, _) { yield node } if block.arity.abs == 1
callback = ->(_, _) { yield } if block.arity.zero?
classes.each { |klass| @callbacks[klass] = callback }
self
end
+6 -3
View File
@@ -3,9 +3,9 @@
module Liquid
class Parser
def initialize(input)
l = Lexer.new(input)
l = Lexer.new(input)
@tokens = l.tokenize
@p = 0 # pointer to current location
@p = 0 # pointer to current location
end
def jump(point)
@@ -14,6 +14,7 @@ module Liquid
def consume(type = nil)
token = @tokens[@p]
if type && token[0] != type
raise SyntaxError, "Expected #{type} but found #{@tokens[@p].first}"
end
@@ -26,6 +27,7 @@ module Liquid
# or false otherwise.
def consume?(type)
token = @tokens[@p]
return false unless token && token[0] == type
@p += 1
token[1]
@@ -34,6 +36,7 @@ module Liquid
# Like consume? Except for an :id token of a certain name
def id?(str)
token = @tokens[@p]
return false unless token && token[0] == :id
return false unless token[1] == str
@p += 1
@@ -59,7 +62,7 @@ module Liquid
consume
first = expression
consume(:dotdot)
last = expression
last = expression
consume(:close_round)
"(#{first}..#{last})"
else
+1 -1
View File
@@ -21,7 +21,7 @@ module Liquid
def strict_parse_with_error_context(markup)
strict_parse(markup)
rescue SyntaxError => e
e.line_number = line_number
e.line_number = line_number
e.markup_context = markup_context(markup)
raise e
end
+4 -4
View File
@@ -4,14 +4,14 @@ module Liquid
class PartialCache
def self.load(template_name, context:, parse_context:)
cached_partials = (context.registers[:cached_partials] ||= {})
cached = cached_partials[template_name]
cached = cached_partials[template_name]
return cached if cached
file_system = (context.registers[:file_system] ||= Liquid::Template.file_system)
source = file_system.read_template_file(template_name)
file_system = (context.registers[:file_system] ||= Liquid::Template.file_system)
source = file_system.read_template_file(template_name)
parse_context.partial = true
partial = Liquid::Template.parse(source, parse_context)
partial = Liquid::Template.parse(source, parse_context)
cached_partials[template_name] = partial
ensure
parse_context.partial = false
+4 -4
View File
@@ -101,21 +101,21 @@ module Liquid
def initialize
@partial_stack = ["<root>"]
@root_timing = Timing.new("", current_partial)
@root_timing = Timing.new("", current_partial)
@timing_stack = [@root_timing]
@render_start_at = Time.now
@render_end_at = @render_start_at
@render_end_at = @render_start_at
end
def start
Thread.current[:liquid_profiler] = self
@render_start_at = Time.now
@render_start_at = Time.now
end
def stop
Thread.current[:liquid_profiler] = nil
@render_end_at = Time.now
@render_end_at = Time.now
end
def total_render_time
+3 -3
View File
@@ -4,7 +4,7 @@ module Liquid
class RangeLookup
def self.parse(start_markup, end_markup)
start_obj = Expression.parse(start_markup)
end_obj = Expression.parse(end_markup)
end_obj = Expression.parse(end_markup)
if start_obj.respond_to?(:evaluate) || end_obj.respond_to?(:evaluate)
new(start_obj, end_obj)
else
@@ -14,12 +14,12 @@ module Liquid
def initialize(start_obj, end_obj)
@start_obj = start_obj
@end_obj = end_obj
@end_obj = end_obj
end
def evaluate(context)
start_int = to_integer(context.evaluate(@start_obj))
end_int = to_integer(context.evaluate(@end_obj))
end_int = to_integer(context.evaluate(@end_obj))
start_int..end_int
end
+2 -2
View File
@@ -7,8 +7,8 @@ module Liquid
def initialize(limits)
@render_length_limit = limits[:render_length_limit]
@render_score_limit = limits[:render_score_limit]
@assign_score_limit = limits[:assign_score_limit]
@render_score_limit = limits[:render_score_limit]
@assign_score_limit = limits[:assign_score_limit]
reset
end
+11 -10
View File
@@ -12,13 +12,14 @@ module Liquid
'"' => '&quot;',
"'" => '&#39;',
}.freeze
HTML_ESCAPE_ONCE_REGEXP = /["><']|&(?!([a-zA-Z]+|(#\d+));)/
STRIP_HTML_BLOCKS = Regexp.union(
STRIP_HTML_TAGS = /<.*?>/m
STRIP_HTML_BLOCKS = Regexp.union(
%r{<script.*?</script>}m,
/<!--.*?-->/m,
%r{<style.*?</style>}m
)
STRIP_HTML_TAGS = /<.*?>/m
# Return the size of an array or of an string
def size(input)
@@ -76,20 +77,20 @@ module Liquid
# Truncate a string down to x characters
def truncate(input, length = 50, truncate_string = "...")
return if input.nil?
input_str = input.to_s
length = Utils.to_integer(length)
input_str = input.to_s
length = Utils.to_integer(length)
truncate_string_str = truncate_string.to_s
l = length - truncate_string_str.length
l = 0 if l < 0
l = length - truncate_string_str.length
l = 0 if l < 0
input_str.length > length ? input_str[0...l].concat(truncate_string_str) : input_str
end
def truncatewords(input, words = 15, truncate_string = "...")
return if input.nil?
wordlist = input.to_s.split
words = Utils.to_integer(words)
l = words - 1
l = 0 if l < 0
words = Utils.to_integer(words)
l = words - 1
l = 0 if l < 0
wordlist.length > l ? wordlist[0..l].join(" ").concat(truncate_string.to_s) : input
end
@@ -115,7 +116,7 @@ module Liquid
end
def strip_html(input)
empty = ''
empty = ''
result = input.to_s.gsub(STRIP_HTML_BLOCKS, empty)
result.gsub!(STRIP_HTML_TAGS, empty)
result
+1 -1
View File
@@ -5,7 +5,7 @@ module Liquid
attr_reader :static, :registers
def initialize(registers = {})
@static = registers.is_a?(StaticRegisters) ? registers.static : registers
@static = registers.is_a?(StaticRegisters) ? registers.static : registers
@registers = {}
end
+1 -1
View File
@@ -9,7 +9,7 @@ module Liquid
# The Strainer only allows method calls defined in filters given to it via Strainer.global_filter,
# Context#add_filters or Template.register_filter
class Strainer #:nodoc:
@@global_strainer = Class.new(Strainer) do
@@global_strainer = Class.new(Strainer) do
@filter_methods = Set.new
end
@@strainer_class_cache = Hash.new do |hash, filters|
+5 -5
View File
@@ -4,10 +4,10 @@ module Liquid
class TablerowloopDrop < Drop
def initialize(length, cols)
@length = length
@row = 1
@col = 1
@cols = cols
@index = 0
@row = 1
@col = 1
@cols = cols
@index = 0
end
attr_reader :length, :col, :row
@@ -54,7 +54,7 @@ module Liquid
@index += 1
if @col == @cols
@col = 1
@col = 1
@row += 1
else
@col += 1
+3 -3
View File
@@ -17,10 +17,10 @@ module Liquid
end
def initialize(tag_name, markup, parse_context)
@tag_name = tag_name
@markup = markup
@tag_name = tag_name
@markup = markup
@parse_context = parse_context
@line_number = parse_context.line_number
@line_number = parse_context.line_number
end
def parse(_tokens)
+4 -4
View File
@@ -21,7 +21,7 @@ module Liquid
def initialize(tag_name, markup, options)
super
if markup =~ SYNTAX
@to = Regexp.last_match(1)
@to = Regexp.last_match(1)
@from = Variable.new(Regexp.last_match(2), options)
else
raise SyntaxError, options[:locale].t(self.class.syntax_error_translation_key)
@@ -29,8 +29,8 @@ module Liquid
end
def render_to_output_buffer(context, output)
val = @from.render(context)
context.scopes.last[@to] = val
val = @from.render(context)
context.scopes.last[@to] = val
context.resource_limits.assign_score += assign_score_of(val)
output
end
@@ -45,7 +45,7 @@ module Liquid
if val.instance_of?(String)
val.bytesize
elsif val.instance_of?(Array) || val.instance_of?(Hash)
sum = 1
sum = 1
# Uses #each to avoid extra allocations.
val.each { |child| sum += assign_score_of(child) }
sum
+2 -2
View File
@@ -25,9 +25,9 @@ module Liquid
end
def render_to_output_buffer(context, output)
previous_output_size = output.bytesize
previous_output_size = output.bytesize
super
context.scopes.last[@to] = output
context.scopes.last[@to] = output
context.resource_limits.assign_score += (output.bytesize - previous_output_size)
output
end
+1 -1
View File
@@ -2,7 +2,7 @@
module Liquid
class Case < Block
SYNTAX = /(#{QUOTED_FRAGMENT})/o
SYNTAX = /(#{QUOTED_FRAGMENT})/o
WHEN_SYNTAX = /(#{QUOTED_FRAGMENT})(?:(?:\s+or\s+|\s*\,\s*)(#{QUOTED_FRAGMENT}.*))?/om
attr_reader :blocks, :left
+5 -5
View File
@@ -24,10 +24,10 @@ module Liquid
case markup
when NAMED_SYNTAX
@variables = variables_from_string(Regexp.last_match(2))
@name = Expression.parse(Regexp.last_match(1))
@name = Expression.parse(Regexp.last_match(1))
when SIMPLE_SYNTAX
@variables = variables_from_string(markup)
@name = @variables.to_s
@name = @variables.to_s
else
raise SyntaxError, options[:locale].t("errors.syntax.cycle")
end
@@ -36,7 +36,7 @@ module Liquid
def render_to_output_buffer(context, output)
context.registers[:cycle] ||= {}
key = context.evaluate(@name)
key = context.evaluate(@name)
iteration = context.registers[:cycle][key].to_i
val = context.evaluate(@variables[iteration])
@@ -49,8 +49,8 @@ module Liquid
output << val
iteration += 1
iteration = 0 if iteration >= @variables.size
iteration += 1
iteration = 0 if iteration >= @variables.size
context.registers[:cycle][key] = iteration
output
+2 -2
View File
@@ -26,8 +26,8 @@ module Liquid
end
def render_to_output_buffer(context, output)
value = context.environments.first[@variable] ||= 0
value -= 1
value = context.environments.first[@variable] ||= 0
value -= 1
context.environments.first[@variable] = value
output << value.to_s
output
+13 -13
View File
@@ -52,9 +52,9 @@ module Liquid
def initialize(tag_name, markup, options)
super
@from = @limit = nil
@from = @limit = nil
parse_with_selected_parser(markup)
@for_block = BlockBody.new
@for_block = BlockBody.new
@else_block = nil
end
@@ -88,10 +88,10 @@ module Liquid
def lax_parse(markup)
if markup =~ SYNTAX
@variable_name = Regexp.last_match(1)
collection_name = Regexp.last_match(2)
@reversed = !!Regexp.last_match(3)
@name = "#{@variable_name}-#{collection_name}"
@variable_name = Regexp.last_match(1)
collection_name = Regexp.last_match(2)
@reversed = !!Regexp.last_match(3)
@name = "#{@variable_name}-#{collection_name}"
@collection_name = Expression.parse(collection_name)
markup.scan(TAG_ATTRIBUTES) do |key, value|
set_attribute(key, value)
@@ -102,13 +102,13 @@ module Liquid
end
def strict_parse(markup)
p = Parser.new(markup)
@variable_name = p.consume(:id)
p = Parser.new(markup)
@variable_name = p.consume(:id)
raise SyntaxError, options[:locale].t("errors.syntax.for_invalid_in") unless p.id?('in')
collection_name = p.expression
@name = "#{@variable_name}-#{collection_name}"
collection_name = p.expression
@name = "#{@variable_name}-#{collection_name}"
@collection_name = Expression.parse(collection_name)
@reversed = p.id?('reversed')
@reversed = p.id?('reversed')
while p.look(:id) && p.look(:colon, 1)
unless (attribute = p.id?('limit') || p.id?('offset'))
@@ -140,7 +140,7 @@ module Liquid
collection = collection.to_a if collection.is_a?(Range)
limit_value = context.evaluate(@limit)
to = if limit_value.nil?
to = if limit_value.nil?
nil
else
Utils.to_integer(limit_value) + from
@@ -156,7 +156,7 @@ module Liquid
def render_segment(context, output, segment)
for_stack = context.registers[:for_stack] ||= []
length = segment.length
length = segment.length
context.stack do
loop_vars = Liquid::ForloopDrop.new(@name, length, for_stack[-1])
+8 -8
View File
@@ -12,9 +12,9 @@ module Liquid
# There are {% if count < 5 %} less {% else %} more {% endif %} items than you need.
#
class If < Block
SYNTAX = /(#{QUOTED_FRAGMENT})\s*([=!<>a-z_]+)?\s*(#{QUOTED_FRAGMENT})?/o
SYNTAX = /(#{QUOTED_FRAGMENT})\s*([=!<>a-z_]+)?\s*(#{QUOTED_FRAGMENT})?/o
EXPRESSIONS_AND_OPERATORS = /(?:\b(?:\s?and\s?|\s?or\s?)\b|(?:\s*(?!\b(?:\s?and\s?|\s?or\s?)\b)(?:#{QUOTED_FRAGMENT}|\S+)\s*)+)/o
BOOLEAN_OPERATORS = %w(and or).freeze
BOOLEAN_OPERATORS = %w(and or).freeze
attr_reader :blocks
@@ -78,32 +78,32 @@ module Liquid
new_condition = Condition.new(Expression.parse(Regexp.last_match(1)), Regexp.last_match(2), Expression.parse(Regexp.last_match(3)))
raise SyntaxError, options[:locale].t("errors.syntax.if") unless BOOLEAN_OPERATORS.include?(operator)
new_condition.send(operator, condition)
condition = new_condition
condition = new_condition
end
condition
end
def strict_parse(markup)
p = Parser.new(markup)
p = Parser.new(markup)
condition = parse_binary_comparisons(p)
p.consume(:end_of_string)
condition
end
def parse_binary_comparisons(p)
condition = parse_comparison(p)
condition = parse_comparison(p)
first_condition = condition
while (op = (p.id?('and') || p.id?('or')))
while (op = (p.id?('and') || p.id?('or')))
child_condition = parse_comparison(p)
condition.send(op, child_condition)
condition = child_condition
condition = child_condition
end
first_condition
end
def parse_comparison(p)
a = Expression.parse(p.expression)
a = Expression.parse(p.expression)
if (op = p.consume?(:comparison))
b = Expression.parse(p.expression)
Condition.new(a, op, b)
+4 -4
View File
@@ -30,7 +30,7 @@ module Liquid
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
@template_name_expr = Expression.parse(template_name)
@attributes = {}
@attributes = {}
markup.scan(TAG_ATTRIBUTES) do |key, value|
@attributes[key] = Expression.parse(value)
@@ -63,10 +63,10 @@ module Liquid
end
old_template_name = context.template_name
old_partial = context.partial
old_partial = context.partial
begin
context.template_name = template_name
context.partial = true
context.partial = true
context.stack do
@attributes.each do |key, value|
context[key] = context.evaluate(value)
@@ -84,7 +84,7 @@ module Liquid
end
ensure
context.template_name = old_template_name
context.partial = old_partial
context.partial = old_partial
end
output
+1 -1
View File
@@ -23,7 +23,7 @@ module Liquid
end
def render_to_output_buffer(context, output)
value = context.environments.first[@variable] ||= 0
value = context.environments.first[@variable] ||= 0
context.environments.first[@variable] = value + 1
output << value.to_s
+2 -2
View File
@@ -2,7 +2,7 @@
module Liquid
class Raw < Block
SYNTAX = /\A\s*\z/
SYNTAX = /\A\s*\z/
FULL_TOKEN_POSSIBLY_INVALID = /\A(.*)#{TAG_START}\s*(\w+)\s*(.*)?#{TAG_END}\z/om
def initialize(tag_name, markup, parse_context)
@@ -12,7 +12,7 @@ module Liquid
end
def parse(tokens)
@body = +''
@body = +''
while (token = tokens.shift)
if token =~ FULL_TOKEN_POSSIBLY_INVALID
@body << Regexp.last_match(1) if Regexp.last_match(1) != ""
+2 -2
View File
@@ -32,9 +32,9 @@ module Liquid
parse_context: parse_context
)
inner_context = context.new_isolated_subcontext
inner_context = context.new_isolated_subcontext
inner_context.template_name = template_name
inner_context.partial = true
inner_context.partial = true
@attributes.each do |key, value|
inner_context[key] = context.evaluate(value)
end
+4 -4
View File
@@ -9,9 +9,9 @@ module Liquid
def initialize(tag_name, markup, options)
super
if markup =~ SYNTAX
@variable_name = Regexp.last_match(1)
@variable_name = Regexp.last_match(1)
@collection_name = Expression.parse(Regexp.last_match(2))
@attributes = {}
@attributes = {}
markup.scan(TAG_ATTRIBUTES) do |key, value|
@attributes[key] = Expression.parse(value)
end
@@ -24,7 +24,7 @@ module Liquid
(collection = context.evaluate(@collection_name)) || (return '')
from = @attributes.key?('offset') ? context.evaluate(@attributes['offset']).to_i : 0
to = @attributes.key?('limit') ? from + context.evaluate(@attributes['limit']).to_i : nil
to = @attributes.key?('limit') ? from + context.evaluate(@attributes['limit']).to_i : nil
collection = Utils.slice_collection(collection, from, to)
@@ -34,7 +34,7 @@ module Liquid
output << "<tr class=\"row1\">\n"
context.stack do
tablerowloop = Liquid::TablerowloopDrop.new(length, cols)
tablerowloop = Liquid::TablerowloopDrop.new(length, cols)
context['tablerowloop'] = tablerowloop
collection.each do |item|
+11 -11
View File
@@ -24,7 +24,7 @@ module Liquid
include Enumerable
def initialize
@tags = {}
@tags = {}
@cache = {}
end
@@ -120,19 +120,19 @@ module Liquid
end
def initialize
@rethrow_errors = false
@rethrow_errors = false
@resource_limits = ResourceLimits.new(self.class.default_resource_limits)
end
# Parse source code.
# Returns self for easy chaining
def parse(source, options = {})
@options = options
@profiling = options[:profile]
@options = options
@profiling = options[:profile]
@line_numbers = options[:line_numbers] || @profiling
parse_context = options.is_a?(ParseContext) ? options : ParseContext.new(options)
@root = Document.parse(tokenize(source), parse_context)
@warnings = parse_context.warnings
@root = Document.parse(tokenize(source), parse_context)
@warnings = parse_context.warnings
self
end
@@ -179,7 +179,7 @@ module Liquid
c
when Liquid::Drop
drop = args.shift
drop = args.shift
drop.context = Context.new([drop, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits)
when Hash
Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits)
@@ -194,7 +194,7 @@ module Liquid
case args.last
when Hash
options = args.pop
output = options[:output] if options[:output]
output = options[:output] if options[:output]
registers.merge!(options[:registers]) if options[:registers].is_a?(Hash)
@@ -253,10 +253,10 @@ module Liquid
def apply_options_to_context(context, options)
context.add_filters(options[:filters]) if options[:filters]
context.global_filter = options[:global_filter] if options[:global_filter]
context.global_filter = options[:global_filter] if options[:global_filter]
context.exception_renderer = options[:exception_renderer] if options[:exception_renderer]
context.strict_variables = options[:strict_variables] if options[:strict_variables]
context.strict_filters = options[:strict_filters] if options[:strict_filters]
context.strict_variables = options[:strict_variables] if options[:strict_variables]
context.strict_filters = options[:strict_filters] if options[:strict_filters]
end
end
end
+3 -3
View File
@@ -5,10 +5,10 @@ module Liquid
attr_reader :line_number, :for_liquid_tag
def initialize(source, line_numbers = false, line_number: nil, for_liquid_tag: false)
@source = source
@line_number = line_number || (line_numbers ? 1 : nil)
@source = source
@line_number = line_number || (line_numbers ? 1 : nil)
@for_liquid_tag = for_liquid_tag
@tokens = tokenize
@tokens = tokenize
end
def shift
+1 -1
View File
@@ -12,7 +12,7 @@ module Liquid
def self.slice_collection_using_each(collection, from, to)
segments = []
index = 0
index = 0
# Maintains Ruby 1.8.7 String#each behaviour on 1.9
if collection.is_a?(String)
+15 -15
View File
@@ -12,10 +12,10 @@ module Liquid
# {{ user | link }}
#
class Variable
FILTER_MARKUP_REGEX = /#{FILTER_SEPARATOR}\s*(.*)/om
FILTER_PARSER = /(?:\s+|#{QUOTED_FRAGMENT}|#{ARGUMENT_SEPARATOR})+/o
FILTER_ARGS_REGEX = /(?:#{FILTER_ARGUMENT_SEPARATOR}|#{ARGUMENT_SEPARATOR})\s*((?:\w+\s*\:\s*)?#{QUOTED_FRAGMENT})/o
JUST_TAG_ATTRIBUTES = /\A#{TAG_ATTRIBUTES}\z/o
FILTER_MARKUP_REGEX = /#{FILTER_SEPARATOR}\s*(.*)/om
FILTER_PARSER = /(?:\s+|#{QUOTED_FRAGMENT}|#{ARGUMENT_SEPARATOR})+/o
FILTER_ARGS_REGEX = /(?:#{FILTER_ARGUMENT_SEPARATOR}|#{ARGUMENT_SEPARATOR})\s*((?:\w+\s*\:\s*)?#{QUOTED_FRAGMENT})/o
JUST_TAG_ATTRIBUTES = /\A#{TAG_ATTRIBUTES}\z/o
MARKUP_WITH_QUOTED_FRAGMENT = /(#{QUOTED_FRAGMENT})(.*)/om
attr_accessor :filters, :name, :line_number
@@ -25,10 +25,10 @@ module Liquid
include ParserSwitching
def initialize(markup, parse_context)
@markup = markup
@name = nil
@markup = markup
@name = nil
@parse_context = parse_context
@line_number = parse_context.line_number
@line_number = parse_context.line_number
parse_with_selected_parser(markup)
end
@@ -45,9 +45,9 @@ module Liquid
@filters = []
return unless markup =~ MARKUP_WITH_QUOTED_FRAGMENT
name_markup = Regexp.last_match(1)
name_markup = Regexp.last_match(1)
filter_markup = Regexp.last_match(2)
@name = Expression.parse(name_markup)
@name = Expression.parse(name_markup)
if filter_markup =~ FILTER_MARKUP_REGEX
filters = Regexp.last_match(1).scan(FILTER_PARSER)
filters.each do |f|
@@ -61,7 +61,7 @@ module Liquid
def strict_parse(markup)
@filters = []
p = Parser.new(markup)
p = Parser.new(markup)
@name = Expression.parse(p.expression)
while p.consume?(:pipe)
@@ -107,17 +107,17 @@ module Liquid
private
def parse_filter_expressions(filter_name, unparsed_args)
filter_args = []
filter_args = []
keyword_args = nil
unparsed_args.each do |a|
if (matches = a.match(JUST_TAG_ATTRIBUTES))
keyword_args ||= {}
keyword_args ||= {}
keyword_args[matches[1]] = Expression.parse(matches[2])
else
filter_args << Expression.parse(a)
end
end
result = [filter_name, filter_args]
result = [filter_name, filter_args]
result << keyword_args if keyword_args
result
end
@@ -141,8 +141,8 @@ module Liquid
@markup =~ QUOTED_FRAGMENT
name = Regexp.last_match(0)
error = TaintedError.new("variable '#{name}' is tainted and was not escaped")
error.line_number = line_number
error = TaintedError.new("variable '#{name}' is tainted and was not escaped")
error.line_number = line_number
error.template_name = context.template_name
case Template.taint_mode
+5 -5
View File
@@ -3,7 +3,7 @@
module Liquid
class VariableLookup
SQUARE_BRACKETED = /\A\[(.*)\]\z/m
COMMAND_METHODS = ['size', 'first', 'last'].freeze
COMMAND_METHODS = ['size', 'first', 'last'].freeze
attr_reader :name, :lookups
@@ -14,13 +14,13 @@ module Liquid
def initialize(markup)
lookups = markup.scan(VARIABLE_PARSER)
name = lookups.shift
name = lookups.shift
if name =~ SQUARE_BRACKETED
name = Expression.parse(Regexp.last_match(1))
end
@name = name
@lookups = lookups
@lookups = lookups
@command_flags = 0
@lookups.each_index do |i|
@@ -34,7 +34,7 @@ module Liquid
end
def evaluate(context)
name = context.evaluate(@name)
name = context.evaluate(@name)
object = context.find_variable(name)
@lookups.each_index do |i|
@@ -47,7 +47,7 @@ module Liquid
(object.respond_to?(:fetch) && key.is_a?(Integer)))
# if its a proc we will replace the entry with the proc
res = context.lookup_and_evaluate(object, key)
res = context.lookup_and_evaluate(object, key)
object = res.to_liquid
# Some special cases. If the part wasn't in square brackets and