Merge branch 'v3-stable' into feature/chained_templates_partials

Conflicts:
	middleman-core/lib/middleman-core/core_extensions/rendering.rb
This commit is contained in:
Dennis Günnewig 2014-07-31 13:00:19 +02:00
commit 98a36d68f0
19 changed files with 128 additions and 35 deletions

View file

@ -0,0 +1,23 @@
Feature: i18n Partials
Scenario: Running localize with the default config
Given a fixture app "i18n-test-app"
And a file named "config.rb" with:
"""
activate :i18n
"""
Given the Server is running at "i18n-test-app"
When I go to "/partials/index.html"
Then I should see "Country: USA"
Then I should see "State: District of Columbia"
Then I should see "Greeting: Hello"
Then I should see "Site: Locale Site"
Then I should see "Flag: stars"
Then I should see "President: obama"
When I go to "/es/partials/index.html"
Then I should see "Country: Mexico"
Then I should see "State: Distrito Federal"
Then I should see "Greeting: Hola"
Then I should see "Site: Locale Site"
Then I should see "Flag: bars"
Then I should see "President: nieto"

View file

@ -49,3 +49,9 @@ Feature: Provide Sane Defaults for Partial Behavior
Given the Server is running at "partials-app"
When I go to "/svg.html"
Then I should see "<svg"
When I go to "/static_underscore.html"
Then I should see "<p>Hello World</p>"
When I go to "/code_snippet.html"
Then I should see "File Not Found"
When I go to "/_code_snippet.html"
Then I should see "File Not Found"

View file

@ -0,0 +1 @@
USA

View file

@ -0,0 +1 @@
Mexico

View file

@ -0,0 +1 @@
Locale Site

View file

@ -0,0 +1 @@
obama

View file

@ -0,0 +1 @@
nieto

View file

@ -0,0 +1 @@
District of Columbia

View file

@ -0,0 +1 @@
Distrito Federal

View file

@ -0,0 +1,6 @@
Site: <%= partial :site %>
Country: <%= partial :country %>
Greeting: <%= partial :greeting %>
State: <%= partial :state %>
Flag: <%= partial "images/flag.svg" %>
President: <%= partial "images/president.svg" %>

View file

@ -0,0 +1 @@
<p>Hello World</p>

View file

@ -0,0 +1 @@
<%= partial "code_snippet.html" %>

View file

@ -200,12 +200,33 @@ module Middleman
# @param [Hash] options
# @return [String]
def render(_, data, options={}, &block)
data = data.to_s
partial_name = data.to_s
found_partial = locate_partial(partial_name, false) || locate_partial(partial_name, true)
# Look in the partials_dir for the partial with the current engine
unless found_partial
partials_path = File.join(config[:partials_dir], partial_name)
found_partial = locate_partial(partials_path, false) || locate_partial(partials_path, true)
end
raise ::Middleman::CoreExtensions::Rendering::TemplateNotFound, "Could not locate partial: #{data}" unless found_partial
locals = options[:locals]
found_partial = false
resolve_opts = { try_without_underscore: true, try_static: true }
if ::Tilt[found_partial]
# Render the partial if found, otherwide throw exception
_render_with_all_renderers(found_partial, locals, self, options, &block)
else
File.read(found_partial)
end
end
# Partial locator.
#
# @param [String] partial_name
# @return [String]
def locate_partial(partial_name, try_static=true)
resolve_opts = { try_without_underscore: true, try_static: try_static }
# If the path is known to the sitemap
if resource = sitemap.find_resource_by_path(current_path)
@ -213,26 +234,11 @@ module Middleman
resolve_opts[:preferred_engine] = File.extname(resource.source_file)[1..-1].to_sym
# Look for partials relative to the current path
relative_dir = File.join(current_dir.sub(%r{^#{Regexp.escape(source_dir)}/?}, ''), data)
relative_dir = File.join(current_dir.sub(%r{^#{Regexp.escape(source_dir)}/?}, ''), partial_name)
found_partial = resolve_template(relative_dir, resolve_opts)
end
# Look in the partials_dir for the partial with the current engine
unless found_partial
partials_path = File.join(config[:partials_dir], data)
found_partial = resolve_template(partials_path, resolve_opts)
end
raise ::Middleman::CoreExtensions::Rendering::TemplateNotFound, "Could not locate partial: #{data}" unless found_partial
r = sitemap.find_resource_by_path(sitemap.file_to_path(found_partial))
if r && !r.template?
File.read(r.source_file)
resolve_template(relative_dir, resolve_opts) || resolve_template(partial_name, resolve_opts)
else
# Render the partial if found, otherwide throw exception
_render_with_all_renderers(found_partial, locals, self, options, &block)
resolve_template(partial_name, resolve_opts)
end
end
@ -276,7 +282,7 @@ module Middleman
# Overwrite with frontmatter options
options = options.deep_merge(options[:renderer_options]) if options[:renderer_options]
template_class = Tilt[path]
template_class = ::Tilt[path]
# Allow hooks to manipulate the template before render
self.class.callbacks_for_hook(:before_render).each do |callback|
# Uber::Options::Value doesn't respond to call
@ -460,9 +466,10 @@ module Middleman
relative_path = Util.strip_leading_slash(request_path)
on_disk_path = File.expand_path(relative_path, source_dir)
# By default, any engine will do
preferred_engines = ['*']
preferred_engines << nil if options[:try_static]
preferred_engines = if options[:try_static]
[nil]
else
possible_engines = ['*'] # By default, any engine will do
# If we're specifically looking for a preferred engine
if options.key?(:preferred_engine)
@ -475,10 +482,13 @@ module Middleman
# Prefer to look for the matched extensions
unless matched_exts.empty?
preferred_engines.unshift('{' + matched_exts.join(',') + '}')
possible_engines.unshift('{' + matched_exts.join(',') + '}')
end
end
possible_engines
end
search_paths = preferred_engines.flat_map do |preferred_engine|
path_with_ext = on_disk_path.dup
path_with_ext << ('.' + preferred_engine) unless preferred_engine.nil?

View file

@ -95,6 +95,14 @@ Then /^I should not see "([^\"]*)"$/ do |expected|
expect(@browser.last_response.body).to_not include(expected)
end
Then /^I should not see:$/ do |expected|
expect(@browser.last_response.body).to_not include(expected.chomp)
end
Then /^the status code should be "([^\"]*)"$/ do |expected|
expect(@browser.last_response.status).to eq expected.to_i
end
Then /^I should see "([^\"]*)" lines$/ do |lines|
expect(@browser.last_response.body.chomp.split($/).length).to eq(lines.to_i)
end

View file

@ -207,5 +207,32 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
def langs
extensions[:i18n].langs
end
def locate_partial(partial_name, try_static=false)
locals_dir = extensions[:i18n].options[:templates_dir]
# Try /localizable
partials_path = File.join(locals_dir, partial_name)
lang_suffix = current_resource.metadata[:locals] && current_resource.metadata[:locals][:lang]
extname = File.extname(partial_name)
maybe_static = extname.length > 0
suffixed_partial_name = if maybe_static
partial_name.sub(extname, ".#{lang_suffix}#{extname}")
else
"#{partial_name}.#{lang_suffix}"
end
if lang_suffix
super(suffixed_partial_name, maybe_static) ||
super(File.join(locals_dir, suffixed_partial_name), maybe_static) ||
super(partials_path, try_static) ||
super
else
super(partials_path, try_static) ||
super
end
end
end
end