From 921dbe0547deb096a0a748faf86781fec64bda7c Mon Sep 17 00:00:00 2001 From: Mathieu Bruyen Date: Sat, 17 May 2014 10:40:52 +0200 Subject: [PATCH] Override collection url template --- features/collections.feature | 14 +++++++++++ lib/jekyll/collection.rb | 7 ++++++ lib/jekyll/document.rb | 2 +- site/docs/collections.md | 48 ++++++++++++++++++++++++++++++++++++ test/test_collections.rb | 22 +++++++++++++++++ 5 files changed, 92 insertions(+), 1 deletion(-) diff --git a/features/collections.feature b/features/collections.feature index 5c75677f..4f69e8b9 100644 --- a/features/collections.feature +++ b/features/collections.feature @@ -29,6 +29,20 @@ Feature: Collections And I should see "Methods metadata: bar" in "_site/collection_metadata.html" And I should see "

Whatever: foo.bar

" in "_site/methods/configuration.html" + Scenario: Rendered collection at a custom URL + Given I have an "index.html" page that contains "Collections: {{ site.collections }}" + And I have fixture collections + And I have a "_config.yml" file with content: + """ + collections: + methods: + output: true + permalink: /:collection/:path/ + """ + When I run jekyll build + Then the _site directory should exist + And I should see "

Whatever: foo.bar

" in "_site/methods/configuration/index.html" + Scenario: Rendered document in a layout Given I have an "index.html" page that contains "Collections: {{ site.collections }}" And I have a default layout that contains "
Tom Preston-Werner
{{content}}" diff --git a/lib/jekyll/collection.rb b/lib/jekyll/collection.rb index dcb7b50d..03474582 100644 --- a/lib/jekyll/collection.rb +++ b/lib/jekyll/collection.rb @@ -132,6 +132,13 @@ module Jekyll !!metadata['output'] end + # The URL template to render collection's documents at. + # + # Returns the URL template to render collection's documents at. + def url_template + metadata.fetch('permalink', "/:collection/:path:output_ext") + end + # Extract options for this collection from the site configuration. # # Returns the metadata for this collection diff --git a/lib/jekyll/document.rb b/lib/jekyll/document.rb index be4ead20..50aeaa14 100644 --- a/lib/jekyll/document.rb +++ b/lib/jekyll/document.rb @@ -92,7 +92,7 @@ module Jekyll # # Returns the URL template for the document. def url_template - "/:collection/:path:output_ext" + collection.url_template end # Construct a Hash of key-value pairs which contain a mapping between diff --git a/site/docs/collections.md b/site/docs/collections.md index 1cbd5d02..1d3a14e5 100644 --- a/site/docs/collections.md +++ b/site/docs/collections.md @@ -56,6 +56,54 @@ For example, if you have `_my_collection/some_subdir/some_doc.md`, it will be rendered using Liquid and the Markdown converter of your choice and written out to `/my_collection/some_subdir/some_doc.html`. +As for posts with [Permalinks](../Permalinks/), document URL can be customized by setting a `permalink` metadata to the collection: + +{% highlight yaml %} +collections: + my_collection: + output: true + permalink: /awesome/:path/ +{% endhighlight %} + +For example, if you have `_my_collection/some_subdir/some_doc.md`, it will be written out to `/awesome/some_subdir/some_doc/index.html`. + +
+ + + + + + + + + + + + + + + + + + + + + +
VariableDescription
+

collection

+
+

Label of the containing collection

+
+

path

+
+

Path to the document relative to the collection's directory

+
+

output_ext

+
+

Extension of the output file

+
+
+ ## Liquid Attributes ### Collections diff --git a/test/test_collections.rb b/test/test_collections.rb index eb8a7e32..6cdb3184 100644 --- a/test/test_collections.rb +++ b/test/test_collections.rb @@ -38,6 +38,10 @@ class TestCollections < Test::Unit::TestCase assert_equal @collection.label, "methods" end + should "have default url template" do + assert_equal @collection.url_template, "/:collection/:path:output_ext" + end + should "contain no docs when initialized" do assert_empty @collection.docs end @@ -91,6 +95,24 @@ class TestCollections < Test::Unit::TestCase end end + context "a collection with permalink" do + setup do + @site = fixture_site({ + "collections" => { + "methods" => { + "permalink" => "/awesome/:path/" + } + } + }) + @site.process + @collection = @site.collections["methods"] + end + + should "have custom url template" do + assert_equal @collection.url_template, "/awesome/:path/" + end + end + context "with a collection" do setup do @site = fixture_site({