Module: Liquid::StandardFilters
- Defined in:
- lib/liquid/standardfilters.rb
Defined Under Namespace
Classes: InputIterator
Constant Summary collapse
- HTML_ESCAPE =
{ '&'.freeze => '&'.freeze, '>'.freeze => '>'.freeze, '<'.freeze => '<'.freeze, '"'.freeze => '"'.freeze, "'".freeze => '''.freeze }
- HTML_ESCAPE_ONCE_REGEXP =
/["><']|&(?!([a-zA-Z]+|(#\d+));)/
Instance Method Summary collapse
-
#abs(input) ⇒ Object
absolute value.
-
#append(input, string) ⇒ Object
add one string to another.
- #at_least(input, n) ⇒ Object
- #at_most(input, n) ⇒ Object
-
#capitalize(input) ⇒ Object
capitalize words in the input centence.
- #ceil(input) ⇒ Object
-
#compact(input, property = nil) ⇒ Object
Remove nils within an array provide optional property with which to check for nil.
- #concat(input, array) ⇒ Object
-
#date(input, format) ⇒ Object
Reformat a date using Ruby’s core Time#strftime( string ) -> string.
- #default(input, default_value = ''.freeze) ⇒ Object
-
#divided_by(input, operand) ⇒ Object
division.
-
#downcase(input) ⇒ Object
convert an input string to DOWNCASE.
- #escape(input) ⇒ Object (also: #h)
- #escape_once(input) ⇒ Object
-
#first(array) ⇒ Object
Get the first element of the passed in array.
- #floor(input) ⇒ Object
-
#join(input, glue = ' '.freeze) ⇒ Object
Join elements of the array with certain character between them.
-
#last(array) ⇒ Object
Get the last element of the passed in array.
- #lstrip(input) ⇒ Object
-
#map(input, property) ⇒ Object
map/collect on a given property.
-
#minus(input, operand) ⇒ Object
subtraction.
- #modulo(input, operand) ⇒ Object
-
#newline_to_br(input) ⇒ Object
Add <br /> tags in front of all newlines in input string.
-
#plus(input, operand) ⇒ Object
addition.
-
#prepend(input, string) ⇒ Object
prepend a string to another.
-
#remove(input, string) ⇒ Object
remove a substring.
-
#remove_first(input, string) ⇒ Object
remove the first occurrences of a substring.
-
#replace(input, string, replacement = ''.freeze) ⇒ Object
Replace occurrences of a string with another.
-
#replace_first(input, string, replacement = ''.freeze) ⇒ Object
Replace the first occurrences of a string with another.
-
#reverse(input) ⇒ Object
Reverse the elements of an array.
- #round(input, n = 0) ⇒ Object
- #rstrip(input) ⇒ Object
-
#size(input) ⇒ Object
Return the size of an array or of an string.
- #slice(input, offset, length = nil) ⇒ Object
-
#sort(input, property = nil) ⇒ Object
Sort elements of the array provide optional property with which to sort an array of hashes or drops.
-
#sort_natural(input, property = nil) ⇒ Object
Sort elements of an array ignoring case if strings provide optional property with which to sort an array of hashes or drops.
-
#split(input, pattern) ⇒ Object
Split input string into an array of substrings separated by given pattern.
- #strip(input) ⇒ Object
- #strip_html(input) ⇒ Object
-
#strip_newlines(input) ⇒ Object
Remove all newlines from the string.
-
#times(input, operand) ⇒ Object
multiplication.
-
#truncate(input, length = 50, truncate_string = "...".freeze) ⇒ Object
Truncate a string down to x characters.
- #truncatewords(input, words = 15, truncate_string = "...".freeze) ⇒ Object
-
#uniq(input, property = nil) ⇒ Object
Remove duplicate elements from an array provide optional property with which to determine uniqueness.
-
#upcase(input) ⇒ Object
convert an input string to UPCASE.
- #url_decode(input) ⇒ Object
- #url_encode(input) ⇒ Object
-
#where(input, property, target_value = nil) ⇒ Object
Filter the elements of an array to those with a certain property value.
Instance Method Details
#abs(input) ⇒ Object
absolute value
317 318 319 320 |
# File 'lib/liquid/standardfilters.rb', line 317 def abs(input) result = Utils.to_number(input).abs result.is_a?(BigDecimal) ? result.to_f : result end |
#append(input, string) ⇒ Object
add one string to another
238 239 240 |
# File 'lib/liquid/standardfilters.rb', line 238 def append(input, string) input.to_s + string.to_s end |
#at_least(input, n) ⇒ Object
371 372 373 374 375 376 377 |
# File 'lib/liquid/standardfilters.rb', line 371 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
379 380 381 382 383 384 385 |
# File 'lib/liquid/standardfilters.rb', line 379 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 |
#capitalize(input) ⇒ Object
capitalize words in the input centence
31 32 33 |
# File 'lib/liquid/standardfilters.rb', line 31 def capitalize(input) input.to_s.capitalize end |
#ceil(input) ⇒ Object
359 360 361 362 363 |
# File 'lib/liquid/standardfilters.rb', line 359 def ceil(input) Utils.to_number(input).ceil.to_i rescue ::FloatDomainError => e raise Liquid::FloatDomainError, e. end |
#compact(input, property = nil) ⇒ Object
Remove nils within an array provide optional property with which to check for nil
205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/liquid/standardfilters.rb', line 205 def compact(input, property = nil) ary = InputIterator.new(input) if property.nil? ary.compact elsif ary.empty? # The next two cases assume a non-empty array. [] elsif ary.first.respond_to?(:[]) ary.reject{ |a| a[property].nil? } end end |
#concat(input, array) ⇒ Object
242 243 244 245 246 247 |
# File 'lib/liquid/standardfilters.rb', line 242 def concat(input, array) unless array.respond_to?(:to_ary) raise ArgumentError.new("concat filter requires an array argument") end InputIterator.new(input).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
290 291 292 293 294 295 296 |
# File 'lib/liquid/standardfilters.rb', line 290 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 = ''.freeze) ⇒ Object
387 388 389 390 391 392 393 |
# File 'lib/liquid/standardfilters.rb', line 387 def default(input, default_value = ''.freeze) if !input || input.respond_to?(:empty?) && input.empty? default_value else input end end |
#divided_by(input, operand) ⇒ Object
division
338 339 340 341 342 |
# File 'lib/liquid/standardfilters.rb', line 338 def divided_by(input, operand) apply_operation(input, operand, :/) rescue ::ZeroDivisionError => e raise Liquid::ZeroDivisionError, e. end |
#downcase(input) ⇒ Object
convert an input string to DOWNCASE
21 22 23 |
# File 'lib/liquid/standardfilters.rb', line 21 def downcase(input) input.to_s.downcase end |
#escape(input) ⇒ Object Also known as: h
35 36 37 |
# File 'lib/liquid/standardfilters.rb', line 35 def escape(input) CGI.escapeHTML(input.to_s).untaint unless input.nil? end |
#escape_once(input) ⇒ Object
40 41 42 |
# File 'lib/liquid/standardfilters.rb', line 40 def escape_once(input) input.to_s.gsub(HTML_ESCAPE_ONCE_REGEXP, HTML_ESCAPE) end |
#first(array) ⇒ Object
Get the first element of the passed in array
Example:
{{ product.images | first | to_img }}
303 304 305 |
# File 'lib/liquid/standardfilters.rb', line 303 def first(array) array.first if array.respond_to?(:first) end |
#floor(input) ⇒ Object
365 366 367 368 369 |
# File 'lib/liquid/standardfilters.rb', line 365 def floor(input) Utils.to_number(input).floor.to_i rescue ::FloatDomainError => e raise Liquid::FloatDomainError, e. end |
#join(input, glue = ' '.freeze) ⇒ Object
Join elements of the array with certain character between them
115 116 117 |
# File 'lib/liquid/standardfilters.rb', line 115 def join(input, glue = ' '.freeze) InputIterator.new(input).join(glue) end |
#last(array) ⇒ Object
Get the last element of the passed in array
Example:
{{ product.images | last | to_img }}
312 313 314 |
# File 'lib/liquid/standardfilters.rb', line 312 def last(array) array.last if array.respond_to?(:last) end |
#lstrip(input) ⇒ Object
96 97 98 |
# File 'lib/liquid/standardfilters.rb', line 96 def lstrip(input) input.to_s.lstrip end |
#map(input, property) ⇒ Object
map/collect on a given property
190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/liquid/standardfilters.rb', line 190 def map(input, property) InputIterator.new(input).map do |e| e = e.call if e.is_a?(Proc) if property == "to_liquid".freeze e elsif e.respond_to?(:[]) r = e[property] r.is_a?(Proc) ? r.call : r end end end |
#minus(input, operand) ⇒ Object
subtraction
328 329 330 |
# File 'lib/liquid/standardfilters.rb', line 328 def minus(input, operand) apply_operation(input, operand, :-) end |
#modulo(input, operand) ⇒ Object
344 345 346 347 348 |
# File 'lib/liquid/standardfilters.rb', line 344 def modulo(input, operand) apply_operation(input, operand, :%) rescue ::ZeroDivisionError => e raise Liquid::ZeroDivisionError, e. end |
#newline_to_br(input) ⇒ Object
Add <br /> tags in front of all newlines in input string
255 256 257 |
# File 'lib/liquid/standardfilters.rb', line 255 def newline_to_br(input) input.to_s.gsub(/\n/, "<br />\n".freeze) end |
#plus(input, operand) ⇒ Object
addition
323 324 325 |
# File 'lib/liquid/standardfilters.rb', line 323 def plus(input, operand) apply_operation(input, operand, :+) end |
#prepend(input, string) ⇒ Object
prepend a string to another
250 251 252 |
# File 'lib/liquid/standardfilters.rb', line 250 def prepend(input, string) string.to_s + input.to_s end |
#remove(input, string) ⇒ Object
remove a substring
228 229 230 |
# File 'lib/liquid/standardfilters.rb', line 228 def remove(input, string) input.to_s.gsub(string.to_s, ''.freeze) end |
#remove_first(input, string) ⇒ Object
remove the first occurrences of a substring
233 234 235 |
# File 'lib/liquid/standardfilters.rb', line 233 def remove_first(input, string) input.to_s.sub(string.to_s, ''.freeze) end |
#replace(input, string, replacement = ''.freeze) ⇒ Object
Replace occurrences of a string with another
218 219 220 |
# File 'lib/liquid/standardfilters.rb', line 218 def replace(input, string, replacement = ''.freeze) input.to_s.gsub(string.to_s, replacement.to_s) end |
#replace_first(input, string, replacement = ''.freeze) ⇒ Object
Replace the first occurrences of a string with another
223 224 225 |
# File 'lib/liquid/standardfilters.rb', line 223 def replace_first(input, string, replacement = ''.freeze) input.to_s.sub(string.to_s, replacement.to_s) end |
#reverse(input) ⇒ Object
Reverse the elements of an array
184 185 186 187 |
# File 'lib/liquid/standardfilters.rb', line 184 def reverse(input) ary = InputIterator.new(input) ary.reverse end |
#round(input, n = 0) ⇒ Object
350 351 352 353 354 355 356 357 |
# File 'lib/liquid/standardfilters.rb', line 350 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
100 101 102 |
# File 'lib/liquid/standardfilters.rb', line 100 def rstrip(input) input.to_s.rstrip end |
#size(input) ⇒ Object
Return the size of an array or of an string
16 17 18 |
# File 'lib/liquid/standardfilters.rb', line 16 def size(input) input.respond_to?(:size) ? input.size : 0 end |
#slice(input, offset, length = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/liquid/standardfilters.rb', line 52 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
Sort elements of the array provide optional property with which to sort an array of hashes or drops
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/liquid/standardfilters.rb', line 121 def sort(input, property = nil) ary = InputIterator.new(input) 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?(:[]) } ary.sort do |a, b| nil_safe_compare(a[property], b[property]) end end end |
#sort_natural(input, property = nil) ⇒ Object
Sort elements of an array ignoring case if strings provide optional property with which to sort an array of hashes or drops
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/liquid/standardfilters.rb', line 139 def sort_natural(input, property = nil) ary = InputIterator.new(input) 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?(:[]) } ary.sort do |a, b| nil_safe_casecmp(a[property], b[property]) end end end |
#split(input, pattern) ⇒ Object
Split input string into an array of substrings separated by given pattern.
Example:
<div class="summary">{{ post | split '//' | first }}</div>
88 89 90 |
# File 'lib/liquid/standardfilters.rb', line 88 def split(input, pattern) input.to_s.split(pattern.to_s) end |
#strip(input) ⇒ Object
92 93 94 |
# File 'lib/liquid/standardfilters.rb', line 92 def strip(input) input.to_s.strip end |
#strip_html(input) ⇒ Object
104 105 106 107 |
# File 'lib/liquid/standardfilters.rb', line 104 def strip_html(input) empty = ''.freeze input.to_s.gsub(/<script.*?<\/script>/m, empty).gsub(/<!--.*?-->/m, empty).gsub(/<style.*?<\/style>/m, empty).gsub(/<.*?>/m, empty) end |
#strip_newlines(input) ⇒ Object
Remove all newlines from the string
110 111 112 |
# File 'lib/liquid/standardfilters.rb', line 110 def strip_newlines(input) input.to_s.gsub(/\r?\n/, ''.freeze) end |
#times(input, operand) ⇒ Object
multiplication
333 334 335 |
# File 'lib/liquid/standardfilters.rb', line 333 def times(input, operand) apply_operation(input, operand, :*) end |
#truncate(input, length = 50, truncate_string = "...".freeze) ⇒ Object
Truncate a string down to x characters
64 65 66 67 68 69 70 71 72 |
# File 'lib/liquid/standardfilters.rb', line 64 def truncate(input, length = 50, truncate_string = "...".freeze) 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] + truncate_string_str : input_str end |
#truncatewords(input, words = 15, truncate_string = "...".freeze) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/liquid/standardfilters.rb', line 74 def truncatewords(input, words = 15, truncate_string = "...".freeze) return if input.nil? wordlist = input.to_s.split words = Utils.to_integer(words) l = words - 1 l = 0 if l < 0 wordlist.length > l ? wordlist[0..l].join(" ".freeze) + truncate_string.to_s : input end |
#uniq(input, property = nil) ⇒ Object
Remove duplicate elements from an array provide optional property with which to determine uniqueness
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/liquid/standardfilters.rb', line 171 def uniq(input, property = nil) ary = InputIterator.new(input) if property.nil? ary.uniq elsif ary.empty? # The next two cases assume a non-empty array. [] elsif ary.first.respond_to?(:[]) ary.uniq{ |a| a[property] } end end |
#upcase(input) ⇒ Object
convert an input string to UPCASE
26 27 28 |
# File 'lib/liquid/standardfilters.rb', line 26 def upcase(input) input.to_s.upcase end |
#url_decode(input) ⇒ Object
48 49 50 |
# File 'lib/liquid/standardfilters.rb', line 48 def url_decode(input) CGI.unescape(input.to_s) unless input.nil? end |
#url_encode(input) ⇒ Object
44 45 46 |
# File 'lib/liquid/standardfilters.rb', line 44 def url_encode(input) CGI.escape(input.to_s) unless input.nil? end |
#where(input, property, target_value = nil) ⇒ Object
Filter the elements of an array to those with a certain property value. By default the target is any truthy value.
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/liquid/standardfilters.rb', line 157 def where(input, property, target_value = nil) ary = InputIterator.new(input) if ary.empty? [] elsif ary.first.respond_to?(:[]) && target_value.nil? ary.where_present(property) elsif ary.first.respond_to?(:[]) ary.where(property, target_value) end end |