Remove options to proxy or ignore through page, and use Middleman::Util#path_match for matchers to allow a wider set of matchers.

This commit is contained in:
Ben Hollis 2014-06-16 21:44:14 -07:00
parent 838e25085a
commit dfecfebc69
11 changed files with 56 additions and 77 deletions

View file

@ -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

View file

@ -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

View file

@ -1,3 +1,3 @@
data.pages.each do |p|
page p.from, proxy: p.to
proxy p.from, p.to
end

View file

@ -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" }

View file

@ -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'

View file

@ -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'

View file

@ -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'

View file

@ -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

View file

@ -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

View file

@ -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]

View file

@ -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)