From de7e62176600dafcc54c69d3b18e76e1ee8988e1 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Fri, 8 Feb 2013 00:05:34 -0800 Subject: [PATCH 1/8] Revert the more-flexible Thor dependency since it breaks bundle install for JRuby and we will need to update Padrino when it gets upgraded anyway --- middleman-core/middleman-core-x86-mingw32.gemspec | 2 +- middleman-core/middleman-core.gemspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/middleman-core/middleman-core-x86-mingw32.gemspec b/middleman-core/middleman-core-x86-mingw32.gemspec index 1ec32454..c91e80c9 100644 --- a/middleman-core/middleman-core-x86-mingw32.gemspec +++ b/middleman-core/middleman-core-x86-mingw32.gemspec @@ -27,7 +27,7 @@ Gem::Specification.new do |s| s.add_dependency("rack-test", ["~> 0.6.1"]) # CLI - s.add_dependency("thor", [">= 0.15.4", "~> 0.15"]) + s.add_dependency("thor", [">~ 0.15.4"]) # Helpers s.add_dependency("activesupport", ["~> 3.2.6"]) diff --git a/middleman-core/middleman-core.gemspec b/middleman-core/middleman-core.gemspec index 4eb1aed0..f268f2f5 100644 --- a/middleman-core/middleman-core.gemspec +++ b/middleman-core/middleman-core.gemspec @@ -26,7 +26,7 @@ Gem::Specification.new do |s| s.add_dependency("rack-test", ["~> 0.6.1"]) # CLI - s.add_dependency("thor", [">= 0.15.4", "~> 0.15"]) + s.add_dependency("thor", [">~ 0.15.4"]) # Helpers s.add_dependency("activesupport", ["~> 3.2.6"]) From 1af96fe305d95930f3728b1091d8cae45373e130 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 9 Feb 2013 10:28:55 -0800 Subject: [PATCH 2/8] Fix gemspecs --- middleman-core/middleman-core-x86-mingw32.gemspec | 2 +- middleman-core/middleman-core.gemspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/middleman-core/middleman-core-x86-mingw32.gemspec b/middleman-core/middleman-core-x86-mingw32.gemspec index c91e80c9..d1779828 100644 --- a/middleman-core/middleman-core-x86-mingw32.gemspec +++ b/middleman-core/middleman-core-x86-mingw32.gemspec @@ -27,7 +27,7 @@ Gem::Specification.new do |s| s.add_dependency("rack-test", ["~> 0.6.1"]) # CLI - s.add_dependency("thor", [">~ 0.15.4"]) + s.add_dependency("thor", ["~> 0.15.4"]) # Helpers s.add_dependency("activesupport", ["~> 3.2.6"]) diff --git a/middleman-core/middleman-core.gemspec b/middleman-core/middleman-core.gemspec index f268f2f5..c366daa3 100644 --- a/middleman-core/middleman-core.gemspec +++ b/middleman-core/middleman-core.gemspec @@ -26,7 +26,7 @@ Gem::Specification.new do |s| s.add_dependency("rack-test", ["~> 0.6.1"]) # CLI - s.add_dependency("thor", [">~ 0.15.4"]) + s.add_dependency("thor", ["~> 0.15.4"]) # Helpers s.add_dependency("activesupport", ["~> 3.2.6"]) From 7020bdce1a0db4152e19b7284c9e0e3fe1df7555 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 9 Feb 2013 12:44:37 -0800 Subject: [PATCH 3/8] Failing test for #584 "i18n fails when default locale is not english" --- middleman-more/features/i18n_preview.feature | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/middleman-more/features/i18n_preview.feature b/middleman-more/features/i18n_preview.feature index 81360e23..bfc591ef 100644 --- a/middleman-more/features/i18n_preview.feature +++ b/middleman-more/features/i18n_preview.feature @@ -103,6 +103,51 @@ Feature: i18n Preview Then I should see "Como Esta?" When I go to "/spanish/hola.html" Then I should see "Hola World" + + Scenario: Running localize with a non-English mount config + Given a fixture app "i18n-test-app" + And a file named "config.rb" with: + """ + activate :i18n, :mount_at_root => :es + """ + Given the Server is running at "i18n-test-app" + When I go to "/en/index.html" + Then I should see "Howdy" + When I go to "/en/hello.html" + Then I should see "Hello World" + When I go to "/" + Then I should see "Como Esta?" + When I go to "/hola.html" + Then I should see "Hola World" + When I go to "/hello.html" + Then I should see "File Not Found" + When I go to "/es/index.html" + Then I should see "File Not Found" + When I go to "/es/hola.html" + Then I should see "File Not Found" + + Scenario: Running localize with a non-English lang subset + Given a fixture app "i18n-test-app" + And a file named "config.rb" with: + """ + activate :i18n, :langs => :es + """ + Given the Server is running at "i18n-test-app" + When I go to "/en/index.html" + Then I should see "File Not Found" + When I go to "/en/hello.html" + Then I should see "File Not Found" + When I go to "/" + Then I should see "Como Esta?" + When I go to "/hola.html" + Then I should see "Hola World" + When I go to "/hello.html" + Then I should see "File Not Found" + When I go to "/es/index.html" + Then I should see "File Not Found" + When I go to "/es/hola.html" + Then I should see "File Not Found" + Scenario: Running localize with the no mount config Given a fixture app "i18n-test-app" From afc62d5a26cf362751edbdff040288ad203283d7 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 9 Feb 2013 12:34:30 -0800 Subject: [PATCH 4/8] i18n tweaks --- middleman-more/lib/middleman-more/core_extensions/i18n.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/middleman-more/lib/middleman-more/core_extensions/i18n.rb b/middleman-more/lib/middleman-more/core_extensions/i18n.rb index 534f4ad8..4f84b2b8 100644 --- a/middleman-more/lib/middleman-more/core_extensions/i18n.rb +++ b/middleman-more/lib/middleman-more/core_extensions/i18n.rb @@ -43,7 +43,7 @@ module Middleman @mount_at_root = @options.has_key?(:mount_at_root) ? @options[:mount_at_root] : langs.first if !@app.build? - logger.info "== Locales: #{langs.join(", ")}" + logger.info "== Locales: #{langs.join(", ")} (Default #{@mount_at_root})" end # Don't output localizable files @@ -79,7 +79,9 @@ module Middleman end def langs - @options[:langs] || begin + if @options[:langs] + Array(@options[:langs]).map(&:to_sym) + else Dir[File.join(@app.root, @locales_glob)].map { |file| File.basename(file).sub(/\.yml$/, "").sub(/\.rb$/, "") }.sort.map(&:to_sym) From fbc4d7e9c7ded08266bcc192efe5f58ad6c10ba6 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 9 Feb 2013 12:40:43 -0800 Subject: [PATCH 5/8] Handle non-english default languages. Fixes #584 --- .../middleman-more/core_extensions/i18n.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/middleman-more/lib/middleman-more/core_extensions/i18n.rb b/middleman-more/lib/middleman-more/core_extensions/i18n.rb index 4f84b2b8..57eee442 100644 --- a/middleman-more/lib/middleman-more/core_extensions/i18n.rb +++ b/middleman-more/lib/middleman-more/core_extensions/i18n.rb @@ -52,15 +52,20 @@ module Middleman @app.sitemap.provides_metadata_for_path do |url| if d = get_localization_data(url) lang, page_id = d - instance_vars = Proc.new { - ::I18n.locale = lang - @lang = lang - @page_id = page_id - } - { :blocks => [instance_vars] } else - {} + # Default to the @mount_at_root lang + page_id = nil + lang = @mount_at_root end + + instance_vars = Proc.new do + ::I18n.locale = lang + @lang = lang + @page_id = page_id + end + + locals = { :lang => lang, :page_id => page_id } + { :blocks => [instance_vars], :locals => locals } end @app.sitemap.register_resource_list_manipulator( From 0f2dc821381000462419c46e848669e4a4e8d41b Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 9 Feb 2013 15:12:35 -0800 Subject: [PATCH 6/8] Add vendor.yml to exclude our fixtures from GitHub language stats --- vendor.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 vendor.yml diff --git a/vendor.yml b/vendor.yml new file mode 100644 index 00000000..417247e2 --- /dev/null +++ b/vendor.yml @@ -0,0 +1,2 @@ +- fixtures +- templates From ed119bed7ba118e7404ee5385f7c9890d2f9cf52 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 9 Feb 2013 10:15:25 -0800 Subject: [PATCH 7/8] Support .yaml files for i18n --- middleman-more/lib/middleman-more/core_extensions/i18n.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/middleman-more/lib/middleman-more/core_extensions/i18n.rb b/middleman-more/lib/middleman-more/core_extensions/i18n.rb index 57eee442..e4d5c696 100644 --- a/middleman-more/lib/middleman-more/core_extensions/i18n.rb +++ b/middleman-more/lib/middleman-more/core_extensions/i18n.rb @@ -26,9 +26,9 @@ module Middleman def initialize(app, options={}) @app = app - @locales_glob = File.join(app.locales_dir, "**", "*.{rb,yml}") + @locales_glob = File.join(app.locales_dir, "**", "*.{rb,yml,yaml}") - regex = @locales_glob.sub(/\./, '\.').sub(File.join("**", "*"), ".*").sub(/\//, '\/').sub("{rb,yml}", "rb|yml") + regex = @locales_glob.sub(/\./, '\.').sub(File.join("**", "*"), ".*").sub(/\//, '\/').sub("{rb,yml,yaml}", "rb|ya?ml") @locales_regex = %r{^#{regex}} @maps = {} @@ -88,7 +88,7 @@ module Middleman Array(@options[:langs]).map(&:to_sym) else Dir[File.join(@app.root, @locales_glob)].map { |file| - File.basename(file).sub(/\.yml$/, "").sub(/\.rb$/, "") + File.basename(file).sub(/\.ya?ml$/, "").sub(/\.rb$/, "") }.sort.map(&:to_sym) end end From 923697b5178a5ddf456327188a6361f82a029802 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 9 Feb 2013 00:20:37 -0800 Subject: [PATCH 8/8] Micro-optimizations around string comparisons --- .../lib/middleman-core/cli/build.rb | 2 +- .../middleman-core/core_extensions/data.rb | 4 ++-- .../core_extensions/file_watcher.rb | 3 ++- .../core_extensions/rendering.rb | 2 +- .../lib/middleman-core/preview_server.rb | 2 +- middleman-core/lib/middleman-core/sitemap.rb | 8 ++++---- .../lib/middleman-core/sitemap/store.rb | 6 +++--- middleman-core/lib/middleman-core/util.rb | 8 +++++++- .../core_extensions/default_helpers.rb | 19 +++++++++---------- .../middleman-more/core_extensions/i18n.rb | 3 ++- .../extensions/automatic_image_sizes.rb | 2 +- 11 files changed, 33 insertions(+), 26 deletions(-) diff --git a/middleman-core/lib/middleman-core/cli/build.rb b/middleman-core/lib/middleman-core/cli/build.rb index 0e08dbdc..26680123 100644 --- a/middleman-core/lib/middleman-core/cli/build.rb +++ b/middleman-core/lib/middleman-core/cli/build.rb @@ -233,7 +233,7 @@ module Middleman::Cli paths = ::Middleman::Util.all_files_under(@destination) @cleaning_queue += paths.select do |path| - !path.to_s.match(/\/\./) || path.to_s.match(/\.htaccess|\.htpasswd/) + path.to_s !~ /\/\./ || path.to_s =~ /\.(htaccess|htpasswd)/ end end diff --git a/middleman-core/lib/middleman-core/core_extensions/data.rb b/middleman-core/lib/middleman-core/core_extensions/data.rb index 360d953f..22f91778 100644 --- a/middleman-core/lib/middleman-core/core_extensions/data.rb +++ b/middleman-core/lib/middleman-core/core_extensions/data.rb @@ -25,11 +25,11 @@ module Middleman # parsing config.rb def initialize self.files.changed DataStore.matcher do |file| - self.data.touch_file(file) if file.match(%r{^#{self.data_dir}\/}) + self.data.touch_file(file) if file.start_with?("#{self.data_dir}/") end self.files.deleted DataStore.matcher do |file| - self.data.remove_file(file) if file.match(%r{^#{self.data_dir}\/}) + self.data.remove_file(file) if file.start_with?("#{self.data_dir}/") end super diff --git a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb index 44eb2e31..14c5b214 100644 --- a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb +++ b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb @@ -145,7 +145,8 @@ module Middleman # @param [Pathname] path # @return [Boolean] def ignored?(path) - IGNORE_LIST.any? { |r| path.to_s.match(r) } + path = path.to_s + IGNORE_LIST.any? { |r| path =~ r } end # Notify callbacks for a file given an array of callbacks diff --git a/middleman-core/lib/middleman-core/core_extensions/rendering.rb b/middleman-core/lib/middleman-core/core_extensions/rendering.rb index 83c5d737..f700e329 100644 --- a/middleman-core/lib/middleman-core/core_extensions/rendering.rb +++ b/middleman-core/lib/middleman-core/core_extensions/rendering.rb @@ -408,7 +408,7 @@ module Middleman # Find the path by searching or using the cache request_path = request_path.to_s cache.fetch(:resolve_template, request_path, options) do - relative_path = request_path.sub(%r{^/}, "") + relative_path = Util.strip_leading_slash(request_path) on_disk_path = File.expand_path(relative_path, self.source_dir) # By default, any engine will do diff --git a/middleman-core/lib/middleman-core/preview_server.rb b/middleman-core/lib/middleman-core/preview_server.rb index 5f53c5ed..6308f4f2 100644 --- a/middleman-core/lib/middleman-core/preview_server.rb +++ b/middleman-core/lib/middleman-core/preview_server.rb @@ -185,7 +185,7 @@ module Middleman paths.any? do |path| match_against.any? do |matcher| - path.match(matcher) + path =~ matcher end end end diff --git a/middleman-core/lib/middleman-core/sitemap.rb b/middleman-core/lib/middleman-core/sitemap.rb index b50652b9..b3c75568 100644 --- a/middleman-core/lib/middleman-core/sitemap.rb +++ b/middleman-core/lib/middleman-core/sitemap.rb @@ -25,18 +25,18 @@ module Middleman # Setup callbacks which can exclude paths from the sitemap app.set :ignored_sitemap_matchers, { # dotfiles and folders in the root - :root_dotfiles => proc { |file| file.match(%r{^\.}) }, + :root_dotfiles => proc { |file| file.start_with?('.') }, # Files starting with an dot, but not .htaccess :source_dotfiles => proc { |file| - file.match(%r{/\.}) && !file.match(%r{/\.htaccess|\.htpasswd}) + file =~ %r{/\.} && file !~ %r{/\.(htaccess|htpasswd)} }, # Files starting with an underscore, but not a double-underscore - :partials => proc { |file| file.match(%r{/_}) && !file.match(%r{/__}) }, + :partials => proc { |file| file =~ %r{/_} && file !~ %r{/__} }, :layout => proc { |file| - file.match(%r{^source/layout\.}) || file.match(%r{^source/layouts/}) + file.start_with?('source/layout.') || file.start_with?('source/layouts/') } } diff --git a/middleman-core/lib/middleman-core/sitemap/store.rb b/middleman-core/lib/middleman-core/sitemap/store.rb index eb78088c..30f59789 100644 --- a/middleman-core/lib/middleman-core/sitemap/store.rb +++ b/middleman-core/lib/middleman-core/sitemap/store.rb @@ -108,7 +108,7 @@ module Middleman blank_metadata = { :options => {}, :locals => {}, :page => {}, :blocks => [] } provides_metadata.inject(blank_metadata) do |result, (callback, matcher)| - next result if !matcher.nil? && !source_file.match(matcher) + next result if matcher && !source_file.match(matcher) metadata = callback.call(source_file) @@ -146,9 +146,9 @@ module Middleman @_cached_metadata[request_path] = provides_metadata_for_path.inject(blank_metadata) do |result, (callback, matcher)| case matcher when Regexp - next result unless request_path.match(matcher) + next result unless request_path =~ matcher when String - next result unless File.fnmatch("/" + matcher.sub(%r{^/}, ''), "/#{request_path}") + next result unless File.fnmatch("/" + Util.strip_leading_slash(matcher), "/#{request_path}") end metadata = callback.call(request_path) diff --git a/middleman-core/lib/middleman-core/util.rb b/middleman-core/lib/middleman-core/util.rb index 0444be7a..00a28e2b 100644 --- a/middleman-core/lib/middleman-core/util.rb +++ b/middleman-core/lib/middleman-core/util.rb @@ -83,7 +83,13 @@ module Middleman # @return [String] def self.normalize_path(path) # The tr call works around a bug in Ruby's Unicode handling - path.sub(/^\//, "").tr('','') + path.sub(%r{^/}, "").tr('','') + end + + # This is a separate method from normalize_path in case we + # change how we normalize paths + def self.strip_leading_slash(path) + path.sub(%r{^/}, "") end # Extract the text of a Rack response as a string. diff --git a/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb b/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb index 4b30967c..80233244 100644 --- a/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb +++ b/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb @@ -64,7 +64,7 @@ module Middleman # If the basename of the request as no extension, assume we are serving a # directory and join index_file to the path. path = File.join(asset_dir, current_path) - path = path.gsub(File.extname(path), ".#{asset_ext}") + path = path.sub(/#{File.extname(path)}$/, ".#{asset_ext}") yield path if sitemap.find_resource_by_path(path) end @@ -74,11 +74,11 @@ module Middleman # @return [String] def page_classes path = current_path.dup - path << index_file if path.match(%r{/$}) - path = path.gsub(%r{^/}, '') + path << index_file if path.end_with?('/') + path = Util.strip_leading_slash(path) classes = [] - parts = path.split('.')[0].split('/') + parts = path.split('.').first.split('/') parts.each_with_index { |path, i| classes << parts.first(i+1).join('_') } classes.join(' ') @@ -90,19 +90,18 @@ module Middleman # @param [String] source The path to the file # @return [String] def asset_path(kind, source) - return source if source =~ /^http/ + return source if source.to_s.include?('//') asset_folder = case kind when :css then css_dir when :js then js_dir when :images then images_dir else kind.to_s end - source = source.to_s.gsub(/\s/, '') + source = source.to_s.tr(' ', '') ignore_extension = (kind == :images) # don't append extension - source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/ - if source =~ %r{^/} # absolute path - asset_folder = "" - end + source << ".#{kind}" unless ignore_extension || source.end_with?(".#{kind}") + asset_folder = "" if source.start_with?('/') # absolute path + asset_url(source, asset_folder) end diff --git a/middleman-more/lib/middleman-more/core_extensions/i18n.rb b/middleman-more/lib/middleman-more/core_extensions/i18n.rb index e4d5c696..1ab2c019 100644 --- a/middleman-more/lib/middleman-more/core_extensions/i18n.rb +++ b/middleman-more/lib/middleman-more/core_extensions/i18n.rb @@ -28,6 +28,7 @@ module Middleman @app = app @locales_glob = File.join(app.locales_dir, "**", "*.{rb,yml,yaml}") + # File.fnmatch doesn't support brackets: {rb,yml} regex = @locales_glob.sub(/\./, '\.').sub(File.join("**", "*"), ".*").sub(/\//, '\/').sub("{rb,yml,yaml}", "rb|ya?ml") @locales_regex = %r{^#{regex}} @@ -78,7 +79,7 @@ module Middleman end def on_file_changed(file) - if @locales_regex.match(file) + if @locales_regex =~ file ::I18n.reload! end end diff --git a/middleman-more/lib/middleman-more/extensions/automatic_image_sizes.rb b/middleman-more/lib/middleman-more/extensions/automatic_image_sizes.rb index 58e52f71..a83ca4cc 100644 --- a/middleman-more/lib/middleman-more/extensions/automatic_image_sizes.rb +++ b/middleman-more/lib/middleman-more/extensions/automatic_image_sizes.rb @@ -38,7 +38,7 @@ module Middleman params[:alt] ||= "" real_path = path - real_path = File.join(images_dir, real_path) unless real_path =~ %r{^/} + real_path = File.join(images_dir, real_path) unless real_path.start_with?('/') full_path = File.join(source_dir, real_path) if File.exists? full_path