Add spaceless tag

This commit is contained in:
Justin Li
2015-06-26 10:00:34 -07:00
parent 8a8de46c6a
commit 74f9bad513
4 changed files with 39 additions and 0 deletions
+5
View File
@@ -105,6 +105,11 @@ module Liquid
input.to_s.gsub(/\r?\n/, ''.freeze)
end
# Remove whitespace between HTML tags
def strip_html_whitespace(input)
Spaceless.strip_html_whitespace(input.to_s)
end
# Join elements of the array with certain character between them
def join(input, glue = ' '.freeze)
InputIterator.new(input).join(glue)
+24
View File
@@ -0,0 +1,24 @@
module Liquid
# The spaceless tag strips whitespace between HTML tags using a simple regex.
#
# == Usage:
# {% spaceless %}
# <h1>
# Hello
# </h1>
# {% endspaceless %}
#
class Spaceless < Block
HTML_STRIP_SPACE_REGEXP = />\s+</
def render(context)
self.class.strip_html_whitespace(super)
end
def self.strip_html_whitespace(input)
input.gsub(HTML_STRIP_SPACE_REGEXP, '><')
end
end
Template.register_tag('spaceless'.freeze, Spaceless)
end