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
-
#append(input, string) ⇒ Object
add one string to another.
-
#capitalize(input) ⇒ Object
capitalize words in the input centence.
- #ceil(input) ⇒ 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.
-
#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_encode(input) ⇒ Object
Instance Method Details
#append(input, string) ⇒ Object
add one string to another
176 177 178 |
# File 'lib/liquid/standardfilters.rb', line 176 def append(input, string) input.to_s + string.to_s end |
#capitalize(input) ⇒ Object
capitalize words in the input centence
32 33 34 |
# File 'lib/liquid/standardfilters.rb', line 32 def capitalize(input) input.to_s.capitalize end |
#ceil(input) ⇒ Object
278 279 280 |
# File 'lib/liquid/standardfilters.rb', line 278 def ceil(input) to_number(input).ceil.to_i 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
221 222 223 224 225 226 227 |
# File 'lib/liquid/standardfilters.rb', line 221 def date(input, format) return input if format.to_s.empty? return input unless date = to_date(input) date.strftime(format.to_s) end |
#default(input, default_value = "".freeze) ⇒ Object
286 287 288 289 |
# File 'lib/liquid/standardfilters.rb', line 286 def default(input, default_value = "".freeze) is_blank = input.respond_to?(:empty?) ? input.empty? : !input is_blank ? default_value : input end |
#divided_by(input, operand) ⇒ Object
division
263 264 265 |
# File 'lib/liquid/standardfilters.rb', line 263 def divided_by(input, operand) apply_operation(input, operand, :/) end |
#downcase(input) ⇒ Object
convert an input string to DOWNCASE
22 23 24 |
# File 'lib/liquid/standardfilters.rb', line 22 def downcase(input) input.to_s.downcase end |
#escape(input) ⇒ Object Also known as: h
36 37 38 |
# File 'lib/liquid/standardfilters.rb', line 36 def escape(input) CGI.escapeHTML(input).untaint rescue input end |
#escape_once(input) ⇒ Object
41 42 43 |
# File 'lib/liquid/standardfilters.rb', line 41 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 }}
234 235 236 |
# File 'lib/liquid/standardfilters.rb', line 234 def first(array) array.first if array.respond_to?(:first) end |
#floor(input) ⇒ Object
282 283 284 |
# File 'lib/liquid/standardfilters.rb', line 282 def floor(input) to_number(input).floor.to_i end |
#join(input, glue = ' '.freeze) ⇒ Object
Join elements of the array with certain character between them
108 109 110 |
# File 'lib/liquid/standardfilters.rb', line 108 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 }}
243 244 245 |
# File 'lib/liquid/standardfilters.rb', line 243 def last(array) array.last if array.respond_to?(:last) end |
#lstrip(input) ⇒ Object
89 90 91 |
# File 'lib/liquid/standardfilters.rb', line 89 def lstrip(input) input.to_s.lstrip end |
#map(input, property) ⇒ Object
map/collect on a given property
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/liquid/standardfilters.rb', line 143 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?(:[]) e[property] end end end |
#minus(input, operand) ⇒ Object
subtraction
253 254 255 |
# File 'lib/liquid/standardfilters.rb', line 253 def minus(input, operand) apply_operation(input, operand, :-) end |
#modulo(input, operand) ⇒ Object
267 268 269 |
# File 'lib/liquid/standardfilters.rb', line 267 def modulo(input, operand) apply_operation(input, operand, :%) end |
#newline_to_br(input) ⇒ Object
Add <br /> tags in front of all newlines in input string
186 187 188 |
# File 'lib/liquid/standardfilters.rb', line 186 def newline_to_br(input) input.to_s.gsub(/\n/, "<br />\n".freeze) end |
#plus(input, operand) ⇒ Object
addition
248 249 250 |
# File 'lib/liquid/standardfilters.rb', line 248 def plus(input, operand) apply_operation(input, operand, :+) end |
#prepend(input, string) ⇒ Object
prepend a string to another
181 182 183 |
# File 'lib/liquid/standardfilters.rb', line 181 def prepend(input, string) string.to_s + input.to_s end |
#remove(input, string) ⇒ Object
remove a substring
166 167 168 |
# File 'lib/liquid/standardfilters.rb', line 166 def remove(input, string) input.to_s.gsub(string, ''.freeze) end |
#remove_first(input, string) ⇒ Object
remove the first occurrences of a substring
171 172 173 |
# File 'lib/liquid/standardfilters.rb', line 171 def remove_first(input, string) input.to_s.sub(string, ''.freeze) end |
#replace(input, string, replacement = ''.freeze) ⇒ Object
Replace occurrences of a string with another
156 157 158 |
# File 'lib/liquid/standardfilters.rb', line 156 def replace(input, string, replacement = ''.freeze) input.to_s.gsub(string, replacement.to_s) end |
#replace_first(input, string, replacement = ''.freeze) ⇒ Object
Replace the first occurrences of a string with another
161 162 163 |
# File 'lib/liquid/standardfilters.rb', line 161 def replace_first(input, string, replacement = ''.freeze) input.to_s.sub(string, replacement.to_s) end |
#reverse(input) ⇒ Object
Reverse the elements of an array
137 138 139 140 |
# File 'lib/liquid/standardfilters.rb', line 137 def reverse(input) ary = InputIterator.new(input) ary.reverse end |
#round(input, n = 0) ⇒ Object
271 272 273 274 275 276 |
# File 'lib/liquid/standardfilters.rb', line 271 def round(input, n = 0) result = to_number(input).round(to_number(n)) result = result.to_f if result.is_a?(BigDecimal) result = result.to_i if n == 0 result end |
#rstrip(input) ⇒ Object
93 94 95 |
# File 'lib/liquid/standardfilters.rb', line 93 def rstrip(input) input.to_s.rstrip end |
#size(input) ⇒ Object
Return the size of an array or of an string
17 18 19 |
# File 'lib/liquid/standardfilters.rb', line 17 def size(input) input.respond_to?(:size) ? input.size : 0 end |
#slice(input, offset, length = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/liquid/standardfilters.rb', line 49 def slice(input, offset, length=nil) offset = Integer(offset) length = length ? 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
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/liquid/standardfilters.rb', line 114 def sort(input, property = nil) ary = InputIterator.new(input) if property.nil? ary.sort elsif ary.first.respond_to?(:[]) && !ary.first[property].nil? ary.sort {|a,b| a[property] <=> b[property] } elsif ary.first.respond_to?(property) ary.sort {|a,b| a.send(property) <=> b.send(property) } 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>
81 82 83 |
# File 'lib/liquid/standardfilters.rb', line 81 def split(input, pattern) input.to_s.split(pattern) end |
#strip(input) ⇒ Object
85 86 87 |
# File 'lib/liquid/standardfilters.rb', line 85 def strip(input) input.to_s.strip end |
#strip_html(input) ⇒ Object
97 98 99 100 |
# File 'lib/liquid/standardfilters.rb', line 97 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
103 104 105 |
# File 'lib/liquid/standardfilters.rb', line 103 def strip_newlines(input) input.to_s.gsub(/\r?\n/, ''.freeze) end |
#times(input, operand) ⇒ Object
multiplication
258 259 260 |
# File 'lib/liquid/standardfilters.rb', line 258 def times(input, operand) apply_operation(input, operand, :*) end |
#truncate(input, length = 50, truncate_string = "...".freeze) ⇒ Object
Truncate a string down to x characters
61 62 63 64 65 66 |
# File 'lib/liquid/standardfilters.rb', line 61 def truncate(input, length = 50, truncate_string = "...".freeze) if input.nil? then return end l = length.to_i - truncate_string.length l = 0 if l < 0 input.length > length.to_i ? input[0...l] + truncate_string : input end |
#truncatewords(input, words = 15, truncate_string = "...".freeze) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/liquid/standardfilters.rb', line 68 def truncatewords(input, words = 15, truncate_string = "...".freeze) if input.nil? then return end wordlist = input.to_s.split l = words.to_i - 1 l = 0 if l < 0 wordlist.length > l ? wordlist[0..l].join(" ".freeze) + truncate_string : input end |
#uniq(input, property = nil) ⇒ Object
Remove duplicate elements from an array provide optional property with which to determine uniqueness
127 128 129 130 131 132 133 134 |
# File 'lib/liquid/standardfilters.rb', line 127 def uniq(input, property = nil) ary = InputIterator.new(input) if property.nil? input.uniq elsif input.first.respond_to?(:[]) input.uniq{ |a| a[property] } end end |
#upcase(input) ⇒ Object
convert an input string to UPCASE
27 28 29 |
# File 'lib/liquid/standardfilters.rb', line 27 def upcase(input) input.to_s.upcase end |
#url_encode(input) ⇒ Object
45 46 47 |
# File 'lib/liquid/standardfilters.rb', line 45 def url_encode(input) CGI.escape(input) rescue input end |