Tweak locale links

This commit is contained in:
Thomas Reynolds 2015-05-16 13:21:12 -07:00
parent 6502b37934
commit d3c7436647
16 changed files with 91 additions and 53 deletions

View file

@ -1,6 +1,6 @@
Feature: i18n Links Feature: i18n Paths
Scenario: A template changes i18n during preview Scenario: link_to is i18n aware
Given a fixture app "empty-app" Given a fixture app "empty-app"
And a file named "data/pages.yml" with: And a file named "data/pages.yml" with:
""" """
@ -47,3 +47,45 @@ Feature: i18n Links
Then I should see '<a title="Other hello.html" href="/hello.html">Other hello.html</a>' Then I should see '<a title="Other hello.html" href="/hello.html">Other hello.html</a>'
Then I should see '<a class="current" href="/es/hola.html"><span>Current Block</span></a>' Then I should see '<a class="current" href="/es/hola.html"><span>Current Block</span></a>'
Then I should see '<a title="Other hello.html" href="/hello.html"><span>Other Block</span></a>' Then I should see '<a title="Other hello.html" href="/hello.html"><span>Other Block</span></a>'
Scenario: url_for is i18n aware
Given a fixture app "empty-app"
And a file named "data/pages.yml" with:
"""
- hello.html
"""
And a file named "locales/en.yml" with:
"""
---
en:
msg: Hello
"""
And a file named "locales/es.yml" with:
"""
---
es:
paths:
hello: "hola"
msg: Hola
"""
And a file named "source/localizable/hello.html.erb" with:
"""
Page: <%= t(:msg) %>
<% data.pages.each_with_index do |p, i| %>
Current: <%= url_for "/#{p}" %>
Other: <%= url_for "/#{p}", locale: ::I18n.locale == :en ? :es : :en %>
<% end %>
"""
And a file named "config.rb" with:
"""
activate :i18n
"""
Given the Server is running at "empty-app"
When I go to "/hello.html"
Then I should see "Page: Hello"
Then I should see 'Current: /hello.html'
Then I should see 'Other: /es/hola.html'
When I go to "/es/hola.html"
Then I should see "Page: Hola"
Then I should see 'Current: /es/hola.html'
Then I should see 'Other: /hello.html'

View file

@ -101,7 +101,7 @@ module Middleman::CoreExtensions
data, content = frontmatter_and_content(p) data, content = frontmatter_and_content(p)
if app.files.exists?("#{path}.frontmatter") if app.files.exists?("#{path}.frontmatter")
external_data, _ = frontmatter_and_content("#{p}.frontmatter") external_data, = frontmatter_and_content("#{p}.frontmatter")
data = external_data.deep_merge(data) data = external_data.deep_merge(data)
end end

View file

@ -422,7 +422,8 @@ module Middleman
engine = extension[1..-1].to_sym engine = extension[1..-1].to_sym
# Store last engine for later (could be inside nested renders) # Store last engine for later (could be inside nested renders)
self.current_engine, engine_was = engine, current_engine self.current_engine = engine
engine_was = current_engine
begin begin
content = if block_given? content = if block_given?

View file

@ -198,12 +198,12 @@ module Middleman
http_opts[:SSLEnable] = true http_opts[:SSLEnable] = true
if ssl_certificate || ssl_private_key if ssl_certificate || ssl_private_key
raise "You must provide both :ssl_certificate and :ssl_private_key" unless ssl_private_key && ssl_certificate raise 'You must provide both :ssl_certificate and :ssl_private_key' unless ssl_private_key && ssl_certificate
http_opts[:SSLCertificate] = OpenSSL::X509::Certificate.new File.read ssl_certificate http_opts[:SSLCertificate] = OpenSSL::X509::Certificate.new File.read ssl_certificate
http_opts[:SSLPrivateKey] = OpenSSL::PKey::RSA.new File.read ssl_private_key http_opts[:SSLPrivateKey] = OpenSSL::PKey::RSA.new File.read ssl_private_key
else else
# use a generated self-signed cert # use a generated self-signed cert
cert, key = create_self_signed_cert(1024, [["CN", host]], "Middleman Preview Server") cert, key = create_self_signed_cert(1024, [['CN', host]], 'Middleman Preview Server')
http_opts[:SSLCertificate] = cert http_opts[:SSLCertificate] = cert
http_opts[:SSLPrivateKey] = key http_opts[:SSLPrivateKey] = key
end end
@ -257,18 +257,18 @@ module Middleman
ef = OpenSSL::X509::ExtensionFactory.new(nil, cert) ef = OpenSSL::X509::ExtensionFactory.new(nil, cert)
ef.issuer_certificate = cert ef.issuer_certificate = cert
cert.extensions = [ cert.extensions = [
ef.create_extension("basicConstraints","CA:FALSE"), ef.create_extension('basicConstraints', 'CA:FALSE'),
ef.create_extension("keyUsage", "keyEncipherment"), ef.create_extension('keyUsage', 'keyEncipherment'),
ef.create_extension("subjectKeyIdentifier", "hash"), ef.create_extension('subjectKeyIdentifier', 'hash'),
ef.create_extension("extendedKeyUsage", "serverAuth"), ef.create_extension('extendedKeyUsage', 'serverAuth'),
ef.create_extension("nsComment", comment), ef.create_extension('nsComment', comment)
] ]
aki = ef.create_extension("authorityKeyIdentifier", aki = ef.create_extension('authorityKeyIdentifier',
"keyid:always,issuer:always") 'keyid:always,issuer:always')
cert.add_extension(aki) cert.add_extension(aki)
cert.sign(rsa, OpenSSL::Digest::SHA1.new) cert.sign(rsa, OpenSSL::Digest::SHA1.new)
return [ cert, rsa ] [cert, rsa]
end end
# Attach a new Middleman::Application instance # Attach a new Middleman::Application instance

