Module: Liquid::StandardFilters
- Defined in:
- lib/liquid/standardfilters.rb
Defined Under Namespace
Classes: InputIterator
Constant Summary collapse
- MAX_INT =
(1 << 31) - 1
- HTML_ESCAPE =
{ '&' => '&', '>' => '>', '<' => '<', '"' => '"', "'" => ''', }.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
Instance Method Summary collapse
- #abs(input) ⇒ Object
- #append(input, string) ⇒ Object
- #at_least(input, n) ⇒ Object
- #at_most(input, n) ⇒ Object
- #base64_decode(input) ⇒ Object
- #base64_encode(input) ⇒ Object
- #base64_url_safe_decode(input) ⇒ Object
- #base64_url_safe_encode(input) ⇒ Object
- #capitalize(input) ⇒ Object
- #ceil(input) ⇒ Object
- #compact(input, property = nil) ⇒ Object
- #concat(input, array) ⇒ Object
-
#date(input, format) ⇒ Object
Reformat a date using Ruby’s core Time#strftime( string ) -> string.
- #default(input, default_value = '', options = {}) ⇒ Object
- #divided_by(input, operand) ⇒ Object
- #downcase(input) ⇒ Object
- #escape(input) ⇒ Object (also: #h)
- #escape_once(input) ⇒ Object
- #first(array) ⇒ Object
- #floor(input) ⇒ Object
- #join(input, glue = ' ') ⇒ Object
- #last(array) ⇒ Object
- #lstrip(input) ⇒ Object
- #map(input, property) ⇒ Object
- #minus(input, operand) ⇒ Object
- #modulo(input, operand) ⇒ Object
- #newline_to_br(input) ⇒ Object
- #plus(input, operand) ⇒ Object
- #prepend(input, string) ⇒ Object
- #remove(input, string) ⇒ Object
- #remove_first(input, string) ⇒ Object
- #remove_last(input, string) ⇒ Object
- #replace(input, string, replacement = '') ⇒ Object
- #replace_first(input, string, replacement = '') ⇒ Object
- #replace_last(input, string, replacement) ⇒ Object
- #reverse(input) ⇒ Object
- #round(input, n = 0) ⇒ Object
- #rstrip(input) ⇒ Object
- #size(input) ⇒ Object
- #slice(input, offset, length = nil) ⇒ Object
- #sort(input, property = nil) ⇒ Object
- #sort_natural(input, property = nil) ⇒ Object
- #split(input, pattern) ⇒ Object
- #strip(input) ⇒ Object
- #strip_html(input) ⇒ Object
- #strip_newlines(input) ⇒ Object
- #times(input, operand) ⇒ Object
- #truncate(input, length = 50, truncate_string = "...") ⇒ Object
- #truncatewords(input, words = 15, truncate_string = "...") ⇒ Object
- #uniq(input, property = nil) ⇒ Object
- #upcase(input) ⇒ Object
- #url_decode(input) ⇒ Object
- #url_encode(input) ⇒ Object
- #where(input, property, target_value = nil) ⇒ Object
Instance Method Details
#abs(input) ⇒ Object
702 703 704 705 |
# File 'lib/liquid/standardfilters.rb', line 702 def abs(input) result = Utils.to_number(input).abs result.is_a?(BigDecimal) ? result.to_f : result end |
#append(input, string) ⇒ Object
590 591 592 |
# File 'lib/liquid/standardfilters.rb', line 590 def append(input, string) input.to_s + string.to_s end |
#at_least(input, n) ⇒ Object
815 816 817 818 819 820 821 |
# File 'lib/liquid/standardfilters.rb', line 815 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
830 831 832 833 834 835 836 |
# File 'lib/liquid/standardfilters.rb', line 830 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
145 146 147 148 149 |
# File 'lib/liquid/standardfilters.rb', line 145 def base64_decode(input) Base64.strict_decode64(input.to_s) rescue ::ArgumentError raise Liquid::ArgumentError, "invalid base64 provided to base64_decode" end |
#base64_encode(input) ⇒ Object
134 135 136 |
# File 'lib/liquid/standardfilters.rb', line 134 def base64_encode(input) Base64.strict_encode64(input.to_s) end |
#base64_url_safe_decode(input) ⇒ Object
169 170 171 172 173 |
# File 'lib/liquid/standardfilters.rb', line 169 def base64_url_safe_decode(input) Base64.urlsafe_decode64(input.to_s) rescue ::ArgumentError raise Liquid::ArgumentError, "invalid base64 provided to base64_url_safe_decode" end |
#base64_url_safe_encode(input) ⇒ Object
158 159 160 |
# File 'lib/liquid/standardfilters.rb', line 158 def base64_url_safe_encode(input) Base64.urlsafe_encode64(input.to_s) end |
#capitalize(input) ⇒ Object
68 69 70 |
# File 'lib/liquid/standardfilters.rb', line 68 def capitalize(input) input.to_s.capitalize end |
#ceil(input) ⇒ Object
789 790 791 792 793 |
# File 'lib/liquid/standardfilters.rb', line 789 def ceil(input) Utils.to_number(input).ceil.to_i rescue ::FloatDomainError => e raise Liquid::FloatDomainError, e. end |
#compact(input, property = nil) ⇒ Object
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/liquid/standardfilters.rb', line 488 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
605 606 607 608 609 610 |
# File 'lib/liquid/standardfilters.rb', line 605 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
Reformat a date using Ruby’s core Time#strftime( string ) -> string
%a - The abbreviated weekday name (``Sun'')
%A - The full weekday name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The full month name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM'' or ``PM'')
%s - Number of seconds since 1970-01-01 00:00:00 UTC.
%S - Second of the minute (00..60)
%U - Week number of the current year,
starting with the first Sunday as the first
day of the first week (00..53)
%W - Week number of the current year,
starting with the first Monday as the first
day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character
See also: http://www.ruby-doc.org/core/Time.html#method-i-strftime
665 666 667 668 669 670 671 |
# File 'lib/liquid/standardfilters.rb', line 665 def date(input, format) return input if format.to_s.empty? return input unless (date = Utils.to_date(input)) date.strftime(format.to_s) end |
#default(input, default_value = '', options = {}) ⇒ Object
850 851 852 853 854 |
# File 'lib/liquid/standardfilters.rb', line 850 def default(input, default_value = '', = {}) = {} unless .is_a?(Hash) false_check = ['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
747 748 749 750 751 |
# File 'lib/liquid/standardfilters.rb', line 747 def divided_by(input, operand) apply_operation(input, operand, :/) rescue ::ZeroDivisionError => e raise Liquid::ZeroDivisionError, e. end |
#downcase(input) ⇒ Object
46 47 48 |
# File 'lib/liquid/standardfilters.rb', line 46 def downcase(input) input.to_s.downcase end |
#escape(input) ⇒ Object Also known as: h
79 80 81 |
# File 'lib/liquid/standardfilters.rb', line 79 def escape(input) CGI.escapeHTML(input.to_s) unless input.nil? end |
#escape_once(input) ⇒ Object
91 92 93 |
# File 'lib/liquid/standardfilters.rb', line 91 def escape_once(input) input.to_s.gsub(HTML_ESCAPE_ONCE_REGEXP, HTML_ESCAPE) end |
#first(array) ⇒ Object
680 681 682 |
# File 'lib/liquid/standardfilters.rb', line 680 def first(array) array.first if array.respond_to?(:first) end |
#floor(input) ⇒ Object
802 803 804 805 806 |
# File 'lib/liquid/standardfilters.rb', line 802 def floor(input) Utils.to_number(input).floor.to_i rescue ::FloatDomainError => e raise Liquid::FloatDomainError, e. end |
#join(input, glue = ' ') ⇒ Object
328 329 330 |
# File 'lib/liquid/standardfilters.rb', line 328 def join(input, glue = ' ') InputIterator.new(input, context).join(glue) end |
#last(array) ⇒ Object
691 692 693 |
# File 'lib/liquid/standardfilters.rb', line 691 def last(array) array.last if array.respond_to?(:last) end |
#lstrip(input) ⇒ Object
281 282 283 |
# File 'lib/liquid/standardfilters.rb', line 281 def lstrip(input) input.to_s.lstrip end |
#map(input, property) ⇒ Object
466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/liquid/standardfilters.rb', line 466 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
725 726 727 |
# File 'lib/liquid/standardfilters.rb', line 725 def minus(input, operand) apply_operation(input, operand, :-) end |
#modulo(input, operand) ⇒ Object
760 761 762 763 764 |
# File 'lib/liquid/standardfilters.rb', line 760 def modulo(input, operand) apply_operation(input, operand, :%) rescue ::ZeroDivisionError => e raise Liquid::ZeroDivisionError, e. end |
#newline_to_br(input) ⇒ Object
630 631 632 |
# File 'lib/liquid/standardfilters.rb', line 630 def newline_to_br(input) input.to_s.gsub(/\r?\n/, "<br />\n") end |
#plus(input, operand) ⇒ Object
714 715 716 |
# File 'lib/liquid/standardfilters.rb', line 714 def plus(input, operand) apply_operation(input, operand, :+) end |
#prepend(input, string) ⇒ Object
619 620 621 |
# File 'lib/liquid/standardfilters.rb', line 619 def prepend(input, string) string.to_s + input.to_s end |
#remove(input, string) ⇒ Object
557 558 559 |
# File 'lib/liquid/standardfilters.rb', line 557 def remove(input, string) replace(input, string, '') end |
#remove_first(input, string) ⇒ Object
568 569 570 |
# File 'lib/liquid/standardfilters.rb', line 568 def remove_first(input, string) replace_first(input, string, '') end |
#remove_last(input, string) ⇒ Object
579 580 581 |
# File 'lib/liquid/standardfilters.rb', line 579 def remove_last(input, string) replace_last(input, string, '') end |
#replace(input, string, replacement = '') ⇒ Object
514 515 516 |
# File 'lib/liquid/standardfilters.rb', line 514 def replace(input, string, replacement = '') input.to_s.gsub(string.to_s, replacement.to_s) end |
#replace_first(input, string, replacement = '') ⇒ Object
525 526 527 |
# File 'lib/liquid/standardfilters.rb', line 525 def replace_first(input, string, replacement = '') input.to_s.sub(string.to_s, replacement.to_s) end |
#replace_last(input, string, replacement) ⇒ Object
536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/liquid/standardfilters.rb', line 536 def replace_last(input, string, replacement) input = input.to_s string = string.to_s replacement = replacement.to_s start_index = input.rindex(string) return input unless start_index output = input.dup output[start_index, string.length] = replacement output end |
#reverse(input) ⇒ Object
454 455 456 457 |
# File 'lib/liquid/standardfilters.rb', line 454 def reverse(input) ary = InputIterator.new(input, context) ary.reverse end |
#round(input, n = 0) ⇒ Object
773 774 775 776 777 778 779 780 |
# File 'lib/liquid/standardfilters.rb', line 773 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. end |
#rstrip(input) ⇒ Object
292 293 294 |
# File 'lib/liquid/standardfilters.rb', line 292 def rstrip(input) input.to_s.rstrip end |
#size(input) ⇒ Object
35 36 37 |
# File 'lib/liquid/standardfilters.rb', line 35 def size(input) input.respond_to?(:size) ? input.size : 0 end |
#slice(input, offset, length = nil) ⇒ Object
185 186 187 188 189 190 191 192 193 194 |
# File 'lib/liquid/standardfilters.rb', line 185 def slice(input, offset, length = nil) offset = Utils.to_integer(offset) length = length ? Utils.to_integer(length) : 1 if input.is_a?(Array) input.slice(offset, length) || [] else input.to_s.slice(offset, length) || '' end end |
#sort(input, property = nil) ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/liquid/standardfilters.rb', line 339 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
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/liquid/standardfilters.rb', line 368 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
259 260 261 |
# File 'lib/liquid/standardfilters.rb', line 259 def split(input, pattern) input.to_s.split(pattern.to_s) end |
#strip(input) ⇒ Object
270 271 272 |
# File 'lib/liquid/standardfilters.rb', line 270 def strip(input) input.to_s.strip end |
#strip_html(input) ⇒ Object
303 304 305 306 307 308 |
# File 'lib/liquid/standardfilters.rb', line 303 def strip_html(input) empty = '' result = input.to_s.gsub(STRIP_HTML_BLOCKS, empty) result.gsub!(STRIP_HTML_TAGS, empty) result end |
#strip_newlines(input) ⇒ Object
317 318 319 |
# File 'lib/liquid/standardfilters.rb', line 317 def strip_newlines(input) input.to_s.gsub(/\r?\n/, '') end |
#times(input, operand) ⇒ Object
736 737 738 |
# File 'lib/liquid/standardfilters.rb', line 736 def times(input, operand) apply_operation(input, operand, :*) end |
#truncate(input, length = 50, truncate_string = "...") ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/liquid/standardfilters.rb', line 206 def truncate(input, length = 50, truncate_string = "...") return if input.nil? 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 input_str.length > length ? input_str[0...l].concat(truncate_string_str) : input_str end |
#truncatewords(input, words = 15, truncate_string = "...") ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/liquid/standardfilters.rb', line 233 def truncatewords(input, words = 15, truncate_string = "...") return if input.nil? input = input.to_s words = Utils.to_integer(words) words = 1 if words <= 0 wordlist = begin input.split(" ", words + 1) rescue RangeError raise if words + 1 < MAX_INT # e.g. integer #{words} too big to convert to `int' raise Liquid::ArgumentError, "integer #{words} too big for truncatewords" end return input if wordlist.length <= words wordlist.pop wordlist.join(" ").concat(truncate_string.to_s) end |
#uniq(input, property = nil) ⇒ Object
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/liquid/standardfilters.rb', line 428 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
57 58 59 |
# File 'lib/liquid/standardfilters.rb', line 57 def upcase(input) input.to_s.upcase end |
#url_decode(input) ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/liquid/standardfilters.rb', line 118 def url_decode(input) return if input.nil? result = CGI.unescape(input.to_s) raise Liquid::ArgumentError, "invalid byte sequence in #{result.encoding}" unless result.valid_encoding? result end |
#url_encode(input) ⇒ Object
106 107 108 |
# File 'lib/liquid/standardfilters.rb', line 106 def url_encode(input) CGI.escape(input.to_s) unless input.nil? end |
#where(input, property, target_value = nil) ⇒ Object
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/liquid/standardfilters.rb', line 395 def where(input, property, target_value = nil) ary = InputIterator.new(input, context) if ary.empty? [] elsif target_value.nil? ary.select do |item| item[property] rescue TypeError raise_property_error(property) rescue NoMethodError return nil unless item.respond_to?(:[]) raise end else ary.select do |item| item[property] == target_value rescue TypeError raise_property_error(property) rescue NoMethodError return nil unless item.respond_to?(:[]) raise end end end |