From 46e76bde3f052dd3e9ab3cab3d1dfdc0621626d9 Mon Sep 17 00:00:00 2001 From: nitoyon Date: Sun, 14 Sep 2014 02:38:37 +0900 Subject: [PATCH 1/5] Add mode parameter to slugify Liquid filter --- lib/jekyll/filters.rb | 10 ++++---- lib/jekyll/utils.rb | 53 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index d6f0fa03..5cfcf7cb 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -50,12 +50,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". diff --git a/lib/jekyll/utils.rb b/lib/jekyll/utils.rb index d528f22d..b0552168 100644 --- a/lib/jekyll/utils.rb +++ b/lib/jekyll/utils.rb @@ -104,19 +104,50 @@ 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? - # Replace each non-alphanumeric character sequence with a hyphen - slug = string.gsub(/[^a-z0-9]+/i, '-') - # Remove leading/trailing hyphen - slug.gsub!(/^\-|\-$/i, '') - slug.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('[^a-zA-Z0-9]+') + 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 + slug = string.gsub(re, '-') + + # Remove leading/trailing hyphen + slug.gsub!(/^\-|\-$/i, '') + slug.downcase end end From 3c5e9f53346d8fbe2ed1e079f9377c603aecf93e Mon Sep 17 00:00:00 2001 From: nitoyon Date: Sun, 14 Sep 2014 02:43:17 +0900 Subject: [PATCH 2/5] Add tests for mode parameters of slugify Liquid filter --- test/test_filters.rb | 4 ++++ test/test_utils.rb | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/test/test_filters.rb b/test/test_filters.rb index 35ea6f0b..a6be0800 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -221,6 +221,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 diff --git a/test/test_utils.rb b/test/test_utils.rb index ddc8750e..fc2d9b5d 100644 --- a/test/test_utils.rb +++ b/test/test_utils.rb @@ -139,6 +139,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 From ae57f693e450dc5d67c042db522b3b1c2fa94e66 Mon Sep 17 00:00:00 2001 From: nitoyon Date: Sun, 14 Sep 2014 02:43:25 +0900 Subject: [PATCH 3/5] Document the mode parameter of slugify Liquid filter --- site/_docs/templates.md | 20 ++++++++++++++++++-- site/_sass/_style.scss | 8 ++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/site/_docs/templates.md b/site/_docs/templates.md index fbf63626..36a06e4c 100644 --- a/site/_docs/templates.md +++ b/site/_docs/templates.md @@ -214,11 +214,27 @@ common tasks easier.

Slugify

-

Convert a string into a lowercase URL "slug" by replacing every sequence of spaces and non-alphanumeric characters with a hyphen.

+

Convert a string into a lowercase URL "slug".

+

Optional argument mode specify what characters are replaced with a hyphen. The default value is 'default'.

+
    +
  • 'none': no characters
  • +
  • 'raw': spaces
  • +
  • 'default': spaces and non-alphanumeric characters
  • +
  • 'pretty': spaces and non-alphanumeric characters except for ._~!$&'()+,;=@
  • +

- {% raw %}{{ page.title | slugify }}{% endraw %} + {% raw %}{{ "The _config.yml file" | slugify }}{% endraw %} +

+

+ the-config-yml-file +

+

+ {% raw %}{{ "The _config.yml file" | slugify: 'pretty' }}{% endraw %} +

+

+ the-_config.yml-file

diff --git a/site/_sass/_style.scss b/site/_sass/_style.scss index 16cc902f..8f635e3f 100644 --- a/site/_sass/_style.scss +++ b/site/_sass/_style.scss @@ -834,10 +834,14 @@ td { padding: .5em .75em; } -td p { +td p, td ul, article td li { margin: 0; } +td ul { + padding: 0 0 0 1.2em; +} + th { text-transform: uppercase; font-size: 16px; @@ -861,7 +865,7 @@ tbody td { filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1affffff', endColorstr='#00ffffff',GradientType=0 ); } -td p { +td p, td ul { font-size: 16px; } From 6081fcd75acdc31f45f2dddf7411d4857fda4d4e Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sat, 17 Jan 2015 16:00:35 -0800 Subject: [PATCH 4/5] Move the slugify options out to their own section so as to fix the formatting. --- site/_docs/templates.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/site/_docs/templates.md b/site/_docs/templates.md index 36a06e4c..cc1d9d87 100644 --- a/site/_docs/templates.md +++ b/site/_docs/templates.md @@ -214,14 +214,7 @@ common tasks easier.

Slugify

-

Convert a string into a lowercase URL "slug".

-

Optional argument mode specify what characters are replaced with a hyphen. The default value is 'default'.

-
    -
  • 'none': no characters
  • -
  • 'raw': spaces
  • -
  • 'default': spaces and non-alphanumeric characters
  • -
  • 'pretty': spaces and non-alphanumeric characters except for ._~!$&'()+,;=@
  • -
+

Convert a string into a lowercase URL "slug". See below for options.

@@ -270,6 +263,16 @@ common tasks easier. +### 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 From aaea08e094f2f38f15727718dc7d6a5edb8bbd2f Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sat, 17 Jan 2015 16:02:33 -0800 Subject: [PATCH 5/5] Remove superfluous Sass declarations. --- site/_sass/_style.scss | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/site/_sass/_style.scss b/site/_sass/_style.scss index 8f635e3f..16cc902f 100644 --- a/site/_sass/_style.scss +++ b/site/_sass/_style.scss @@ -834,14 +834,10 @@ td { padding: .5em .75em; } -td p, td ul, article td li { +td p { margin: 0; } -td ul { - padding: 0 0 0 1.2em; -} - th { text-transform: uppercase; font-size: 16px; @@ -865,7 +861,7 @@ tbody td { filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1affffff', endColorstr='#00ffffff',GradientType=0 ); } -td p, td ul { +td p { font-size: 16px; }