diff --git a/lib/jekyll/document.rb b/lib/jekyll/document.rb index db471c32..f4761ef8 100644 --- a/lib/jekyll/document.rb +++ b/lib/jekyll/document.rb @@ -45,15 +45,6 @@ module Jekyll File.basename(path, suffix) end - # The sluggified base filename of the document. - # - # Returns the base filename of the document in lowercase, with every - # sequence of spaces and non-alphanumeric characters replaced with a - # hyphen. - def slug - File.basename(path, ".*").downcase.gsub(/[\W\s]+/, '-') - end - # The extension name of the document. # # Returns the extension name of the document. @@ -138,7 +129,8 @@ module Jekyll collection: collection.label, path: cleaned_relative_path, output_ext: Jekyll::Renderer.new(site, self).output_ext, - name: slug + name: Utils.slugify(basename(".*")), + title: Utils.slugify(data['title']) || Utils.slugify(basename(".*")) } end diff --git a/lib/jekyll/utils.rb b/lib/jekyll/utils.rb index f3dbf9d7..d528f22d 100644 --- a/lib/jekyll/utils.rb +++ b/lib/jekyll/utils.rb @@ -102,5 +102,22 @@ module Jekyll !!(File.open(file, 'rb') { |f| f.read(5) } =~ /\A---\r?\n/) end + # Slugify a filename or title. + # + # name - the filename or title to slugify + # + # 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 + end + end + end end diff --git a/site/_docs/collections.md b/site/_docs/collections.md index b4078c17..94e1ef44 100644 --- a/site/_docs/collections.md +++ b/site/_docs/collections.md @@ -81,7 +81,7 @@ For example, if you have `_my_collection/some_subdir/some_doc.md`, it will be wr

collection

-

Label of the containing collection

+

Label of the containing collection.

@@ -89,7 +89,7 @@ For example, if you have `_my_collection/some_subdir/some_doc.md`, it will be wr

path

-

Path to the document relative to the collection's directory

+

Path to the document relative to the collection's directory.

@@ -97,7 +97,15 @@ For example, if you have `_my_collection/some_subdir/some_doc.md`, it will be wr

name

-

The document's base filename, with every sequence of spaces and non-alphanumeric characters replaced by a hyphen

+

The document's base filename, with every sequence of spaces and non-alphanumeric characters replaced by a hyphen.

+ + + + +

title

+ + +

The document's lowercase title (as defined in its front matter), with every sequence of spaces and non-alphanumeric characters replaced by a hyphen. If the document does not define a title in its front matter, this is equivalent to name.

@@ -105,7 +113,7 @@ For example, if you have `_my_collection/some_subdir/some_doc.md`, it will be wr

output_ext

-

Extension of the output file

+

Extension of the output file.

diff --git a/test/source/_slides/example-slide-4.html b/test/source/_slides/example-slide-4.html new file mode 100644 index 00000000..6cec4eae --- /dev/null +++ b/test/source/_slides/example-slide-4.html @@ -0,0 +1,15 @@ +--- + title: So what is Jekyll, exactly? + layout: slide +--- + +Jekyll is a simple, blog-aware, static site generator. It takes a template +directory containing raw text files in various formats, runs it through +[Markdown](http://daringfireball.net/projects/markdown/) (or +[Textile](http://redcloth.org/textile)) and +[Liquid](http://wiki.shopify.com/Liquid) +converters, and spits out a complete, ready-to-publish static website suitable +for serving with your favorite web server. Jekyll also happens to be the engine +behind [GitHub Pages](http://pages.github.com), which means you can use Jekyll +to host your project’s page, blog, or website from GitHub’s servers **for +free**. diff --git a/test/source/_slides/example-slide-5.html b/test/source/_slides/example-slide-5.html new file mode 100644 index 00000000..622df123 --- /dev/null +++ b/test/source/_slides/example-slide-5.html @@ -0,0 +1,5 @@ +--- + layout: slide +--- + +Wooot diff --git a/test/test_document.rb b/test/test_document.rb index 68ca8d92..5e26173f 100644 --- a/test/test_document.rb +++ b/test/test_document.rb @@ -186,7 +186,7 @@ class TestDocument < Test::Unit::TestCase end end - context "a document in a collection with custom permalinks" do + context "a document in a collection with custom filename permalinks" do setup do @site = Site.new(Jekyll.configuration({ "collections" => { @@ -207,6 +207,32 @@ class TestDocument < Test::Unit::TestCase end end + context "documents in a collection with custom title permalinks" do + setup do + @site = Site.new(Jekyll.configuration({ + "collections" => { + "slides" => { + "output" => true, + "permalink" => "/slides/:title" + } + }, + "source" => source_dir, + "destination" => dest_dir + })) + @site.process + @document = @site.collections["slides"].docs[3] + @document_without_title = @site.collections["slides"].docs[4] + end + + should "produce the right URL if they have a title" do + assert_equal "/slides/so-what-is-jekyll-exactly", @document.url + end + + should "produce the right URL if they don't have a title" do + assert_equal "/slides/example-slide-5", @document_without_title.url + end + end + context "a static file in a collection" do setup do @site = Site.new(Jekyll.configuration({ diff --git a/test/test_utils.rb b/test/test_utils.rb index d4daa097..ddc8750e 100644 --- a/test/test_utils.rb +++ b/test/test_utils.rb @@ -97,4 +97,48 @@ class TestUtils < Test::Unit::TestCase end end + context "The \`Utils.slugify\` method" do + should "return nil if passed nil" do + begin + assert Utils.slugify(nil).nil? + rescue NoMethodError + assert false, "Threw NoMethodError" + end + end + + should "replace whitespace with hyphens" do + assert_equal "working-with-drafts", Utils.slugify("Working with drafts") + end + + should "replace consecutive whitespace with a single hyphen" do + assert_equal "basic-usage", Utils.slugify("Basic Usage") + end + + should "trim leading and trailing whitespace" do + assert_equal "working-with-drafts", Utils.slugify(" Working with drafts ") + end + + should "drop trailing punctuation" do + assert_equal "so-what-is-jekyll-exactly", Utils.slugify("So what is Jekyll, exactly?") + end + + should "ignore hyphens" do + assert_equal "pre-releases", Utils.slugify("Pre-releases") + end + + should "replace underscores with hyphens" do + assert_equal "the-config-yml-file", Utils.slugify("The _config.yml file") + end + + should "combine adjacent hyphens and spaces" do + assert_equal "customizing-git-git-hooks", Utils.slugify("Customizing Git - Git Hooks") + end + + should "not modify the original string" do + title = "Quick-start guide" + Utils.slugify(title) + assert_equal "Quick-start guide", title + end + end + end