From dfecfebc692857a1e72e6f80f9f325fee5b9be22 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Mon, 16 Jun 2014 21:44:14 -0700 Subject: [PATCH] Remove options to proxy or ignore through `page`, and use Middleman::Util#path_match for matchers to allow a wider set of matchers. --- .../clean-app/config-complications.rb | 10 ++--- middleman-core/fixtures/clean-app/config.rb | 11 ------ middleman-core/fixtures/data-app/config.rb | 2 +- .../fixtures/dynamic-pages-app/config.rb | 30 +++++++-------- .../frontmatter-settings-app/config.rb | 2 +- .../config.rb | 2 +- .../more-frontmatter-settings-app/config.rb | 2 +- .../fixtures/more-traversal-app/config.rb | 8 ++-- .../fixtures/traversal-app/config.rb | 8 ++-- .../middleman-core/core_extensions/routing.rb | 37 ++++++------------- .../sitemap/extensions/proxies.rb | 21 +++++++---- 11 files changed, 56 insertions(+), 77 deletions(-) diff --git a/middleman-core/fixtures/clean-app/config-complications.rb b/middleman-core/fixtures/clean-app/config-complications.rb index a958bf5a..1326120c 100644 --- a/middleman-core/fixtures/clean-app/config-complications.rb +++ b/middleman-core/fixtures/clean-app/config-complications.rb @@ -1,11 +1,9 @@ -page "/fake.html", proxy: "/real.html", layout: false +proxy "/fake.html", "/real.html", layout: false ignore "/should_be_ignored.html" -page "/should_be_ignored2.html", ignore: true -page "/target_ignore.html", proxy: "/should_be_ignored3.html", ignore: true +ignore "/should_be_ignored2.html" +proxy "/target_ignore.html", "/should_be_ignored3.html", ignore: true %w(one two).each do |num| - page "/fake/#{num}.html", proxy: "/real/index.html" do - @num = num - end + proxy "/fake/#{num}.html", "/real/index.html", locals: { num: num } end diff --git a/middleman-core/fixtures/clean-app/config.rb b/middleman-core/fixtures/clean-app/config.rb index a958bf5a..e69de29b 100644 --- a/middleman-core/fixtures/clean-app/config.rb +++ b/middleman-core/fixtures/clean-app/config.rb @@ -1,11 +0,0 @@ -page "/fake.html", proxy: "/real.html", layout: false - -ignore "/should_be_ignored.html" -page "/should_be_ignored2.html", ignore: true -page "/target_ignore.html", proxy: "/should_be_ignored3.html", ignore: true - -%w(one two).each do |num| - page "/fake/#{num}.html", proxy: "/real/index.html" do - @num = num - end -end diff --git a/middleman-core/fixtures/data-app/config.rb b/middleman-core/fixtures/data-app/config.rb index 1d0d0ba7..2aa77658 100644 --- a/middleman-core/fixtures/data-app/config.rb +++ b/middleman-core/fixtures/data-app/config.rb @@ -1,3 +1,3 @@ data.pages.each do |p| - page p.from, proxy: p.to + proxy p.from, p.to end diff --git a/middleman-core/fixtures/dynamic-pages-app/config.rb b/middleman-core/fixtures/dynamic-pages-app/config.rb index db5990d8..75fab5e0 100644 --- a/middleman-core/fixtures/dynamic-pages-app/config.rb +++ b/middleman-core/fixtures/dynamic-pages-app/config.rb @@ -1,27 +1,27 @@ # -*- coding: utf-8 -*- -page "/fake.html", proxy: "/real.html", layout: false -page "fake2.html", proxy: "/real.html", layout: false -page "fake3.html", proxy: "real.html", layout: false -page "/fake4.html", proxy: "real.html", layout: false +proxy "/fake.html", "/real.html", layout: false +proxy "fake2.html", "/real.html", layout: false +proxy "fake3.html", "real.html", layout: false +proxy "/fake4.html", "real.html", layout: false ignore "/should_be_ignored.html" -page "/should_be_ignored2.html", ignore: true -page "/target_ignore.html", proxy: "/should_be_ignored3.html", ignore: true +ignore "/should_be_ignored2.html" +proxy "/target_ignore.html", "/should_be_ignored3.html", ignore: true ignore "should_be_ignored4.html" -page "should_be_ignored5.html", ignore: true -page "target_ignore2.html", proxy: "/should_be_ignored6.html", ignore: true -page "target_ignore3.html", proxy: "should_be_ignored7.html", ignore: true -page "/target_ignore4.html", proxy: "should_be_ignored8.html", ignore: true +ignore "should_be_ignored5.html" +proxy "target_ignore2.html", "/should_be_ignored6.html", ignore: true +proxy "target_ignore3.html", "should_be_ignored7.html", ignore: true +proxy "/target_ignore4.html", "should_be_ignored8.html", ignore: true %w(one two).each do |num| - page "/fake/#{num}.html", proxy: "/real/index.html", ignore: true, locals: { num: num } - page "fake2/#{num}.html", proxy: "/real/index.html", ignore: true, locals: { num: num } - page "fake3/#{num}.html", proxy: "real/index.html", ignore: true, locals: { num: num } - page "/fake4/#{num}.html", proxy: "real/index.html", ignore: true, locals: { num: num } + proxy "/fake/#{num}.html", "/real/index.html", ignore: true, locals: { num: num } + proxy "fake2/#{num}.html", "/real/index.html", ignore: true, locals: { num: num } + proxy "fake3/#{num}.html", "real/index.html", ignore: true, locals: { num: num } + proxy "/fake4/#{num}.html", "real/index.html", ignore: true, locals: { num: num } end -page "明日がある.html", proxy: "/real.html", layout: false +proxy "明日がある.html", "/real.html", layout: false page "f*/*", locals: { all_glob: "I am all glob" } page "fake/*", locals: { glob_var: "I am one glob" } diff --git a/middleman-core/fixtures/frontmatter-settings-app/config.rb b/middleman-core/fixtures/frontmatter-settings-app/config.rb index 7e6a7bf9..a5e8129a 100644 --- a/middleman-core/fixtures/frontmatter-settings-app/config.rb +++ b/middleman-core/fixtures/frontmatter-settings-app/config.rb @@ -1,4 +1,4 @@ # Proxy ignored.html, which should ignore itself through a frontmatter -page 'proxied.html', proxy: 'ignored.html' +proxy 'proxied.html', 'ignored.html' page 'override_layout.html', layout: :alternate page 'page_mentioned.html' diff --git a/middleman-core/fixtures/frontmatter-settings-neighbor-app/config.rb b/middleman-core/fixtures/frontmatter-settings-neighbor-app/config.rb index 7e6a7bf9..a5e8129a 100644 --- a/middleman-core/fixtures/frontmatter-settings-neighbor-app/config.rb +++ b/middleman-core/fixtures/frontmatter-settings-neighbor-app/config.rb @@ -1,4 +1,4 @@ # Proxy ignored.html, which should ignore itself through a frontmatter -page 'proxied.html', proxy: 'ignored.html' +proxy 'proxied.html', 'ignored.html' page 'override_layout.html', layout: :alternate page 'page_mentioned.html' diff --git a/middleman-core/fixtures/more-frontmatter-settings-app/config.rb b/middleman-core/fixtures/more-frontmatter-settings-app/config.rb index f0a890d2..c5c9fb86 100644 --- a/middleman-core/fixtures/more-frontmatter-settings-app/config.rb +++ b/middleman-core/fixtures/more-frontmatter-settings-app/config.rb @@ -1,4 +1,4 @@ activate :directory_indexes # Proxy ignored.html, which should ignore itself through a frontmatter -page 'proxied.html', proxy: 'ignored.html' +proxy 'proxied.html', 'ignored.html' diff --git a/middleman-core/fixtures/more-traversal-app/config.rb b/middleman-core/fixtures/more-traversal-app/config.rb index 051a262d..e484ea5d 100644 --- a/middleman-core/fixtures/more-traversal-app/config.rb +++ b/middleman-core/fixtures/more-traversal-app/config.rb @@ -1,7 +1,7 @@ activate :directory_indexes -page "/sub/fake.html", proxy: "/proxied.html", ignore: true -page "/sub/fake2.html", proxy: "/proxied.html", ignore: true +proxy "/sub/fake.html", "/proxied.html", ignore: true +proxy "/sub/fake2.html", "/proxied.html", ignore: true -page "/directory-indexed/fake.html", proxy: "/proxied.html", ignore: true -page "/directory-indexed/fake2.html", proxy: "/proxied.html", ignore: true +proxy "/directory-indexed/fake.html", "/proxied.html", ignore: true +proxy "/directory-indexed/fake2.html", "/proxied.html", ignore: true diff --git a/middleman-core/fixtures/traversal-app/config.rb b/middleman-core/fixtures/traversal-app/config.rb index 5d030e02..4ad23bcc 100644 --- a/middleman-core/fixtures/traversal-app/config.rb +++ b/middleman-core/fixtures/traversal-app/config.rb @@ -1,5 +1,5 @@ -page "/sub/fake.html", proxy: "/proxied.html", ignore: true -page "/sub/fake2.html", proxy: "/proxied.html", ignore: true +proxy "/sub/fake.html", "/proxied.html", ignore: true +proxy "/sub/fake2.html", "/proxied.html", ignore: true -page "/directory-indexed/fake.html", proxy: "/proxied.html", ignore: true -page "/directory-indexed/fake2.html", proxy: "/proxied.html", ignore: true +proxy "/directory-indexed/fake.html", "/proxied.html", ignore: true +proxy "/directory-indexed/fake2.html", "/proxied.html", ignore: true diff --git a/middleman-core/lib/middleman-core/core_extensions/routing.rb b/middleman-core/lib/middleman-core/core_extensions/routing.rb index 004fdc2c..da0cead6 100644 --- a/middleman-core/lib/middleman-core/core_extensions/routing.rb +++ b/middleman-core/lib/middleman-core/core_extensions/routing.rb @@ -19,14 +19,7 @@ module Middleman def manipulate_resource_list(resources) resources.each do |resource| @page_configs.each do |matcher, metadata| - case matcher - when Regexp - next unless resource.path =~ matcher - when String - next unless File.fnmatch('/' + Util.strip_leading_slash(matcher), "/#{resource.path}") - end - - resource.add_metadata metadata + resource.add_metadata(metadata) if Middleman::Util.path_match(matcher, "/#{resource.path}") end end end @@ -47,8 +40,6 @@ module Middleman # @option opts [Boolean] directory_indexes Whether or not the `:directory_indexes` extension applies to these paths. # @option opts [Hash] locals Local variables for the template. These will be available when the template renders. # @option opts [Hash] data Extra metadata to add to the page. This is the same as frontmatter, though frontmatter will take precedence over metadata defined here. Available via {Resource#data}. - # @option opts [String] proxy The source path for a template to proxy this path to. Only valid when a single path is provided. Prefer using the `proxy` method to do this. - # @option opts [Boolean] ignore Set to `true` to ignore the provided path(s). Only valid when a single path is provided. Prefer using the `ignore` method to do this. # @return [void] def page(path, opts={}) options = opts.dup @@ -56,26 +47,22 @@ module Middleman # Default layout # TODO: This seems wrong options[:layout] = @app.config[:layout] if options[:layout].nil? - # TODO: You can set options and locals, but not data - metadata = { options: options, locals: options.delete(:locals) || {}, page: options.delete(:data) || {} } + metadata = { + options: options, + locals: options.delete(:locals) || {}, + page: options.delete(:data) || {} + } - # If the path is a regexp - unless path.is_a?(Regexp) || path.include?('*') - # Normalized path - path = '/' + Middleman::Util.normalize_path(path) + if path.is_a?(String) && !path.include?('*') + # Normalize path + path = Middleman::Util.normalize_path(path) if path.end_with?('/') || File.directory?(File.join(@app.source_dir, path)) path = File.join(path, @app.config[:index_file]) end + end - # Setup proxy - if target = options.delete(:proxy) - # TODO: deprecate proxy through page? - @app.proxy(path, target, opts.dup) - return - elsif options.delete(:ignore) - # TODO: deprecate ignore through page? - @app.ignore(path) - end + if path.is_a?(String) + path = '/' + Util.strip_leading_slash(path) end @page_configs << [path, metadata] diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb b/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb index 37f76894..acba2eb7 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb @@ -15,19 +15,24 @@ module Middleman end # Setup a proxy from a path to a target - # @param [String] path - # @param [String] target - # @param [Hash] opts options to apply to the proxy, including things like - # :locals, :ignore to hide the proxy target, :layout, and :directory_indexes. + # @param [String] path The new, proxied path to create + # @param [String] target The existing path that should be proxied to. This must be a real resource, not another proxy. + # @option opts [Boolean] ignore Ignore the target from the sitemap (so only the new, proxy resource ends up in the output) + # @option opts [Symbol, Boolean, String] layout The layout name to use (e.g. `:article`) or `false` to disable layout. + # @option opts [Boolean] directory_indexes Whether or not the `:directory_indexes` extension applies to these paths. + # @option opts [Hash] locals Local variables for the template. These will be available when the template renders. + # @option opts [Hash] data Extra metadata to add to the page. This is the same as frontmatter, though frontmatter will take precedence over metadata defined here. Available via {Resource#data}. # @return [void] def create_proxy(path, target, opts={}) options = opts.dup - metadata = { options: {}, locals: {} } - metadata[:locals] = options.delete(:locals) || {} - @app.ignore(target) if options.delete(:ignore) - metadata[:options] = options + + metadata = { + options: options, + locals: options.delete(:locals) || {}, + page: options.delete(:data) || {} + } @proxy_configs << ProxyConfiguration.new(path: path, target: target, metadata: metadata)