View file

@ -28,16 +28,16 @@ module Middleman
root_dotfiles: proc { |file| file.start_with?('.') }, root_dotfiles: proc { |file| file.start_with?('.') },
# Files starting with an dot, but not .htaccess # Files starting with an dot, but not .htaccess
source_dotfiles: proc { |file| source_dotfiles: proc do |file|
file =~ %r{/\.} && file !~ %r{/\.(htaccess|htpasswd|nojekyll)} file =~ %r{/\.} && file !~ %r{/\.(htaccess|htpasswd|nojekyll)}
}, end,
# Files starting with an underscore, but not a double-underscore # Files starting with an underscore, but not a double-underscore
partials: proc { |file| file =~ %r{/_[^_]} }, partials: proc { |file| file =~ %r{/_[^_]} },
layout: proc { |file, sitemap_app| layout: proc do |file, sitemap_app|
file.start_with?(File.join(sitemap_app.config[:source], 'layout.')) || file.start_with?(File.join(sitemap_app.config[:source], 'layouts/')) file.start_with?(File.join(sitemap_app.config[:source], 'layout.')) || file.start_with?(File.join(sitemap_app.config[:source], 'layouts/'))
} end
}, 'Callbacks that can exclude paths from the sitemap' }, 'Callbacks that can exclude paths from the sitemap'
# Include instance methods # Include instance methods

View file

@ -125,7 +125,8 @@ module Middleman
unless VALID_OPERATORS.include? opts[:operator] unless VALID_OPERATORS.include? opts[:operator]
raise OperatorNotSupportedError raise OperatorNotSupportedError
end end
@attribute, @operator = opts[:attribute], opts[:operator] @attribute = opts[:attribute]
@operator = opts[:operator]
end end
end end
end end

View file

@ -231,10 +231,10 @@ module Middleman
private private
def reset_lookup_cache! def reset_lookup_cache!
@lock.synchronize { @lock.synchronize do
@_lookup_by_path = {} @_lookup_by_path = {}
@_lookup_by_destination_path = {} @_lookup_by_destination_path = {}
} end
end end
# Removes the templating extensions, while keeping the others # Removes the templating extensions, while keeping the others

View file

@ -8,7 +8,8 @@ class Padrino::Helpers::OutputHelpers::ErbHandler
# Force Erb capture not to use safebuffer # Force Erb capture not to use safebuffer
# rubocop:disable UnderscorePrefixedVariableName # rubocop:disable UnderscorePrefixedVariableName
def capture_from_template(*args, &block) def capture_from_template(*args, &block)
self.output_buffer, _buf_was = '', output_buffer self.output_buffer = ''
_buf_was = output_buffer
raw = block.call(*args) raw = block.call(*args)
captured = template.instance_variable_get(:@_out_buf) captured = template.instance_variable_get(:@_out_buf)
self.output_buffer = _buf_was self.output_buffer = _buf_was

View file

@ -58,20 +58,11 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
::I18n.t(*args) ::I18n.t(*args)
end end
def link_to(*args, &block) def url_for(path_or_resource, options={})
options = args.extract_options!
name = block_given? ? '' : args.shift
href = args.first
locale = options.delete(:locale) || ::I18n.locale locale = options.delete(:locale) || ::I18n.locale
url = extensions[:i18n].localized_path(href, locale) href = super(path_or_resource, options)
new_args = [] extensions[:i18n].localized_path(href, locale) || href
new_args << name unless block_given?
new_args << url || href
new_args << options
super(*new_args, &block)
end end
end end
@ -177,9 +168,9 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
p.to_s.match(@locales_regex) && (p.to_s.split(File::SEPARATOR).length == 2) p.to_s.match(@locales_regex) && (p.to_s.split(File::SEPARATOR).length == 2)
end end
known_langs.map { |p| known_langs.map do |p|
File.basename(p.to_s).sub(/\.ya?ml$/, '').sub(/\.rb$/, '') File.basename(p.to_s).sub(/\.ya?ml$/, '').sub(/\.rb$/, '')
}.sort.map(&:to_sym) end.sort.map(&:to_sym)
end end
end end

View file

@ -27,8 +27,8 @@ class Middleman::Extensions::AutomaticImageSizes < ::Middleman::Extension
retina = full_path.match(/@(\d)x\.[a-zA-Z]{3,4}$/) retina = full_path.match(/@(\d)x\.[a-zA-Z]{3,4}$/)
if retina if retina
factor = retina[1].to_i factor = retina[1].to_i
width = width / factor width /= factor
height = height / factor height /= factor
end end
params[:width] = width params[:width] = width
params[:height] = height params[:height] = height

View file

@ -103,7 +103,9 @@ class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension
# @return [String] # @return [String]
def minify_inline(content) def minify_inline(content)
content.gsub(INLINE_JS_REGEX) do |match| content.gsub(INLINE_JS_REGEX) do |match|
first, inline_content, last = $1, $2, $3 first = $1
inline_content = $2
last = $3
# Only compress script tags that contain JavaScript (as opposed to # Only compress script tags that contain JavaScript (as opposed to
# something like jQuery templates, identified with a "text/html" type). # something like jQuery templates, identified with a "text/html" type).