From f7714add1506b06a1352b17b2dedc415ea87911a Mon Sep 17 00:00:00 2001
From: Pat Hawks
Date: Thu, 19 May 2016 19:59:22 -0700
Subject: [PATCH 1/2] Add normalize_whitepace filter
---
lib/jekyll/filters.rb | 9 +++++++++
test/test_filters.rb | 24 ++++++++++++++++++++++++
2 files changed, 33 insertions(+)
diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb
index 65fc7e41..a996c496 100644
--- a/lib/jekyll/filters.rb
+++ b/lib/jekyll/filters.rb
@@ -150,6 +150,15 @@ module Jekyll
URI.escape(input)
end
+ # Replace any whitespace in the input string with a single space
+ #
+ # input - The String on which to operate.
+ #
+ # Returns the formatted String
+ def normalize_whitespace(input)
+ input.to_s.gsub(/\s+/, " ").strip
+ end
+
# Count the number of words in the input string.
#
# input - The String on which to operate.
diff --git a/test/test_filters.rb b/test/test_filters.rb
index 565e8208..7c10c8df 100644
--- a/test/test_filters.rb
+++ b/test/test_filters.rb
@@ -86,6 +86,30 @@ class TestFilters < JekyllUnitTest
assert_equal "chunky, bacon, bits, and pieces", @filter.array_to_sentence_string(["chunky", "bacon", "bits", "pieces"])
end
+ context "normalize_whitespace filter" do
+ should "replace newlines with a space" do
+ assert_equal "a b", @filter.normalize_whitespace("a\nb")
+ assert_equal "a b", @filter.normalize_whitespace("a\n\nb")
+ end
+
+ should "replace tabs with a space" do
+ assert_equal "a b", @filter.normalize_whitespace("a\tb")
+ assert_equal "a b", @filter.normalize_whitespace("a\t\tb")
+ end
+
+ should "replace multiple spaces with a single space" do
+ assert_equal "a b", @filter.normalize_whitespace("a b")
+ assert_equal "a b", @filter.normalize_whitespace("a\t\nb")
+ assert_equal "a b", @filter.normalize_whitespace("a \t \n\nb")
+ end
+
+ should "strip whitespace from begining and end of string" do
+ assert_equal "a", @filter.normalize_whitespace("a ")
+ assert_equal "a", @filter.normalize_whitespace(" a")
+ assert_equal "a", @filter.normalize_whitespace(" a ")
+ end
+ end
+
context "date filters" do
context "with Time object" do
should "format a date with short format" do
From 2c567a6dc358221b810355807a7f4df3adf3f8c1 Mon Sep 17 00:00:00 2001
From: Pat Hawks
Date: Tue, 24 May 2016 16:36:50 -0500
Subject: [PATCH 2/2] Document normalize_whitepace filter
---
site/_docs/templates.md | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/site/_docs/templates.md b/site/_docs/templates.md
index 1c09cab0..d46911e5 100644
--- a/site/_docs/templates.md
+++ b/site/_docs/templates.md
@@ -256,6 +256,17 @@ common tasks easier.
+
+
+ Normalize Whitespace
+ Replace any occurance of whitespace with a single space.
+ |
+
+
+ {% raw %}{{ "a \n b" | normalize_whitepace }}{% endraw %}
+
+ |
+
Sort
|