/lib/liquid/standardfilters.rb

https://github.com/peertransfer/liquid · Ruby · 244 lines · 141 code · 42 blank · 61 comment · 15 complexity · 809dba72ee248121e0fa0b5220ecfe6d MD5 · raw file

  1. require 'cgi'
  2. module Liquid
  3. module StandardFilters
  4. # Return the size of an array or of an string
  5. def size(input)
  6. input.respond_to?(:size) ? input.size : 0
  7. end
  8. # convert a input string to DOWNCASE
  9. def downcase(input)
  10. input.to_s.downcase
  11. end
  12. # convert a input string to UPCASE
  13. def upcase(input)
  14. input.to_s.upcase
  15. end
  16. # capitalize words in the input centence
  17. def capitalize(input)
  18. input.to_s.capitalize
  19. end
  20. def escape(input)
  21. CGI.escapeHTML(input) rescue input
  22. end
  23. def escape_once(input)
  24. ActionView::Helpers::TagHelper.escape_once(input)
  25. rescue NameError
  26. input
  27. end
  28. alias_method :h, :escape
  29. # Truncate a string down to x characters
  30. def truncate(input, length = 50, truncate_string = "...")
  31. if input.nil? then return end
  32. l = length.to_i - truncate_string.length
  33. l = 0 if l < 0
  34. input.length > length.to_i ? input[0...l] + truncate_string : input
  35. end
  36. def truncatewords(input, words = 15, truncate_string = "...")
  37. if input.nil? then return end
  38. wordlist = input.to_s.split
  39. l = words.to_i - 1
  40. l = 0 if l < 0
  41. wordlist.length > l ? wordlist[0..l].join(" ") + truncate_string : input
  42. end
  43. # mask the string with '*' and leave the number of characters indicated at the end, two by default
  44. def mask(input, chars = 2, mask_char = '*')
  45. word = input.to_s
  46. return word if word.empty?
  47. mask_char * (word.size - chars.to_i) + word[(word.size - chars.to_i)..word.size]
  48. end
  49. # Split input string into an array of substrings separated by given pattern.
  50. def split(input, pattern)
  51. input.split(pattern)
  52. end
  53. def strip_html(input)
  54. input.to_s.gsub(/<script.*?<\/script>/, '').gsub(/<.*?>/, '')
  55. end
  56. # Remove all newlines from the string
  57. def strip_newlines(input)
  58. input.to_s.gsub(/\n/, '')
  59. end
  60. # Join elements of the array with certain character between them
  61. def join(input, glue = ' ')
  62. [input].flatten.join(glue)
  63. end
  64. # Sort elements of the array
  65. # provide optional property with which to sort an array of hashes or drops
  66. def sort(input, property = nil)
  67. ary = [input].flatten
  68. if property.nil?
  69. ary.sort
  70. elsif ary.first.respond_to?('[]') and !ary.first[property].nil?
  71. ary.sort {|a,b| a[property] <=> b[property] }
  72. elsif ary.first.respond_to?(property)
  73. ary.sort {|a,b| a.send(property) <=> b.send(property) }
  74. end
  75. end
  76. # map/collect on a given property
  77. def map(input, property)
  78. ary = [input].flatten
  79. if ary.first.respond_to?('[]') and !ary.first[property].nil?
  80. ary.map {|e| e[property] }
  81. elsif ary.first.respond_to?(property)
  82. ary.map {|e| e.send(property) }
  83. end
  84. end
  85. # Replace occurrences of a string with another
  86. def replace(input, string, replacement = '')
  87. input.to_s.gsub(string, replacement)
  88. end
  89. # Replace the first occurrences of a string with another
  90. def replace_first(input, string, replacement = '')
  91. input.to_s.sub(string, replacement)
  92. end
  93. # remove a substring
  94. def remove(input, string)
  95. input.to_s.gsub(string, '')
  96. end
  97. # remove the first occurrences of a substring
  98. def remove_first(input, string)
  99. input.to_s.sub(string, '')
  100. end
  101. # add one string to another
  102. def append(input, string)
  103. input.to_s + string.to_s
  104. end
  105. # prepend a string to another
  106. def prepend(input, string)
  107. string.to_s + input.to_s
  108. end
  109. # Add <br /> tags in front of all newlines in input string
  110. def newline_to_br(input)
  111. input.to_s.gsub(/\n/, "<br />\n")
  112. end
  113. # Reformat a date
  114. #
  115. # %a - The abbreviated weekday name (``Sun'')
  116. # %A - The full weekday name (``Sunday'')
  117. # %b - The abbreviated month name (``Jan'')
  118. # %B - The full month name (``January'')
  119. # %c - The preferred local date and time representation
  120. # %d - Day of the month (01..31)
  121. # %H - Hour of the day, 24-hour clock (00..23)
  122. # %I - Hour of the day, 12-hour clock (01..12)
  123. # %j - Day of the year (001..366)
  124. # %m - Month of the year (01..12)
  125. # %M - Minute of the hour (00..59)
  126. # %p - Meridian indicator (``AM'' or ``PM'')
  127. # %S - Second of the minute (00..60)
  128. # %U - Week number of the current year,
  129. # starting with the first Sunday as the first
  130. # day of the first week (00..53)
  131. # %W - Week number of the current year,
  132. # starting with the first Monday as the first
  133. # day of the first week (00..53)
  134. # %w - Day of the week (Sunday is 0, 0..6)
  135. # %x - Preferred representation for the date alone, no time
  136. # %X - Preferred representation for the time alone, no date
  137. # %y - Year without a century (00..99)
  138. # %Y - Year with century
  139. # %Z - Time zone name
  140. # %% - Literal ``%'' character
  141. def date(input, format)
  142. if format.to_s.empty?
  143. return input.to_s
  144. end
  145. if ((input.is_a?(String) && !/^\d+$/.match(input.to_s).nil?) || input.is_a?(Integer)) && input.to_i > 0
  146. input = Time.at(input.to_i)
  147. end
  148. date = input.is_a?(String) ? Time.parse(input) : input
  149. if date.respond_to?(:strftime)
  150. date.strftime(format.to_s)
  151. else
  152. input
  153. end
  154. rescue => e
  155. input
  156. end
  157. # Get the first element of the passed in array
  158. #
  159. # Example:
  160. # {{ product.images | first | to_img }}
  161. #
  162. def first(array)
  163. array.first if array.respond_to?(:first)
  164. end
  165. # Get the last element of the passed in array
  166. #
  167. # Example:
  168. # {{ product.images | last | to_img }}
  169. #
  170. def last(array)
  171. array.last if array.respond_to?(:last)
  172. end
  173. # addition
  174. def plus(input, operand)
  175. to_number(input) + to_number(operand)
  176. end
  177. # subtraction
  178. def minus(input, operand)
  179. to_number(input) - to_number(operand)
  180. end
  181. # multiplication
  182. def times(input, operand)
  183. to_number(input) * to_number(operand)
  184. end
  185. # division
  186. def divided_by(input, operand)
  187. to_number(input) / to_number(operand)
  188. end
  189. private
  190. def to_number(obj)
  191. case obj
  192. when Numeric
  193. obj
  194. when String
  195. (obj.strip =~ /^\d+\.\d+$/) ? obj.to_f : obj.to_i
  196. else
  197. 0
  198. end
  199. end
  200. end
  201. Template.register_filter(StandardFilters)
  202. end