Module: Liquid::StandardFilters
- Defined in:
- lib/liquid/standardfilters.rb
Instance Method Summary collapse
-
#append(input, string) ⇒ Object
add one string to another.
-
#capitalize(input) ⇒ Object
capitalize words in the input centence.
-
#date(input, format) ⇒ Object
Reformat a date.
-
#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.
-
#join(input, array_glue = ' ', hash_glue = nil) ⇒ Object
Join elements of the array with certain character between them.
-
#last(array) ⇒ Object
Get the last element of the passed in array.
-
#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 = '') ⇒ Object
Replace occurrences of a string with another.
-
#replace_first(input, string, replacement = '') ⇒ Object
Replace the first occurrences of a string with another.
-
#reverse(input) ⇒ Object
Reverse the elements of an array.
-
#size(input) ⇒ Object
Return the size of an array or of an string.
-
#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_html(input) ⇒ Object
-
#strip_newlines(input) ⇒ Object
Remove all newlines from the string.
-
#times(input, operand) ⇒ Object
multiplication.
-
#truncate(input, length = 50, truncate_string = "...") ⇒ Object
Truncate a string down to x characters.
- #truncatewords(input, words = 15, truncate_string = "...") ⇒ Object
-
#upcase(input) ⇒ Object
convert an input string to UPCASE.
Instance Method Details
#append(input, string) ⇒ Object
add one string to another
138 139 140 |
# File 'lib/liquid/standardfilters.rb', line 138 def append(input, string) input.to_s + string.to_s end |
#capitalize(input) ⇒ Object
capitalize words in the input centence
24 25 26 |
# File 'lib/liquid/standardfilters.rb', line 24 def capitalize(input) input.to_s.capitalize end |
#date(input, format) ⇒ Object
Reformat a date
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 - 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
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/liquid/standardfilters.rb', line 180 def date(input, format) if format.to_s.empty? return input.to_s end if ((input.is_a?(String) && !/^\d+$/.match(input.to_s).nil?) || input.is_a?(Integer)) && input.to_i > 0 input = Time.at(input.to_i) end date = if input.is_a?(String) case input.downcase when 'now', 'today' Time.now else Time.parse(input) end else input end if date.respond_to?(:strftime) date.strftime(format.to_s) else input end rescue input end |
#divided_by(input, operand) ⇒ Object
division
244 245 246 |
# File 'lib/liquid/standardfilters.rb', line 244 def divided_by(input, operand) apply_operation(input, operand, :/) end |
#downcase(input) ⇒ Object
convert an input string to DOWNCASE
14 15 16 |
# File 'lib/liquid/standardfilters.rb', line 14 def downcase(input) input.to_s.downcase end |
#escape(input) ⇒ Object Also known as: h
28 29 30 |
# File 'lib/liquid/standardfilters.rb', line 28 def escape(input) CGI.escapeHTML(input) rescue input end |
#escape_once(input) ⇒ Object
32 33 34 35 36 |
# File 'lib/liquid/standardfilters.rb', line 32 def escape_once(input) ActionView::Helpers::TagHelper.escape_once(input) rescue NameError input end |
#first(array) ⇒ Object
Get the first element of the passed in array
Example:
{{ product.images | first | to_img }}
215 216 217 |
# File 'lib/liquid/standardfilters.rb', line 215 def first(array) array.first if array.respond_to?(:first) end |
#join(input, array_glue = ' ', hash_glue = nil) ⇒ Object
Join elements of the array with certain character between them
76 77 78 79 80 81 82 83 |
# File 'lib/liquid/standardfilters.rb', line 76 def join(input, array_glue = ' ', hash_glue = nil) hash_glue ||= array_glue # translate from hash to array if needed input = input.map{|k,v| "#{k}#{hash_glue}#{v}" } if input.is_a?(Hash) [input].flatten.join(array_glue) end |
#last(array) ⇒ Object
Get the last element of the passed in array
Example:
{{ product.images | last | to_img }}
224 225 226 |
# File 'lib/liquid/standardfilters.rb', line 224 def last(array) array.last if array.respond_to?(:last) end |
#map(input, property) ⇒ Object
map/collect on a given property
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/liquid/standardfilters.rb', line 105 def map(input, property) flatten_if_necessary(input).map do |e| e = e.call if e.is_a?(Proc) if property == "to_liquid" e elsif e.respond_to?(:[]) e[property] end end end |
#minus(input, operand) ⇒ Object
subtraction
234 235 236 |
# File 'lib/liquid/standardfilters.rb', line 234 def minus(input, operand) apply_operation(input, operand, :-) end |
#modulo(input, operand) ⇒ Object
248 249 250 |
# File 'lib/liquid/standardfilters.rb', line 248 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
148 149 150 |
# File 'lib/liquid/standardfilters.rb', line 148 def newline_to_br(input) input.to_s.gsub(/\n/, "<br />\n") end |
#plus(input, operand) ⇒ Object
addition
229 230 231 |
# File 'lib/liquid/standardfilters.rb', line 229 def plus(input, operand) apply_operation(input, operand, :+) end |
#prepend(input, string) ⇒ Object
prepend a string to another
143 144 145 |
# File 'lib/liquid/standardfilters.rb', line 143 def prepend(input, string) string.to_s + input.to_s end |
#remove(input, string) ⇒ Object
remove a substring
128 129 130 |
# File 'lib/liquid/standardfilters.rb', line 128 def remove(input, string) input.to_s.gsub(string, '') end |
#remove_first(input, string) ⇒ Object
remove the first occurrences of a substring
133 134 135 |
# File 'lib/liquid/standardfilters.rb', line 133 def remove_first(input, string) input.to_s.sub(string, '') end |
#replace(input, string, replacement = '') ⇒ Object
Replace occurrences of a string with another
118 119 120 |
# File 'lib/liquid/standardfilters.rb', line 118 def replace(input, string, replacement = '') input.to_s.gsub(string, replacement.to_s) end |
#replace_first(input, string, replacement = '') ⇒ Object
Replace the first occurrences of a string with another
123 124 125 |
# File 'lib/liquid/standardfilters.rb', line 123 def replace_first(input, string, replacement = '') input.to_s.sub(string, replacement.to_s) end |
#reverse(input) ⇒ Object
Reverse the elements of an array
99 100 101 102 |
# File 'lib/liquid/standardfilters.rb', line 99 def reverse(input) ary = [input].flatten ary.reverse end |
#size(input) ⇒ Object
Return the size of an array or of an string
9 10 11 |
# File 'lib/liquid/standardfilters.rb', line 9 def size(input) input.respond_to?(:size) ? input.size : 0 end |
#sort(input, property = nil) ⇒ Object
Sort elements of the array provide optional property with which to sort an array of hashes or drops
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/liquid/standardfilters.rb', line 87 def sort(input, property = nil) ary = flatten_if_necessary(input) if property.nil? ary.sort elsif ary.first.respond_to?('[]') and !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>
62 63 64 |
# File 'lib/liquid/standardfilters.rb', line 62 def split(input, pattern) input.split(pattern) end |
#strip_html(input) ⇒ Object
66 67 68 |
# File 'lib/liquid/standardfilters.rb', line 66 def strip_html(input) input.to_s.gsub(/<script.*?<\/script>/m, '').gsub(/<!--.*?-->/m, '').gsub(/<style.*?<\/style>/m, '').gsub(/<.*?>/m, '') end |
#strip_newlines(input) ⇒ Object
Remove all newlines from the string
71 72 73 |
# File 'lib/liquid/standardfilters.rb', line 71 def strip_newlines(input) input.to_s.gsub(/\r?\n/, '') end |
#times(input, operand) ⇒ Object
multiplication
239 240 241 |
# File 'lib/liquid/standardfilters.rb', line 239 def times(input, operand) apply_operation(input, operand, :*) end |
#truncate(input, length = 50, truncate_string = "...") ⇒ Object
Truncate a string down to x characters
41 42 43 44 45 46 47 |
# File 'lib/liquid/standardfilters.rb', line 41 def truncate(input, length = 50, truncate_string = "...") if input.nil? then return end l = length.to_i - truncate_string.length l = 0 if l < 0 truncated = RUBY_VERSION[0,3] == "1.8" ? input.scan(/./mu)[0...l].to_s : input[0...l] input.length > length.to_i ? truncated + truncate_string : input end |
#truncatewords(input, words = 15, truncate_string = "...") ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/liquid/standardfilters.rb', line 49 def truncatewords(input, words = 15, truncate_string = "...") 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(" ") + truncate_string : input end |
#upcase(input) ⇒ Object
convert an input string to UPCASE
19 20 21 |
# File 'lib/liquid/standardfilters.rb', line 19 def upcase(input) input.to_s.upcase end |