Override some filters of Liquid core.

These filters change the implemntation of sub and gsub to use the block method.
This prevents the evaluation of backreferences in the replacement text. See
https://gist.github.com/1491437 for examples.

Also, it allows split to be called without arguments to split strings on
whitespace.
This commit is contained in:
Holger Just 2011-12-18 15:54:09 +01:00
parent 2733e5173f
commit ba6fcfeca9
1 changed files with 33 additions and 0 deletions

View File

@ -13,6 +13,38 @@
module ChiliProject
module Liquid
module OverriddenFilters
# These filters are defined in liquid core but are overwritten here
# to improve on their implementation
# Split input string into an array of substrings separated by given pattern.
# Default to whitespace
def split(input, pattern=nil)
input.split(pattern)
end
def strip_newlines(input)
input.to_s.gsub(/\r?\n/, '')
end
# Add <br /> tags in front of all newlines in input string
def newline_to_br(input)
input.to_s.gsub(/(\r?\n)/, "<br />\1")
end
# Use the block systax for sub and gsub to prevent interpretation of
# backreferences
# See https://gist.github.com/1491437
def replace(input, string, replacement = '')
input.to_s.gsub(string){replacement}
end
# Replace the first occurrences of a string with another
def replace_first(input, string, replacement = '')
input.to_s.sub(string){replacement}
end
end
module Filters
def default(input, default)
input.to_s.strip.present? ? input : default
@ -23,6 +55,7 @@ module ChiliProject
end
end
Template.register_filter(OverriddenFilters)
Template.register_filter(Filters)
end
end