Merge pull request #2864 from kansaichris/add-title-to-collection-urls

This commit is contained in:
Parker Moore 2014-09-05 10:04:38 -07:00
commit 58a76911bb
7 changed files with 122 additions and 15 deletions

View File

@ -45,15 +45,6 @@ module Jekyll
File.basename(path, suffix) File.basename(path, suffix)
end 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. # The extension name of the document.
# #
# Returns the extension name of the document. # Returns the extension name of the document.
@ -138,7 +129,8 @@ module Jekyll
collection: collection.label, collection: collection.label,
path: cleaned_relative_path, path: cleaned_relative_path,
output_ext: Jekyll::Renderer.new(site, self).output_ext, output_ext: Jekyll::Renderer.new(site, self).output_ext,
name: slug name: Utils.slugify(basename(".*")),
title: Utils.slugify(data['title']) || Utils.slugify(basename(".*"))
} }
end end

View File

@ -102,5 +102,22 @@ module Jekyll
!!(File.open(file, 'rb') { |f| f.read(5) } =~ /\A---\r?\n/) !!(File.open(file, 'rb') { |f| f.read(5) } =~ /\A---\r?\n/)
end 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
end end

View File

@ -81,7 +81,7 @@ For example, if you have `_my_collection/some_subdir/some_doc.md`, it will be wr
<p><code>collection</code></p> <p><code>collection</code></p>
</td> </td>
<td> <td>
<p>Label of the containing collection</p> <p>Label of the containing collection.</p>
</td> </td>
</tr> </tr>
<tr> <tr>
@ -89,7 +89,7 @@ For example, if you have `_my_collection/some_subdir/some_doc.md`, it will be wr
<p><code>path</code></p> <p><code>path</code></p>
</td> </td>
<td> <td>
<p>Path to the document relative to the collection's directory</p> <p>Path to the document relative to the collection's directory.</p>
</td> </td>
</tr> </tr>
<tr> <tr>
@ -97,7 +97,15 @@ For example, if you have `_my_collection/some_subdir/some_doc.md`, it will be wr
<p><code>name</code></p> <p><code>name</code></p>
</td> </td>
<td> <td>
<p>The document's base filename, with every sequence of spaces and non-alphanumeric characters replaced by a hyphen</p> <p>The document's base filename, with every sequence of spaces and non-alphanumeric characters replaced by a hyphen.</p>
</td>
</tr>
<tr>
<td>
<p><code>title</code></p>
</td>
<td>
<p>The document's lowercase title (as defined in its <a href="/docs/frontmatter/">front matter</a>), with every sequence of spaces and non-alphanumeric characters replaced by a hyphen. If the document does not define a title in its <a href="/docs/frontmatter/">front matter</a>, this is equivalent to <code>name</code>.</p>
</td> </td>
</tr> </tr>
<tr> <tr>
@ -105,7 +113,7 @@ For example, if you have `_my_collection/some_subdir/some_doc.md`, it will be wr
<p><code>output_ext</code></p> <p><code>output_ext</code></p>
</td> </td>
<td> <td>
<p>Extension of the output file</p> <p>Extension of the output file.</p>
</td> </td>
</tr> </tr>
</tbody> </tbody>

View File

@ -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 projects page, blog, or website from GitHubs servers **for
free**.

View File

@ -0,0 +1,5 @@
---
layout: slide
---
Wooot

View File

@ -186,7 +186,7 @@ class TestDocument < Test::Unit::TestCase
end end
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 setup do
@site = Site.new(Jekyll.configuration({ @site = Site.new(Jekyll.configuration({
"collections" => { "collections" => {
@ -207,6 +207,32 @@ class TestDocument < Test::Unit::TestCase
end end
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 context "a static file in a collection" do
setup do setup do
@site = Site.new(Jekyll.configuration({ @site = Site.new(Jekyll.configuration({

View File

@ -97,4 +97,48 @@ class TestUtils < Test::Unit::TestCase
end end
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 end