Tweak locale links

v3-stable
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"
And a file named "data/pages.yml" with:
"""
@ -46,4 +46,46 @@ Feature: i18n Links
Then I should see '<a class="current" href="/es/hola.html">Current 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 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

@ -73,7 +73,7 @@ module Middleman
# Which server name should be used
# @return [NilClass, String]
config.define_setting :host, nil , 'The preview host name'
config.define_setting :host, nil, 'The preview host name'
# Whether to serve the preview server over HTTPS.
# @return [Boolean]

View File

@ -34,6 +34,6 @@ module Middleman::Cli
# Map "u" to "upgrade"
Base.map(
'u' => 'upgrade'
'u' => 'upgrade'
)
end

View File

@ -64,8 +64,8 @@ module Middleman::Cli
# Map "i", "new" and "n" to "init"
Base.map(
'i' => 'init',
'new' => 'init',
'n' => 'init'
'i' => 'init',
'new' => 'init',
'n' => 'init'
)
end

View File

@ -101,7 +101,7 @@ module Middleman::CoreExtensions
data, content = frontmatter_and_content(p)
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)
end

View File

@ -422,7 +422,8 @@ module Middleman
engine = extension[1..-1].to_sym
# 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
content = if block_given?

View File

@ -173,8 +173,8 @@ module Middleman
def current_path=(path)
Thread.current[:current_path] = path
Thread.current[:legacy_request] = ::Thor::CoreExt::HashWithIndifferentAccess.new(
path: path,
params: req ? ::Thor::CoreExt::HashWithIndifferentAccess.new(req.params) : {}
path: path,
params: req ? ::Thor::CoreExt::HashWithIndifferentAccess.new(req.params) : {}
)
end

View File

@ -198,12 +198,12 @@ module Middleman
http_opts[:SSLEnable] = true
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[:SSLPrivateKey] = OpenSSL::PKey::RSA.new File.read ssl_private_key
else
# 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[:SSLPrivateKey] = key
end
@ -251,24 +251,24 @@ module Middleman
cert.subject = name
cert.issuer = name
cert.not_before = Time.now
cert.not_after = Time.now + (365*24*60*60)
cert.not_after = Time.now + (365 * 24 * 60 * 60)
cert.public_key = rsa.public_key
ef = OpenSSL::X509::ExtensionFactory.new(nil,cert)
ef = OpenSSL::X509::ExtensionFactory.new(nil, cert)
ef.issuer_certificate = cert
cert.extensions = [
ef.create_extension("basicConstraints","CA:FALSE"),
ef.create_extension("keyUsage", "keyEncipherment"),
ef.create_extension("subjectKeyIdentifier", "hash"),
ef.create_extension("extendedKeyUsage", "serverAuth"),
ef.create_extension("nsComment", comment),
]
aki = ef.create_extension("authorityKeyIdentifier",
"keyid:always,issuer:always")
ef.create_extension('basicConstraints', 'CA:FALSE'),
ef.create_extension('keyUsage', 'keyEncipherment'),
ef.create_extension('subjectKeyIdentifier', 'hash'),
ef.create_extension('extendedKeyUsage', 'serverAuth'),
ef.create_extension('nsComment', comment)
]
aki = ef.create_extension('authorityKeyIdentifier',
'keyid:always,issuer:always')
cert.add_extension(aki)
cert.sign(rsa, OpenSSL::Digest::SHA1.new)
return [ cert, rsa ]
[cert, rsa]
end
# Attach a new Middleman::Application instance

View File

@ -28,16 +28,16 @@ module Middleman
root_dotfiles: proc { |file| file.start_with?('.') },
# Files starting with an dot, but not .htaccess
source_dotfiles: proc { |file|
source_dotfiles: proc do |file|
file =~ %r{/\.} && file !~ %r{/\.(htaccess|htpasswd|nojekyll)}
},
end,
# Files starting with an underscore, but not a double-underscore
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/'))
}
end
}, 'Callbacks that can exclude paths from the sitemap'
# Include instance methods

View File

@ -80,7 +80,7 @@ module Middleman
url = ::Middleman::Util.url_for(store.app, @request_path,
relative: false,
find_resource: true
)
)
if output
output.call(path, url)

View File

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

View File

@ -231,10 +231,10 @@ module Middleman
private
def reset_lookup_cache!
@lock.synchronize {
@lock.synchronize do
@_lookup_by_path = {}
@_lookup_by_destination_path = {}
}
end
end
# 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
# rubocop:disable UnderscorePrefixedVariableName
def capture_from_template(*args, &block)
self.output_buffer, _buf_was = '', output_buffer
self.output_buffer = ''
_buf_was = output_buffer
raw = block.call(*args)
captured = template.instance_variable_get(:@_out_buf)
self.output_buffer = _buf_was
@ -255,12 +256,12 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension
url = url_for(url, options)
super
end
# Modified Padrino image_tag so that it finds the paths for srcset
# using asset_path for the images listed in the srcset param
def image_tag(path, params={})
params.symbolize_keys!
if params.key?(:srcset)
images_sources = params[:srcset].split(',').map do |src_def|
if src_def.include?('//')

View File

@ -58,20 +58,11 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
::I18n.t(*args)
end
def link_to(*args, &block)
options = args.extract_options!
name = block_given? ? '' : args.shift
href = args.first
def url_for(path_or_resource, options={})
locale = options.delete(:locale) || ::I18n.locale
url = extensions[:i18n].localized_path(href, locale)
new_args = []
new_args << name unless block_given?
new_args << url || href
new_args << options
super(*new_args, &block)
href = super(path_or_resource, options)
extensions[:i18n].localized_path(href, locale) || href
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)
end
known_langs.map { |p|
known_langs.map do |p|
File.basename(p.to_s).sub(/\.ya?ml$/, '').sub(/\.rb$/, '')
}.sort.map(&:to_sym)
end.sort.map(&:to_sym)
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}$/)
if retina
factor = retina[1].to_i
width = width / factor
height = height / factor
width /= factor
height /= factor
end
params[:width] = width
params[:height] = height

View File

@ -103,7 +103,9 @@ class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension
# @return [String]
def minify_inline(content)
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
# something like jQuery templates, identified with a "text/html" type).