From 7c50c9e4d510eb022f134a16c7ec7f98463cc88c Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sat, 26 Nov 2011 15:32:41 -0800 Subject: [PATCH] tests for partials --- features/partials.feature | 30 ++++++++++++++ features/tiny_src.feature | 15 ------- fixtures/partials-app/config.rb | 0 fixtures/partials-app/source/_main.erb | 1 + fixtures/partials-app/source/_main.haml | 1 + fixtures/partials-app/source/index.html.erb | 3 ++ fixtures/partials-app/source/second.html.haml | 3 ++ .../partials-app/source/shared/_footer.erb | 1 + .../partials-app/source/shared/_header.erb | 1 + fixtures/partials-app/source/sub/_local.erb | 1 + .../partials-app/source/sub/index.html.erb | 3 ++ lib/middleman/base.rb | 40 +++++++++++++++---- lib/middleman/sitemap/template.rb | 16 ++++---- 13 files changed, 84 insertions(+), 31 deletions(-) create mode 100644 features/partials.feature delete mode 100644 features/tiny_src.feature create mode 100644 fixtures/partials-app/config.rb create mode 100644 fixtures/partials-app/source/_main.erb create mode 100644 fixtures/partials-app/source/_main.haml create mode 100755 fixtures/partials-app/source/index.html.erb create mode 100755 fixtures/partials-app/source/second.html.haml create mode 100644 fixtures/partials-app/source/shared/_footer.erb create mode 100644 fixtures/partials-app/source/shared/_header.erb create mode 100644 fixtures/partials-app/source/sub/_local.erb create mode 100644 fixtures/partials-app/source/sub/index.html.erb diff --git a/features/partials.feature b/features/partials.feature new file mode 100644 index 00000000..3473a784 --- /dev/null +++ b/features/partials.feature @@ -0,0 +1,30 @@ +Feature: Provide Sane Defaults for Partial Behavior + + Scenario: Finds shared partials relative to the root + Given the Server is running at "partials-app" + When I go to "/index.html" + Then I should see "Header" + And I should see "Footer" + + Scenario: Finds shared partials relative to the root (sub) + Given the Server is running at "partials-app" + When I go to "/sub/index.html" + Then I should see "Header" + And I should see "Footer" + + Scenario: Prefers partials of the same engine type + Given the Server is running at "partials-app" + When I go to "/index.html" + Then I should see "ERb Main" + + Scenario: Prefers partials of the same engine type + Given the Server is running at "partials-app" + When I go to "/second.html" + Then I should see "Haml Main" + And I should see "Header" + And I should see "Footer" + + Scenario: Finds partial relative to template + Given the Server is running at "partials-app" + When I go to "/sub/index.html" + Then I should see "Local Partial" \ No newline at end of file diff --git a/features/tiny_src.feature b/features/tiny_src.feature deleted file mode 100644 index 01ee0bd6..00000000 --- a/features/tiny_src.feature +++ /dev/null @@ -1,15 +0,0 @@ -@wip -Feature: Tiny Src - In order automatically scale images for mobile devices - - Scenario: Rendering html with the feature disabled - Given "tiny_src" feature is "disabled" - And the Server is running at "test-app" - When I go to "/tiny_src.html" - Then I should see "http://test.com/image.jpg" - - Scenario: Rendering html with the feature enabled - Given "tiny_src" feature is "enabled" - And the Server is running at "test-app" - When I go to "/tiny_src.html" - Then I should see "http://i.tinysrc.mobi/http://test.com/image.jpg" \ No newline at end of file diff --git a/fixtures/partials-app/config.rb b/fixtures/partials-app/config.rb new file mode 100644 index 00000000..e69de29b diff --git a/fixtures/partials-app/source/_main.erb b/fixtures/partials-app/source/_main.erb new file mode 100644 index 00000000..8003e446 --- /dev/null +++ b/fixtures/partials-app/source/_main.erb @@ -0,0 +1 @@ +ERb Main \ No newline at end of file diff --git a/fixtures/partials-app/source/_main.haml b/fixtures/partials-app/source/_main.haml new file mode 100644 index 00000000..0dde75aa --- /dev/null +++ b/fixtures/partials-app/source/_main.haml @@ -0,0 +1 @@ +Haml Main \ No newline at end of file diff --git a/fixtures/partials-app/source/index.html.erb b/fixtures/partials-app/source/index.html.erb new file mode 100755 index 00000000..3e2cdff9 --- /dev/null +++ b/fixtures/partials-app/source/index.html.erb @@ -0,0 +1,3 @@ +<%= partial "shared/header" %> +<%= partial "main" %> +<%= partial "shared/footer" %> \ No newline at end of file diff --git a/fixtures/partials-app/source/second.html.haml b/fixtures/partials-app/source/second.html.haml new file mode 100755 index 00000000..f6e15653 --- /dev/null +++ b/fixtures/partials-app/source/second.html.haml @@ -0,0 +1,3 @@ += partial "shared/header" += partial "main" += partial "shared/footer" \ No newline at end of file diff --git a/fixtures/partials-app/source/shared/_footer.erb b/fixtures/partials-app/source/shared/_footer.erb new file mode 100644 index 00000000..763283eb --- /dev/null +++ b/fixtures/partials-app/source/shared/_footer.erb @@ -0,0 +1 @@ +%footer Footer \ No newline at end of file diff --git a/fixtures/partials-app/source/shared/_header.erb b/fixtures/partials-app/source/shared/_header.erb new file mode 100644 index 00000000..757d507f --- /dev/null +++ b/fixtures/partials-app/source/shared/_header.erb @@ -0,0 +1 @@ +
Header
\ No newline at end of file diff --git a/fixtures/partials-app/source/sub/_local.erb b/fixtures/partials-app/source/sub/_local.erb new file mode 100644 index 00000000..333bf9e4 --- /dev/null +++ b/fixtures/partials-app/source/sub/_local.erb @@ -0,0 +1 @@ +

