Merge branch 'nitoyon-slugify-new-param'
* nitoyon-slugify-new-param: Remove superfluous Sass declarations. Move the slugify options out to their own section so as to fix the formatting. Document the mode parameter of slugify Liquid filter Add tests for mode parameters of slugify Liquid filter Add mode parameter to slugify Liquid filter Conflicts: lib/jekyll/utils.rb ---> Hadn't added UTF-8 support in nitoyon's PR.
This commit is contained in:
commit
10659e1eef
|
@ -51,12 +51,12 @@ module Jekyll
|
|||
# Slugify a filename or title.
|
||||
#
|
||||
# input - The filename or title to slugify.
|
||||
# mode - how string is slugified
|
||||
#
|
||||
# Returns the given filename or title as a lowercase String, with every
|
||||
# sequence of spaces and non-alphanumeric characters replaced with a
|
||||
# hyphen.
|
||||
def slugify(input)
|
||||
Utils.slugify(input)
|
||||
# Returns the given filename or title as a lowercase URL String.
|
||||
# See Utils.slugify for more detail.
|
||||
def slugify(input, mode=nil)
|
||||
Utils.slugify(input, mode)
|
||||
end
|
||||
|
||||
# Format a date in short format e.g. "27 Jan 2011".
|
||||
|
|
|
@ -104,21 +104,53 @@ module Jekyll
|
|||
|
||||
# Slugify a filename or title.
|
||||
#
|
||||
# name - the filename or title to slugify
|
||||
# string - the filename or title to slugify
|
||||
# mode - how string is slugified
|
||||
#
|
||||
# Returns the given filename or title in lowercase, with every
|
||||
# sequence of spaces and non-alphanumeric characters replaced with a
|
||||
# hyphen.
|
||||
def slugify(string)
|
||||
unless string.nil?
|
||||
string \
|
||||
# Replace each non-alphanumeric character sequence with a hyphen
|
||||
.gsub(/[^[:alnum:]]+/i, '-') \
|
||||
# Remove leading/trailing hyphen
|
||||
.gsub(/^\-|\-$/i, '') \
|
||||
# Downcase it
|
||||
.downcase
|
||||
# When mode is "none", return the given string in lowercase.
|
||||
#
|
||||
# When mode is "raw", return the given string in lowercase,
|
||||
# with every sequence of spaces characters replaced with a hyphen.
|
||||
#
|
||||
# When mode is "default" or nil, non-alphabetic characters are
|
||||
# replaced with a hyphen too.
|
||||
#
|
||||
# When mode is "pretty", some non-alphabetic characters (._~!$&'()+,;=@)
|
||||
# are not replaced with hyphen.
|
||||
#
|
||||
# Examples:
|
||||
# slugify("The _config.yml file")
|
||||
# # => "the-config-yml-file"
|
||||
#
|
||||
# slugify("The _config.yml file", "pretty")
|
||||
# # => "the-_config.yml-file"
|
||||
#
|
||||
# Returns the slugified string.
|
||||
def slugify(string, mode=nil)
|
||||
mode ||= 'default'
|
||||
return nil if string.nil?
|
||||
|
||||
# Replace each character sequence with a hyphen
|
||||
re = case mode
|
||||
when 'raw'
|
||||
Regexp.new('\\s+')
|
||||
when 'default'
|
||||
Regexp.new('[^[:alnum:]]+')
|
||||
when 'pretty'
|
||||
# "._~!$&'()+,;=@" is human readable (not URI-escaped) in URL
|
||||
# and is allowed in both extN and NTFS.
|
||||
Regexp.new("[^a-zA-Z0-9._~!$&'()+,;=@]+")
|
||||
else
|
||||
return string.downcase
|
||||
end
|
||||
|
||||
string.
|
||||
# Strip according to the mode
|
||||
gsub(re, '-').
|
||||
# Remove leading/trailing hyphen
|
||||
gsub(/^\-|\-$/i, '').
|
||||
# Downcase
|
||||
downcase
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -212,11 +212,20 @@ common tasks easier.
|
|||
<tr>
|
||||
<td>
|
||||
<p class="name"><strong>Slugify</strong></p>
|
||||
<p>Convert a string into a lowercase URL "slug" by replacing every sequence of spaces and non-alphanumeric characters with a hyphen.</p>
|
||||
<p>Convert a string into a lowercase URL "slug". See below for options.</p>
|
||||
</td>
|
||||
<td class="align-center">
|
||||
<p>
|
||||
<code class="filter">{% raw %}{{ page.title | slugify }}{% endraw %}</code>
|
||||
<code class="filter">{% raw %}{{ "The _config.yml file" | slugify }}{% endraw %}</code>
|
||||
</p>
|
||||
<p>
|
||||
<code class="output">the-config-yml-file</code>
|
||||
</p>
|
||||
<p>
|
||||
<code class="filter">{% raw %}{{ "The _config.yml file" | slugify: 'pretty' }}{% endraw %}</code>
|
||||
</p>
|
||||
<p>
|
||||
<code class="output">the-_config.yml-file</code>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
@ -252,6 +261,16 @@ common tasks easier.
|
|||
</table>
|
||||
</div>
|
||||
|
||||
### Options for the `slugify` filter
|
||||
|
||||
The `slugify` filter accepts an option, each specifying what to filter.
|
||||
The default is `default`. The are as follows (with what they filter):
|
||||
|
||||
- `none`: no characters
|
||||
- `raw`: spaces
|
||||
- `default`: spaces and non-alphanumeric characters
|
||||
- `pretty`: spaces and non-alphanumeric characters except for `._~!$&'()+,;=@`
|
||||
|
||||
## Tags
|
||||
|
||||
### Includes
|
||||
|
|
|
@ -323,6 +323,10 @@ class TestFilters < Test::Unit::TestCase
|
|||
should "return a slugified string" do
|
||||
assert_equal "q-bert-says", @filter.slugify(" Q*bert says @!#?@!")
|
||||
end
|
||||
|
||||
should "return a slugified string with mode" do
|
||||
assert_equal "q-bert-says-@!-@!", @filter.slugify(" Q*bert says @!#?@!", "pretty")
|
||||
end
|
||||
end
|
||||
|
||||
context "push filter" do
|
||||
|
|
|
@ -144,6 +144,26 @@ class TestUtils < Test::Unit::TestCase
|
|||
Utils.slugify(title)
|
||||
assert_equal "Quick-start guide", title
|
||||
end
|
||||
|
||||
should "not change behaviour if mode is default" do
|
||||
assert_equal "the-config-yml-file", Utils.slugify("The _config.yml file?", "default")
|
||||
end
|
||||
|
||||
should "not change behaviour if mode is nil" do
|
||||
assert_equal "the-config-yml-file", Utils.slugify("The _config.yml file?", nil)
|
||||
end
|
||||
|
||||
should "not replace period and underscore if mode is pretty" do
|
||||
assert_equal "the-_config.yml-file", Utils.slugify("The _config.yml file?", "pretty")
|
||||
end
|
||||
|
||||
should "only replace whitespace if mode is raw" do
|
||||
assert_equal "the-_config.yml-file?", Utils.slugify("The _config.yml file?", "raw")
|
||||
end
|
||||
|
||||
should "return the given string if mode is none" do
|
||||
assert_equal "the _config.yml file?", Utils.slugify("The _config.yml file?", "none")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue