Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undo addition of whitespace to improve readability #1

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions example/server/liquid_servlet.rb
Expand Up @@ -20,8 +20,8 @@ def handle(_type, req, res)
@assigns = send(@action) if respond_to?(@action)

@response['Content-Type'] = "text/html"
@response.status = 200
@response.body = Liquid::Template.parse(read_template).render(@assigns, filters: [ProductsFilter])
@response.status = 200
@response.body = Liquid::Template.parse(read_template).render(@assigns, filters: [ProductsFilter])
end

def read_template(filename = @action)
Expand Down
10 changes: 5 additions & 5 deletions lib/liquid/block_body.rb
Expand Up @@ -34,8 +34,8 @@ def parse(tokenizer, parse_context, &block)
# 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
Expand Down Expand Up @@ -79,8 +79,8 @@ def parse(tokenizer, parse_context, &block)
# 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?(VARSTART)
whitespace_handler(token, parse_context)
Expand Down Expand Up @@ -121,7 +121,7 @@ def render(context)
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

Expand Down
9 changes: 5 additions & 4 deletions lib/liquid/condition.rb
Expand Up @@ -35,16 +35,17 @@ def self.operators
attr_accessor :left, :operator, :right

def initialize(left = nil, operator = nil, right = nil)
@left = left
@operator = operator
@right = right
@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)

Expand Down
12 changes: 6 additions & 6 deletions lib/liquid/context.rb
Expand Up @@ -60,7 +60,7 @@ def strainer
# 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
Expand All @@ -85,7 +85,7 @@ def pop_interrupt
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
errors.push(e)
Expand Down Expand Up @@ -140,10 +140,10 @@ def new_isolated_subcontext
).tap do |subcontext|
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

Expand Down
2 changes: 1 addition & 1 deletion lib/liquid/expression.rb
Expand Up @@ -7,7 +7,7 @@ class MethodLiteral

def initialize(method_name, to_s)
@method_name = method_name
@to_s = to_s
@to_s = to_s
end

def to_liquid
Expand Down
15 changes: 9 additions & 6 deletions lib/liquid/parse_context.rb
Expand Up @@ -7,19 +7,22 @@ class ParseContext

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)
@options[option_key]
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

Expand Down
9 changes: 5 additions & 4 deletions lib/liquid/partial_cache.rb
Expand Up @@ -4,14 +4,15 @@ 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
Expand Down
4 changes: 2 additions & 2 deletions lib/liquid/profiler.rb
Expand Up @@ -121,12 +121,12 @@ def initialize(partial_name = "<root>")

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
Expand Down
19 changes: 12 additions & 7 deletions lib/liquid/standardfilters.rb
Expand Up @@ -76,20 +76,25 @@ def slice(input, offset, length = nil)
# 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

l = words - 1
l = 0 if l < 0

wordlist.length > l ? wordlist[0..l].join(" ").concat(truncate_string.to_s) : input
end

Expand Down Expand Up @@ -432,7 +437,7 @@ def at_most(input, n)
# {{ product.title | default: "No Title", allow_false: true }}
#
def default(input, default_value = '', options = {})
options = {} unless options.is_a?(Hash)
options = {} unless options.is_a?(Hash)
false_check = options['allow_false'] ? input.nil? : !input
false_check || (input.respond_to?(:empty?) && input.empty?) ? default_value : input
end
Expand Down
2 changes: 1 addition & 1 deletion lib/liquid/tablerowloop_drop.rb
Expand Up @@ -54,7 +54,7 @@ def increment!
@index += 1

if @col == @cols
@col = 1
@col = 1
@row += 1
else
@col += 1
Expand Down
6 changes: 3 additions & 3 deletions lib/liquid/tags/assign.rb
Expand Up @@ -29,8 +29,8 @@ def initialize(tag_name, markup, options)
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
Expand All @@ -45,7 +45,7 @@ def assign_score_of(val)
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
Expand Down
2 changes: 1 addition & 1 deletion lib/liquid/tags/capture.rb
Expand Up @@ -27,7 +27,7 @@ def initialize(tag_name, markup, options)
def render_to_output_buffer(context, output)
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
Expand Down
6 changes: 3 additions & 3 deletions lib/liquid/tags/cycle.rb
Expand Up @@ -49,10 +49,10 @@ def render_to_output_buffer(context, output)

output << val

iteration += 1
iteration = 0 if iteration >= @variables.size
context.registers[:cycle][key] = iteration
iteration += 1
iteration = 0 if iteration >= @variables.size

context.registers[:cycle][key] = iteration
output
end

Expand Down
12 changes: 7 additions & 5 deletions lib/liquid/tags/for.rb
Expand Up @@ -102,13 +102,15 @@ def lax_parse(markup)
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 = Expression.parse(collection_name)
@reversed = p.id?('reversed')

@name = "#{@variable_name}-#{collection_name}"
@reversed = p.id?('reversed')

while p.look(:id) && p.look(:colon, 1)
unless (attribute = p.id?('limit') || p.id?('offset'))
Expand Down Expand Up @@ -140,7 +142,7 @@ def collection_segment(context)
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
Expand Down
8 changes: 4 additions & 4 deletions lib/liquid/tags/if.rb
Expand Up @@ -85,16 +85,16 @@ def lax_parse(markup)
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
Expand All @@ -103,7 +103,7 @@ def parse_binary_comparisons(p)
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)
Expand Down
2 changes: 1 addition & 1 deletion lib/liquid/tags/increment.rb
Expand Up @@ -23,7 +23,7 @@ def initialize(tag_name, markup, options)
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
Expand Down
4 changes: 2 additions & 2 deletions lib/liquid/tags/raw.rb
Expand Up @@ -2,7 +2,7 @@

module Liquid
class Raw < Block
Syntax = /\A\s*\z/
Syntax = /\A\s*\z/
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om

def initialize(tag_name, markup, parse_context)
Expand All @@ -12,7 +12,7 @@ def initialize(tag_name, markup, parse_context)
end

def parse(tokens)
@body = +''
@body = +''
while (token = tokens.shift)
if token =~ FullTokenPossiblyInvalid
@body << Regexp.last_match(1) if Regexp.last_match(1) != ""
Expand Down
3 changes: 2 additions & 1 deletion lib/liquid/tags/render.rb
Expand Up @@ -16,7 +16,7 @@ def initialize(tag_name, markup, options)
template_name = Regexp.last_match(1)
variable_name = Regexp.last_match(3)

@alias_name = Regexp.last_match(5)
@alias_name = Regexp.last_match(5)
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
@template_name_expr = Expression.parse(template_name)

Expand Down Expand Up @@ -48,6 +48,7 @@ def render_tag(context, output)
inner_context.template_name = template_name
inner_context.partial = true
inner_context['forloop'] = forloop if forloop

@attributes.each do |key, value|
inner_context[key] = context.evaluate(value)
end
Expand Down
7 changes: 3 additions & 4 deletions lib/liquid/tags/table_row.rb
Expand Up @@ -24,17 +24,16 @@ def render_to_output_buffer(context, output)
(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)

length = collection.length
length = collection.length

cols = context.evaluate(@attributes['cols']).to_i

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|
Expand Down
2 changes: 1 addition & 1 deletion lib/liquid/variable.rb
Expand Up @@ -61,7 +61,7 @@ def lax_parse(markup)

def strict_parse(markup)
@filters = []
p = Parser.new(markup)
p = Parser.new(markup)

@name = Expression.parse(p.expression)
while p.consume?(:pipe)
Expand Down