From ba6fcfeca9947d626e50d224e9c928aa9751b480 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Sun, 18 Dec 2011 15:54:09 +0100 Subject: [PATCH] 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. --- lib/chili_project/liquid/filters.rb | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/chili_project/liquid/filters.rb b/lib/chili_project/liquid/filters.rb index 28faa5b4..469f3900 100644 --- a/lib/chili_project/liquid/filters.rb +++ b/lib/chili_project/liquid/filters.rb @@ -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
tags in front of all newlines in input string + def newline_to_br(input) + input.to_s.gsub(/(\r?\n)/, "
\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