From 91e7033d5474296f347df2731678b49b893cd379 Mon Sep 17 00:00:00 2001 From: Artem Baguinski Date: Fri, 5 Apr 2013 13:58:41 +0200 Subject: [PATCH 1/3] a first stab at partials_dir setting re #252 --- .../lib/middleman-core/application.rb | 4 ++++ .../core_extensions/rendering.rb | 21 +++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index 3b61eb79..c9ec754f 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -87,6 +87,10 @@ module Middleman # @return [String] config.define_setting :fonts_dir, "fonts", 'Location of fonts within source' + # Location of partials within source. Used by renderers. + # @return [String] + config.define_setting :partials_dir, "", 'Location of partials within source' + # Where to build output files # @return [String] config.define_setting :build_dir, "build", 'Where to build output files' diff --git a/middleman-core/lib/middleman-core/core_extensions/rendering.rb b/middleman-core/lib/middleman-core/core_extensions/rendering.rb index 77fae43f..4bcd3456 100644 --- a/middleman-core/lib/middleman-core/core_extensions/rendering.rb +++ b/middleman-core/lib/middleman-core/core_extensions/rendering.rb @@ -196,27 +196,26 @@ module Middleman engine = File.extname(resource.source_file)[1..-1].to_sym # Look for partials relative to the current path - if current_dir != self.source_dir - relative_dir = File.join(current_dir.sub("#{self.source_dir}/", ""), data) + relative_dir = File.join(current_dir.sub(%r{^#{self.source_dir}/?}, ""), data) - # Try to use the current engine first - found_partial, found_engine = resolve_template(relative_dir, :preferred_engine => engine, :try_without_underscore => true) + # Try to use the current engine first + found_partial, found_engine = resolve_template(relative_dir, :preferred_engine => engine, :try_without_underscore => true) - # Fall back to any engine available - if !found_partial - found_partial, found_engine = resolve_template(relative_dir, :try_without_underscore => true) - end + # Fall back to any engine available + if !found_partial + found_partial, found_engine = resolve_template(relative_dir, :try_without_underscore => true) end end - # Look in the root for the partial with the current engine + # Look in the partials_dir for the partial with the current engine + partials_path = File.join(config[:partials_dir], data) if !found_partial && !engine.nil? - found_partial, found_engine = resolve_template(data, :preferred_engine => engine, :try_without_underscore => true) + found_partial, found_engine = resolve_template(partials_path, :preferred_engine => engine, :try_without_underscore => true) end # Look in the root with any engine if !found_partial - found_partial, found_engine = resolve_template(data, :try_without_underscore => true) + found_partial, found_engine = resolve_template(partials_path, :try_without_underscore => true) end # Render the partial if found, otherwide throw exception From fe20d92b2809c036afdb8d2ae2999dd8e0f1a59c Mon Sep 17 00:00:00 2001 From: Artem Baguinski Date: Mon, 8 Apr 2013 09:39:03 +0200 Subject: [PATCH 2/3] feature for partials_dir setting --- middleman-more/features/partials_dir.feature | 20 +++++++++++++++++++ .../source/_partial.html.erb | 1 + .../source/index.html.erb | 2 ++ .../partials-dir-app/source/index.html.erb | 2 ++ .../source/partials/_partial.html.erb | 1 + 5 files changed, 26 insertions(+) create mode 100644 middleman-more/features/partials_dir.feature create mode 100644 middleman-more/fixtures/default-partials-dir-app/source/_partial.html.erb create mode 100644 middleman-more/fixtures/default-partials-dir-app/source/index.html.erb create mode 100644 middleman-more/fixtures/partials-dir-app/source/index.html.erb create mode 100644 middleman-more/fixtures/partials-dir-app/source/partials/_partial.html.erb diff --git a/middleman-more/features/partials_dir.feature b/middleman-more/features/partials_dir.feature new file mode 100644 index 00000000..52211565 --- /dev/null +++ b/middleman-more/features/partials_dir.feature @@ -0,0 +1,20 @@ +Feature: Partials dir + Scenario: Find partials in the custom partials dir + Given a fixture app "partials-dir-app" + And a file named "config.rb" with: + """ + set :partials_dir, 'partials' + """ + And the Server is running + When I go to "/index.html" + Then I should see "contents of the partial" + + Scenario: Find partials in the default partials dir + Given a fixture app "default-partials-dir-app" + And a file named "config.rb" with: + """ + """ + And the Server is running + When I go to "/index.html" + Then I should see "contents of the partial" + diff --git a/middleman-more/fixtures/default-partials-dir-app/source/_partial.html.erb b/middleman-more/fixtures/default-partials-dir-app/source/_partial.html.erb new file mode 100644 index 00000000..5b50edf3 --- /dev/null +++ b/middleman-more/fixtures/default-partials-dir-app/source/_partial.html.erb @@ -0,0 +1 @@ +contents of the partial diff --git a/middleman-more/fixtures/default-partials-dir-app/source/index.html.erb b/middleman-more/fixtures/default-partials-dir-app/source/index.html.erb new file mode 100644 index 00000000..871728d4 --- /dev/null +++ b/middleman-more/fixtures/default-partials-dir-app/source/index.html.erb @@ -0,0 +1,2 @@ +<%= partial 'partial' %> + diff --git a/middleman-more/fixtures/partials-dir-app/source/index.html.erb b/middleman-more/fixtures/partials-dir-app/source/index.html.erb new file mode 100644 index 00000000..871728d4 --- /dev/null +++ b/middleman-more/fixtures/partials-dir-app/source/index.html.erb @@ -0,0 +1,2 @@ +<%= partial 'partial' %> + diff --git a/middleman-more/fixtures/partials-dir-app/source/partials/_partial.html.erb b/middleman-more/fixtures/partials-dir-app/source/partials/_partial.html.erb new file mode 100644 index 00000000..5b50edf3 --- /dev/null +++ b/middleman-more/fixtures/partials-dir-app/source/partials/_partial.html.erb @@ -0,0 +1 @@ +contents of the partial From cf1f6a095707ba95d7f037ebebfa952249fc13db Mon Sep 17 00:00:00 2001 From: Artem Baguinski Date: Tue, 9 Apr 2013 08:45:34 +0200 Subject: [PATCH 3/3] test nested partials_dir --- middleman-more/features/partials_dir.feature | 12 +++++++++++- .../source/nested/partials/_partial.html.erb | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 middleman-more/fixtures/partials-dir-app/source/nested/partials/_partial.html.erb diff --git a/middleman-more/features/partials_dir.feature b/middleman-more/features/partials_dir.feature index 52211565..8648c8e2 100644 --- a/middleman-more/features/partials_dir.feature +++ b/middleman-more/features/partials_dir.feature @@ -1,5 +1,5 @@ Feature: Partials dir - Scenario: Find partials in the custom partials dir + Scenario: Find partials in a custom partials dir Given a fixture app "partials-dir-app" And a file named "config.rb" with: """ @@ -9,6 +9,16 @@ Feature: Partials dir When I go to "/index.html" Then I should see "contents of the partial" + Scenario: Find partials in a nested custom partials dir + Given a fixture app "partials-dir-app" + And a file named "config.rb" with: + """ + set :partials_dir, 'nested/partials' + """ + And the Server is running + When I go to "/index.html" + Then I should see "contents of the nested partial" + Scenario: Find partials in the default partials dir Given a fixture app "default-partials-dir-app" And a file named "config.rb" with: diff --git a/middleman-more/fixtures/partials-dir-app/source/nested/partials/_partial.html.erb b/middleman-more/fixtures/partials-dir-app/source/nested/partials/_partial.html.erb new file mode 100644 index 00000000..b27433e1 --- /dev/null +++ b/middleman-more/fixtures/partials-dir-app/source/nested/partials/_partial.html.erb @@ -0,0 +1 @@ +contents of the nested partial