Merge pull request #4917 from pathawks/pr/normalize_whitespace

Merge pull request 4917
This commit is contained in:
jekyllbot 2016-06-14 14:03:05 -07:00 committed by GitHub
commit fb1a459ddd
3 changed files with 44 additions and 0 deletions

View File

@ -150,6 +150,15 @@ module Jekyll
URI.escape(input) URI.escape(input)
end 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. # Count the number of words in the input string.
# #
# input - The String on which to operate. # input - The String on which to operate.

View File

@ -256,6 +256,17 @@ common tasks easier.
</p> </p>
</td> </td>
</tr> </tr>
<tr>
<td>
<p class="name"><strong>Normalize Whitespace</strong></p>
<p>Replace any occurance of whitespace with a single space.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ "a \n b" | normalize_whitepace }}{% endraw %}</code>
</p>
</td>
</tr>
<tr> <tr>
<td> <td>
<p class="name"><strong>Sort</strong></p> <p class="name"><strong>Sort</strong></p>

View File

@ -133,6 +133,30 @@ class TestFilters < JekyllUnitTest
) )
end 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 "date filters" do
context "with Time object" do context "with Time object" do
should "format a date with short format" do should "format a date with short format" do