Module: Liquid::StandardFilters

Defined in:
lib/liquid/standardfilters.rb

Defined Under Namespace

Classes: InputIterator

Constant Summary collapse

HTML_ESCAPE =
{
  '&' => '&',
  '>' => '>',
  '<' => '&lt;',
  '"' => '&quot;',
  "'" => '&#39;',
}.freeze
HTML_ESCAPE_ONCE_REGEXP =
/["><']|&(?!([a-zA-Z]+|(#\d+));)/
STRIP_HTML_BLOCKS =
Regexp.union(
  %r{<script.*?</script>}m,
  /<!--.*?-->/m,
  %r{<style.*?</style>}m,
)
STRIP_HTML_TAGS =
/<.*?>/m

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.try_coerce_encoding(input, encoding:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/liquid/standardfilters.rb', line 32

def try_coerce_encoding(input, encoding:)
  original_encoding = input.encoding
  if input.encoding != encoding
    input.force_encoding(encoding)
    unless input.valid_encoding?
      input.force_encoding(original_encoding)
    end
  end
  input
end

Instance Method Details

#abs(input) ⇒ Object



809
810
811
812
# File 'lib/liquid/standardfilters.rb', line 809

def abs(input)
  result = Utils.to_number(input).abs
  result.is_a?(BigDecimal) ? result.to_f : result
end

#append(input, string) ⇒ Object



678
679
680
681
682
# File 'lib/liquid/standardfilters.rb', line 678

def append(input, string)
  input = Utils.to_s(input)
  string = Utils.to_s(string)
  input + string
end

#at_least(input, n) ⇒ Object



922
923
924
925
926
927
928
# File 'lib/liquid/standardfilters.rb', line 922

def at_least(input, n)
  min_value = Utils.to_number(n)

  result = Utils.to_number(input)
  result = min_value if min_value > result
  result.is_a?(BigDecimal) ? result.to_f : result
end

#at_most(input, n) ⇒ Object



937
938
939
940
941
942
943
# File 'lib/liquid/standardfilters.rb', line 937

def at_most(input, n)
  max_value = Utils.to_number(n)

  result = Utils.to_number(input)
  result = max_value if max_value < result
  result.is_a?(BigDecimal) ? result.to_f : result
end

#base64_decode(input) ⇒ Object



164
165
166
167
168
169
# File 'lib/liquid/standardfilters.rb', line 164

def base64_decode(input)
  input = Utils.to_s(input)
  StandardFilters.try_coerce_encoding(Base64.strict_decode64(input), encoding: input.encoding)
rescue ::ArgumentError
  raise Liquid::ArgumentError, "invalid base64 provided to base64_decode"
end

#base64_encode(input) ⇒ Object



153
154
155
# File 'lib/liquid/standardfilters.rb', line 153

def base64_encode(input)
  Base64.strict_encode64(Utils.to_s(input))
end

#base64_url_safe_decode(input) ⇒ Object



189
190
191
192
193
194
# File 'lib/liquid/standardfilters.rb', line 189

def base64_url_safe_decode(input)
  input = Utils.to_s(input)
  StandardFilters.try_coerce_encoding(Base64.urlsafe_decode64(input), encoding: input.encoding)
rescue ::ArgumentError
  raise Liquid::ArgumentError, "invalid base64 provided to base64_url_safe_decode"
end

#base64_url_safe_encode(input) ⇒ Object



178
179
180
# File 'lib/liquid/standardfilters.rb', line 178

def base64_url_safe_encode(input)
  Base64.urlsafe_encode64(Utils.to_s(input))
end

#capitalize(input) ⇒ Object



87
88
89
# File 'lib/liquid/standardfilters.rb', line 87

def capitalize(input)
  Utils.to_s(input).capitalize
end

#ceil(input) ⇒ Object



896
897
898
899
900
# File 'lib/liquid/standardfilters.rb', line 896

def ceil(input)
  Utils.to_number(input).ceil.to_i
rescue ::FloatDomainError => e
  raise Liquid::FloatDomainError, e.message
end

#compact(input, property = nil) ⇒ Object



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/liquid/standardfilters.rb', line 570

def compact(input, property = nil)
  ary = InputIterator.new(input, context)

  if property.nil?
    ary.compact
  elsif ary.empty? # The next two cases assume a non-empty array.
    []
  else
    ary.reject do |item|
      item[property].nil?
    rescue TypeError
      raise_property_error(property)
    rescue NoMethodError
      return nil unless item.respond_to?(:[])
      raise
    end
  end
end

#concat(input, array) ⇒ Object



695
696
697
698
699
700
# File 'lib/liquid/standardfilters.rb', line 695

def concat(input, array)
  unless array.respond_to?(:to_ary)
    raise ArgumentError, "concat filter requires an array argument"
  end
  InputIterator.new(input, context).concat(array)
end

#date(input, format) ⇒ Object



767
768
769
770
771
772
773
774
# File 'lib/liquid/standardfilters.rb', line 767

def date(input, format)
  str_format = Utils.to_s(format)
  return input if str_format.empty?

  return input unless (date = Utils.to_date(input))

  date.strftime(str_format)
end

#default(input, default_value = '', options = {}) ⇒ Object



957
958
959
960
961
# File 'lib/liquid/standardfilters.rb', line 957

def default(input, default_value = '', options = {})
  options = {} unless options.is_a?(Hash)
  false_check = options['allow_false'] ? input.nil? : !Liquid::Utils.to_liquid_value(input)
  false_check || (input.respond_to?(:empty?) && input.empty?) ? default_value : input
end

#divided_by(input, operand) ⇒ Object



854
855
856
857
858
# File 'lib/liquid/standardfilters.rb', line 854

def divided_by(input, operand)
  apply_operation(input, operand, :/)
rescue ::ZeroDivisionError => e
  raise Liquid::ZeroDivisionError, e.message
end

#downcase(input) ⇒ Object



65
66
67
# File 'lib/liquid/standardfilters.rb', line 65

def downcase(input)
  Utils.to_s(input).downcase
end

#escape(input) ⇒ Object Also known as: h



98
99
100
# File 'lib/liquid/standardfilters.rb', line 98

def escape(input)
  CGI.escapeHTML(Utils.to_s(input)) unless input.nil?
end

#escape_once(input) ⇒ Object



110
111
112
# File 'lib/liquid/standardfilters.rb', line 110

def escape_once(input)
  Utils.to_s(input).gsub(HTML_ESCAPE_ONCE_REGEXP, HTML_ESCAPE)
end

#find(input, property, target_value = nil) ⇒ Object



486
487
488
# File 'lib/liquid/standardfilters.rb', line 486

def find(input, property, target_value = nil)
  filter_array(input, property, target_value, nil) { |ary, &block| ary.find(&block) }
end

#find_index(input, property, target_value = nil) ⇒ Object



499
500
501
# File 'lib/liquid/standardfilters.rb', line 499

def find_index(input, property, target_value = nil)
  filter_array(input, property, target_value, nil) { |ary, &block| ary.find_index(&block) }
end

#first(array) ⇒ Object



783
784
785
786
787
# File 'lib/liquid/standardfilters.rb', line 783

def first(array)
  # ActiveSupport returns "" for empty strings, not nil
  return array[0] || "" if array.is_a?(String)
  array.first if array.respond_to?(:first)
end

#floor(input) ⇒ Object



909
910
911
912
913
# File 'lib/liquid/standardfilters.rb', line 909

def floor(input)
  Utils.to_number(input).floor.to_i
rescue ::FloatDomainError => e
  raise Liquid::FloatDomainError, e.message
end

#has(input, property, target_value = nil) ⇒ Object



473
474
475
# File 'lib/liquid/standardfilters.rb', line 473

def has(input, property, target_value = nil)
  filter_array(input, property, target_value, false) { |ary, &block| ary.any?(&block) }
end

#join(input, glue = ' ') ⇒ Object



379
380
381
382
# File 'lib/liquid/standardfilters.rb', line 379

def join(input, glue = ' ')
  glue = Utils.to_s(glue)
  InputIterator.new(input, context).join(glue)
end

#last(array) ⇒ Object



796
797
798
799
800
# File 'lib/liquid/standardfilters.rb', line 796

def last(array)
  # ActiveSupport returns "" for empty strings, not nil
  return array[-1] || "" if array.is_a?(String)
  array.last if array.respond_to?(:last)
end

#lstrip(input) ⇒ Object



328
329
330
331
# File 'lib/liquid/standardfilters.rb', line 328

def lstrip(input)
  input = Utils.to_s(input)
  input.lstrip
end

#map(input, property) ⇒ Object



548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/liquid/standardfilters.rb', line 548

def map(input, property)
  InputIterator.new(input, context).map do |e|
    e = e.call if e.is_a?(Proc)

    if property == "to_liquid"
      e
    elsif e.respond_to?(:[])
      r = e[property]
      r.is_a?(Proc) ? r.call : r
    end
  end
rescue TypeError
  raise_property_error(property)
end

#minus(input, operand) ⇒ Object



832
833
834
# File 'lib/liquid/standardfilters.rb', line 832

def minus(input, operand)
  apply_operation(input, operand, :-)
end

#modulo(input, operand) ⇒ Object



867
868
869
870
871
# File 'lib/liquid/standardfilters.rb', line 867

def modulo(input, operand)
  apply_operation(input, operand, :%)
rescue ::ZeroDivisionError => e
  raise Liquid::ZeroDivisionError, e.message
end

#newline_to_br(input) ⇒ Object



722
723
724
725
# File 'lib/liquid/standardfilters.rb', line 722

def newline_to_br(input)
  input = Utils.to_s(input)
  input.gsub(/\r?\n/, "<br />\n")
end

#plus(input, operand) ⇒ Object



821
822
823
# File 'lib/liquid/standardfilters.rb', line 821

def plus(input, operand)
  apply_operation(input, operand, :+)
end

#prepend(input, string) ⇒ Object



709
710
711
712
713
# File 'lib/liquid/standardfilters.rb', line 709

def prepend(input, string)
  input = Utils.to_s(input)
  string = Utils.to_s(string)
  string + input
end

#reject(input, property, target_value = nil) ⇒ Object



460
461
462
# File 'lib/liquid/standardfilters.rb', line 460

def reject(input, property, target_value = nil)
  filter_array(input, property, target_value) { |ary, &block| ary.reject(&block) }
end

#remove(input, string) ⇒ Object



645
646
647
# File 'lib/liquid/standardfilters.rb', line 645

def remove(input, string)
  replace(input, string, '')
end

#remove_first(input, string) ⇒ Object



656
657
658
# File 'lib/liquid/standardfilters.rb', line 656

def remove_first(input, string)
  replace_first(input, string, '')
end

#remove_last(input, string) ⇒ Object



667
668
669
# File 'lib/liquid/standardfilters.rb', line 667

def remove_last(input, string)
  replace_last(input, string, '')
end

#replace(input, string, replacement = '') ⇒ Object



596
597
598
599
600
601
# File 'lib/liquid/standardfilters.rb', line 596

def replace(input, string, replacement = '')
  string = Utils.to_s(string)
  replacement = Utils.to_s(replacement)
  input = Utils.to_s(input)
  input.gsub(string, replacement)
end

#replace_first(input, string, replacement = '') ⇒ Object



610
611
612
613
614
615
# File 'lib/liquid/standardfilters.rb', line 610

def replace_first(input, string, replacement = '')
  string = Utils.to_s(string)
  replacement = Utils.to_s(replacement)
  input = Utils.to_s(input)
  input.sub(string, replacement)
end

#replace_last(input, string, replacement) ⇒ Object



624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/liquid/standardfilters.rb', line 624

def replace_last(input, string, replacement)
  input = Utils.to_s(input)
  string = Utils.to_s(string)
  replacement = Utils.to_s(replacement)

  start_index = input.rindex(string)

  return input unless start_index

  output = input.dup
  output[start_index, string.length] = replacement
  output
end

#reverse(input) ⇒ Object



536
537
538
539
# File 'lib/liquid/standardfilters.rb', line 536

def reverse(input)
  ary = InputIterator.new(input, context)
  ary.reverse
end

#round(input, n = 0) ⇒ Object



880
881
882
883
884
885
886
887
# File 'lib/liquid/standardfilters.rb', line 880

def round(input, n = 0)
  result = Utils.to_number(input).round(Utils.to_number(n))
  result = result.to_f if result.is_a?(BigDecimal)
  result = result.to_i if n == 0
  result
rescue ::FloatDomainError => e
  raise Liquid::FloatDomainError, e.message
end

#rstrip(input) ⇒ Object



340
341
342
343
# File 'lib/liquid/standardfilters.rb', line 340

def rstrip(input)
  input = Utils.to_s(input)
  input.rstrip
end

#size(input) ⇒ Object



54
55
56
# File 'lib/liquid/standardfilters.rb', line 54

def size(input)
  input.respond_to?(:size) ? input.size : 0
end

#slice(input, offset, length = nil) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/liquid/standardfilters.rb', line 206

def slice(input, offset, length = nil)
  offset = Utils.to_integer(offset)
  length = length ? Utils.to_integer(length) : 1

  begin
    if input.is_a?(Array)
      input.slice(offset, length) || []
    else
      Utils.to_s(input).slice(offset, length) || ''
    end
  rescue RangeError
    if I64_RANGE.cover?(length) && I64_RANGE.cover?(offset)
      raise # unexpected error
    end
    offset = offset.clamp(I64_RANGE)
    length = length.clamp(I64_RANGE)
    retry
  end
end

#sort(input, property = nil) ⇒ Object



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/liquid/standardfilters.rb', line 391

def sort(input, property = nil)
  ary = InputIterator.new(input, context)

  return [] if ary.empty?

  if property.nil?
    ary.sort do |a, b|
      nil_safe_compare(a, b)
    end
  elsif ary.all? { |el| el.respond_to?(:[]) }
    begin
      ary.sort { |a, b| nil_safe_compare(a[property], b[property]) }
    rescue TypeError
      raise_property_error(property)
    end
  end
end

#sort_natural(input, property = nil) ⇒ Object



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/liquid/standardfilters.rb', line 420

def sort_natural(input, property = nil)
  ary = InputIterator.new(input, context)

  return [] if ary.empty?

  if property.nil?
    ary.sort do |a, b|
      nil_safe_casecmp(a, b)
    end
  elsif ary.all? { |el| el.respond_to?(:[]) }
    begin
      ary.sort { |a, b| nil_safe_casecmp(a[property], b[property]) }
    rescue TypeError
      raise_property_error(property)
    end
  end
end

#split(input, pattern) ⇒ Object



290
291
292
293
294
# File 'lib/liquid/standardfilters.rb', line 290

def split(input, pattern)
  pattern = Utils.to_s(pattern)
  input = Utils.to_s(input)
  input.split(pattern)
end

#squish(input) ⇒ Object



303
304
305
306
307
# File 'lib/liquid/standardfilters.rb', line 303

def squish(input)
  return if input.nil?

  Utils.to_s(input).strip.gsub(/\s+/, ' ')
end

#strip(input) ⇒ Object



316
317
318
319
# File 'lib/liquid/standardfilters.rb', line 316

def strip(input)
  input = Utils.to_s(input)
  input.strip
end

#strip_html(input) ⇒ Object



352
353
354
355
356
357
358
# File 'lib/liquid/standardfilters.rb', line 352

def strip_html(input)
  input = Utils.to_s(input)
  empty  = ''
  result = input.gsub(STRIP_HTML_BLOCKS, empty)
  result.gsub!(STRIP_HTML_TAGS, empty)
  result
end

#strip_newlines(input) ⇒ Object



367
368
369
370
# File 'lib/liquid/standardfilters.rb', line 367

def strip_newlines(input)
  input = Utils.to_s(input)
  input.gsub(/\r?\n/, '')
end

#sum(input, property = nil) ⇒ Object



970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
# File 'lib/liquid/standardfilters.rb', line 970

def sum(input, property = nil)
  ary = InputIterator.new(input, context)
  return 0 if ary.empty?

  values_for_sum = ary.map do |item|
    if property.nil?
      item
    elsif item.respond_to?(:[])
      item[property]
    else
      0
    end
  rescue TypeError
    raise_property_error(property)
  end

  result = InputIterator.new(values_for_sum, context).sum do |item|
    Utils.to_number(item)
  end

  result.is_a?(BigDecimal) ? result.to_f : result
end

#times(input, operand) ⇒ Object



843
844
845
# File 'lib/liquid/standardfilters.rb', line 843

def times(input, operand)
  apply_operation(input, operand, :*)
end

#truncate(input, length = 50, truncate_string = "...") ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/liquid/standardfilters.rb', line 236

def truncate(input, length = 50, truncate_string = "...")
  return if input.nil?
  input_str = Utils.to_s(input)
  length    = Utils.to_integer(length)

  truncate_string_str = Utils.to_s(truncate_string)

  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

#truncatewords(input, words = 15, truncate_string = "...") ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/liquid/standardfilters.rb', line 263

def truncatewords(input, words = 15, truncate_string = "...")
  return if input.nil?
  input = Utils.to_s(input)
  words = Utils.to_integer(words)
  words = 1 if words <= 0

  wordlist = begin
    input.split(" ", words + 1)
  rescue RangeError
    # integer too big for String#split, but we can semantically assume no truncation is needed
    return input if words + 1 > MAX_I32
    raise # unexpected error
  end
  return input if wordlist.length <= words

  wordlist.pop
  truncate_string = Utils.to_s(truncate_string)
  wordlist.join(" ").concat(truncate_string)
end

#uniq(input, property = nil) ⇒ Object



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/liquid/standardfilters.rb', line 510

def uniq(input, property = nil)
  ary = InputIterator.new(input, context)

  if property.nil?
    ary.uniq
  elsif ary.empty? # The next two cases assume a non-empty array.
    []
  else
    ary.uniq do |item|
      item[property]
    rescue TypeError
      raise_property_error(property)
    rescue NoMethodError
      return nil unless item.respond_to?(:[])
      raise
    end
  end
end

#upcase(input) ⇒ Object



76
77
78
# File 'lib/liquid/standardfilters.rb', line 76

def upcase(input)
  Utils.to_s(input).upcase
end

#url_decode(input) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/liquid/standardfilters.rb', line 137

def url_decode(input)
  return if input.nil?

  result = CGI.unescape(Utils.to_s(input))
  raise Liquid::ArgumentError, "invalid byte sequence in #{result.encoding}" unless result.valid_encoding?

  result
end

#url_encode(input) ⇒ Object



125
126
127
# File 'lib/liquid/standardfilters.rb', line 125

def url_encode(input)
  CGI.escape(Utils.to_s(input)) unless input.nil?
end

#where(input, property, target_value = nil) ⇒ Object



447
448
449
# File 'lib/liquid/standardfilters.rb', line 447

def where(input, property, target_value = nil)
  filter_array(input, property, target_value) { |ary, &block| ary.select(&block) }
end