Local Partial

\ No newline at end of file diff --git a/fixtures/partials-app/source/sub/index.html.erb b/fixtures/partials-app/source/sub/index.html.erb new file mode 100644 index 00000000..5ce2eb35 --- /dev/null +++ b/fixtures/partials-app/source/sub/index.html.erb @@ -0,0 +1,3 @@ +<%= partial "shared/header" %> +<%= partial "local" %> +<%= partial "shared/footer" %> \ No newline at end of file diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb index 273d201f..55125a22 100644 --- a/lib/middleman/base.rb +++ b/lib/middleman/base.rb @@ -381,16 +381,40 @@ class Middleman::Base # @return [String] Output def render(engine, data, options={}, locals={}, &block) data = data.to_s - template_path = File.join(source_dir, data) - if sitemap.exists?(data) - sitemap.page(data).render(options, locals, &block) - elsif File.exists?(template_path) - body = app.cache.fetch(:raw_template, template_path) do - File.read(template_path) - end + found_partial = false + engine = nil + + if sitemap.exists?(current_path) + page = sitemap.page(current_path) + current_dir = File.dirname(page.source_file) + engine = File.extname(page.source_file)[1..-1].to_sym - Middleman::Sitemap::Template.static_render(self, template_path, body, locals, options, &block) + if current_dir != self.source_dir + relative_dir = File.join(current_dir.sub("#{self.source_dir}/", ""), data) + + found_partial, found_engine = Middleman::Sitemap::Template.resolve_template(self, relative_dir, :preferred_engine => engine) + + if !found_partial + found_partial, found_engine = Middleman::Sitemap::Template.resolve_template(self, relative_dir) + end + end + end + + if !found_partial && !engine.nil? + found_partial, found_engine = Middleman::Sitemap::Template.resolve_template(self, data, :preferred_engine => engine) + end + + if !found_partial + found_partial, found_engine = Middleman::Sitemap::Template.resolve_template(self, data) + end + + if found_partial + body = cache.fetch(:raw_template, found_partial) do + File.read(found_partial) + end + + Middleman::Sitemap::Template.static_render(self, found_partial, body, locals, options, &block) else throw "Could not find file to render: #{data}" end diff --git a/lib/middleman/sitemap/template.rb b/lib/middleman/sitemap/template.rb index 7687844f..7e500b12 100644 --- a/lib/middleman/sitemap/template.rb +++ b/lib/middleman/sitemap/template.rb @@ -124,28 +124,28 @@ module Middleman::Sitemap if !preferred_engine.nil? # Check root - layout_path, *etc = resolve_template(name, :preferred_engine => preferred_engine) + layout_path, *etc = self.class.resolve_template(app, name, :preferred_engine => preferred_engine) # Check layouts folder if !layout_path - layout_path, *etc = resolve_template(File.join("layouts", name.to_s), :preferred_engine => preferred_engine) + layout_path, *etc = self.class.resolve_template(app, File.join("layouts", name.to_s), :preferred_engine => preferred_engine) end end # Check root, no preference if !layout_path - layout_path, *etc = resolve_template(name) + layout_path, *etc = self.class.resolve_template(app, name) end # Check layouts folder, no preference if !layout_path - layout_path, *etc = resolve_template(File.join("layouts", name.to_s)) + layout_path, *etc = self.class.resolve_template(app, File.join("layouts", name.to_s)) end layout_path end - def resolve_template(request_path, options={}) + def self.resolve_template(app, request_path, options={}) request_path = request_path.to_s cache.fetch(:resolve_template, request_path, options) do relative_path = request_path.sub(%r{^/}, "") @@ -196,9 +196,9 @@ module Middleman::Sitemap def self.static_render(app, path, body, locs = {}, opts = {}, &block) options = opts.merge(options_for_ext(File.extname(path))) - # template = cache.fetch(:compiled_template, options, body) do - template = ::Tilt.new(path, 1, options) { body } - # end + template = cache.fetch(:compiled_template, options, body) do + ::Tilt.new(path, 1, options) { body } + end template.render(app, locs, &block) end