From 62dba443a0f2d2bcc1abf50b20a71c47ac6e2bc0 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sun, 10 Jul 2016 17:03:22 -0700 Subject: [PATCH] Fix japanese characters encoding in URLs --- CHANGELOG.md | 5 +++ .../extensions/relative_assets.rb | 6 +-- .../sitemap/extensions/redirects.rb | 3 +- .../lib/middleman-core/template_context.rb | 22 +++++---- .../lib/middleman-core/util/binary.rb | 9 ++-- .../lib/middleman-core/util/data.rb | 2 +- .../lib/middleman-core/util/paths.rb | 45 +++++++++++-------- middleman-core/lib/middleman-core/version.rb | 2 +- 8 files changed, 50 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b25a9816..a6494a17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ master === +# 4.1.10 + +* Fix unicode issues in URL deeplinks. +* Add prefix option to asset_hash (#1949) + # 4.1.9 * Fix `--watcher-*` CLI flags. diff --git a/middleman-core/lib/middleman-core/extensions/relative_assets.rb b/middleman-core/lib/middleman-core/extensions/relative_assets.rb index 853a7005..432072c3 100644 --- a/middleman-core/lib/middleman-core/extensions/relative_assets.rb +++ b/middleman-core/lib/middleman-core/extensions/relative_assets.rb @@ -11,9 +11,7 @@ class Middleman::Extensions::RelativeAssets < ::Middleman::Extension def initialize(app, options_hash={}, &block) super - if options[:helpers_only] - return - end + return if options[:helpers_only] app.rewrite_inline_urls id: :relative_assets, url_extensions: options.exts || app.config[:asset_extensions], @@ -53,7 +51,7 @@ class Middleman::Extensions::RelativeAssets < ::Middleman::Extension end def asset_path(kind, source, options={}) - super(kind, source, app.extensions[:relative_assets].mark_as_relative(super, options, current_resource)) + super(kind, source, app.extensions[:relative_assets].mark_as_relative(super, options, current_resource)) end end diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb b/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb index 48ede6a0..3ff15718 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb @@ -53,8 +53,7 @@ module Middleman def render(*) url = ::Middleman::Util.url_for(@store.app, @request_path, relative: false, - find_resource: true - ) + find_resource: true) if output output.call(path, url) diff --git a/middleman-core/lib/middleman-core/template_context.rb b/middleman-core/lib/middleman-core/template_context.rb index defb063f..5efd8c41 100644 --- a/middleman-core/lib/middleman-core/template_context.rb +++ b/middleman-core/lib/middleman-core/template_context.rb @@ -138,18 +138,16 @@ module Middleman relative_dir_no_underscore = current_dir + Pathname(non_root_no_underscore) end - - lookup_stack.push [ relative_dir.to_s, - { preferred_engine: resource.file_descriptor[:relative_path] - .extname[1..-1].to_sym }] if relative_dir - lookup_stack.push [ non_root ] - lookup_stack.push [ non_root, - { try_static: try_static }] - lookup_stack.push [ relative_dir_no_underscore.to_s, - { try_static: try_static }] if relative_dir_no_underscore - lookup_stack.push [ non_root_no_underscore, - { try_static: try_static }] - + lookup_stack.push [relative_dir.to_s, + { preferred_engine: resource.file_descriptor[:relative_path] + .extname[1..-1].to_sym }] if relative_dir + lookup_stack.push [non_root] + lookup_stack.push [non_root, + { try_static: try_static }] + lookup_stack.push [relative_dir_no_underscore.to_s, + { try_static: try_static }] if relative_dir_no_underscore + lookup_stack.push [non_root_no_underscore, + { try_static: try_static }] lookup_stack.each do |args| partial_file = ::Middleman::TemplateRenderer.resolve_template(@app, *args) diff --git a/middleman-core/lib/middleman-core/util/binary.rb b/middleman-core/lib/middleman-core/util/binary.rb index 71e584d1..be131965 100644 --- a/middleman-core/lib/middleman-core/util/binary.rb +++ b/middleman-core/lib/middleman-core/util/binary.rb @@ -47,14 +47,13 @@ module Middleman # @return [Boolean] Contract String => Bool def nonbinary_mime?(mime) - case - when mime.start_with?('text/') + if mime.start_with?('text/') true - when mime.include?('xml') && !mime.include?('officedocument') + elsif mime.include?('xml') && !mime.include?('officedocument') true - when mime.include?('json') + elsif mime.include?('json') true - when mime.include?('javascript') + elsif mime.include?('javascript') true else false diff --git a/middleman-core/lib/middleman-core/util/data.rb b/middleman-core/lib/middleman-core/util/data.rb index ce73540a..b0026293 100644 --- a/middleman-core/lib/middleman-core/util/data.rb +++ b/middleman-core/lib/middleman-core/util/data.rb @@ -94,7 +94,7 @@ module Middleman .transpose .map(&::Regexp.method(:union)) - match = / + / \A(?:[^\r\n]*coding:[^\r\n]*\r?\n)? (?#{start_delims})[ ]*\r?\n (?.*?)[ ]*\r?\n? diff --git a/middleman-core/lib/middleman-core/util/paths.rb b/middleman-core/lib/middleman-core/util/paths.rb index a9b3f2c3..2814cfc9 100644 --- a/middleman-core/lib/middleman-core/util/paths.rb +++ b/middleman-core/lib/middleman-core/util/paths.rb @@ -1,8 +1,8 @@ # Core Pathname library used for traversal require 'pathname' require 'uri' -require 'memoist' require 'addressable' +require 'memoist' require 'tilt' require 'middleman-core/contracts' @@ -32,9 +32,9 @@ module Middleman # @return [String] Contract String => String def normalize_path(path) - # The tr call works around a bug in Ruby's Unicode handling + # The tr call works around a bug in Ruby's Unicode handling ::URI.decode(path).sub(%r{^/}, '').tr('', '') - end + end memoize :normalize_path # This is a separate method from normalize_path in case we @@ -111,7 +111,7 @@ module Middleman raise ArgumentError, '#asset_url must be run in a context with current_resource if relative: true' end - uri = URI(path) + uri = ::Middleman::Util.parse_uri(path) path = uri.path # Ensure the url we pass into find_resource_by_destination_path is not a @@ -131,9 +131,15 @@ module Middleman end end - final_result = ::URI.encode(relative_path_from_resource(options[:current_resource], result, options[:relative])) + final_result = ::Addressable::URI.encode( + relative_path_from_resource( + options[:current_resource], + result, + options[:relative] + ) + ) - result_uri = URI(final_result) + result_uri = ::Middleman::Util.parse_uri(final_result) result_uri.query = uri.query result_uri.fragment = uri.fragment result_uri.to_s @@ -158,14 +164,10 @@ module Middleman # Try to parse URL begin - uri = URI(url) - rescue ::URI::InvalidURIError - begin - uri = URI(::URI.encode(url)) - rescue ::URI::InvalidURIError - # Nothing we can do with it, it's not really a URI - return url - end + uri = ::Middleman::Util.parse_uri(url) + rescue ::Addressable::URI::InvalidURIError + # Nothing we can do with it, it's not really a URI + return url end relative = options[:relative] @@ -206,7 +208,13 @@ module Middleman if resource uri.path = if this_resource - ::URI.encode(relative_path_from_resource(this_resource, resource_url, effective_relative)) + ::Addressable::URI.encode( + relative_path_from_resource( + this_resource, + resource_url, + effective_relative + ) + ) else resource_url end @@ -284,16 +292,15 @@ module Middleman # @return [Boolean] Whether the path matches the matcher Contract PATH_MATCHER, String => Bool def path_match(matcher, path) - case - when matcher.is_a?(String) + if matcher.is_a?(String) if matcher.include? '*' ::File.fnmatch(matcher, path) else path == matcher end - when matcher.respond_to?(:match) + elsif matcher.respond_to?(:match) !!(path =~ matcher) - when matcher.respond_to?(:call) + elsif matcher.respond_to?(:call) matcher.call(path) else ::File.fnmatch(matcher.to_s, path) diff --git a/middleman-core/lib/middleman-core/version.rb b/middleman-core/lib/middleman-core/version.rb index 280b2eca..6f3f1228 100644 --- a/middleman-core/lib/middleman-core/version.rb +++ b/middleman-core/lib/middleman-core/version.rb @@ -1,5 +1,5 @@ module Middleman # Current Version # @return [String] - VERSION = '4.1.9'.freeze unless const_defined?(:VERSION) + VERSION = '4.1.10'.freeze unless const_defined?(:VERSION) end