Merge pull request #1308 from bhollis/slim_page

Remove options to proxy or ignore through `page`
This commit is contained in:
Ben Hollis 2014-06-30 21:11:08 -07:00
commit 4330ff52ae
12 changed files with 57 additions and 77 deletions

View file

@ -1,6 +1,7 @@
master master
=== ===
* Removed the `proxy` and `ignore` options for the `page` command in `config.rb`. Use the `proxy` and `ignore` commands instead of passing these options to `page`.
* The `page` command in `config.rb` can now be used to add data to the page via the `data` argument. It is accessed the same way as frontmatter data, via `current_resource.data`. * The `page` command in `config.rb` can now be used to add data to the page via the `data` argument. It is accessed the same way as frontmatter data, via `current_resource.data`.
* Add support for `environments` with the `-e` CLI flag. Loads additional config from `environments/envname.rb`. Removed `development?` helper in favor of `environment?(:development)`. Added `server?` helper to differentiate between build and server mode. * Add support for `environments` with the `-e` CLI flag. Loads additional config from `environments/envname.rb`. Removed `development?` helper in favor of `environment?(:development)`. Added `server?` helper to differentiate between build and server mode.
* Removed `with_layout`. Use loops of `page` instead. * Removed `with_layout`. Use loops of `page` instead.

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" ignore "/should_be_ignored.html"
page "/should_be_ignored2.html", ignore: true ignore "/should_be_ignored2.html"
page "/target_ignore.html", proxy: "/should_be_ignored3.html", ignore: true proxy "/target_ignore.html", "/should_be_ignored3.html", ignore: true
%w(one two).each do |num| %w(one two).each do |num|
page "/fake/#{num}.html", proxy: "/real/index.html" do proxy "/fake/#{num}.html", "/real/index.html", locals: { num: num }
@num = num
end
end 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| data.pages.each do |p|
page p.from, proxy: p.to proxy p.from, p.to
end end

View file

@ -1,27 +1,27 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
page "/fake.html", proxy: "/real.html", layout: false proxy "/fake.html", "/real.html", layout: false
page "fake2.html", proxy: "/real.html", layout: false proxy "fake2.html", "/real.html", layout: false
page "fake3.html", proxy: "real.html", layout: false proxy "fake3.html", "real.html", layout: false
page "/fake4.html", proxy: "real.html", layout: false proxy "/fake4.html", "real.html", layout: false
ignore "/should_be_ignored.html" ignore "/should_be_ignored.html"
page "/should_be_ignored2.html", ignore: true ignore "/should_be_ignored2.html"
page "/target_ignore.html", proxy: "/should_be_ignored3.html", ignore: true proxy "/target_ignore.html", "/should_be_ignored3.html", ignore: true
ignore "should_be_ignored4.html" ignore "should_be_ignored4.html"
page "should_be_ignored5.html", ignore: true ignore "should_be_ignored5.html"
page "target_ignore2.html", proxy: "/should_be_ignored6.html", ignore: true proxy "target_ignore2.html", "/should_be_ignored6.html", ignore: true
page "target_ignore3.html", proxy: "should_be_ignored7.html", ignore: true proxy "target_ignore3.html", "should_be_ignored7.html", ignore: true
page "/target_ignore4.html", proxy: "should_be_ignored8.html", ignore: true proxy "/target_ignore4.html", "should_be_ignored8.html", ignore: true
%w(one two).each do |num| %w(one two).each do |num|
page "/fake/#{num}.html", proxy: "/real/index.html", ignore: true, locals: { num: num } proxy "/fake/#{num}.html", "/real/index.html", ignore: true, locals: { num: num }
page "fake2/#{num}.html", proxy: "/real/index.html", ignore: true, locals: { num: num } proxy "fake2/#{num}.html", "/real/index.html", ignore: true, locals: { num: num }
page "fake3/#{num}.html", proxy: "real/index.html", ignore: true, locals: { num: num } proxy "fake3/#{num}.html", "real/index.html", ignore: true, locals: { num: num }
page "/fake4/#{num}.html", proxy: "real/index.html", ignore: true, locals: { num: num } proxy "/fake4/#{num}.html", "real/index.html", ignore: true, locals: { num: num }
end end
page "明日がある.html", proxy: "/real.html", layout: false proxy "明日がある.html", "/real.html", layout: false
page "f*/*", locals: { all_glob: "I am all glob" } page "f*/*", locals: { all_glob: "I am all glob" }
page "fake/*", locals: { glob_var: "I am one 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 # 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 'override_layout.html', layout: :alternate
page 'page_mentioned.html' page 'page_mentioned.html'

View file

@ -1,4 +1,4 @@
# Proxy ignored.html, which should ignore itself through a frontmatter # 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 'override_layout.html', layout: :alternate
page 'page_mentioned.html' page 'page_mentioned.html'

View file

@ -1,4 +1,4 @@
activate :directory_indexes activate :directory_indexes
# Proxy ignored.html, which should ignore itself through a frontmatter # 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 activate :directory_indexes
page "/sub/fake.html", proxy: "/proxied.html", ignore: true proxy "/sub/fake.html", "/proxied.html", ignore: true
page "/sub/fake2.html", proxy: "/proxied.html", ignore: true proxy "/sub/fake2.html", "/proxied.html", ignore: true
page "/directory-indexed/fake.html", proxy: "/proxied.html", ignore: true proxy "/directory-indexed/fake.html", "/proxied.html", ignore: true
page "/directory-indexed/fake2.html", proxy: "/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 proxy "/sub/fake.html", "/proxied.html", ignore: true
page "/sub/fake2.html", proxy: "/proxied.html", ignore: true proxy "/sub/fake2.html", "/proxied.html", ignore: true
page "/directory-indexed/fake.html", proxy: "/proxied.html", ignore: true proxy "/directory-indexed/fake.html", "/proxied.html", ignore: true
page "/directory-indexed/fake2.html", proxy: "/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) def manipulate_resource_list(resources)
resources.each do |resource| resources.each do |resource|
@page_configs.each do |matcher, metadata| @page_configs.each do |matcher, metadata|
case matcher resource.add_metadata(metadata) if Middleman::Util.path_match(matcher, "/#{resource.path}")
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
end end
end 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 [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] 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 [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] # @return [void]
def page(path, opts={}) def page(path, opts={})
options = opts.dup options = opts.dup
@ -56,26 +47,22 @@ module Middleman
# Default layout # Default layout
# TODO: This seems wrong # TODO: This seems wrong
options[:layout] = @app.config[:layout] if options[:layout].nil? options[:layout] = @app.config[:layout] if options[:layout].nil?
# TODO: You can set options and locals, but not data metadata = {
metadata = { options: options, locals: options.delete(:locals) || {}, page: options.delete(:data) || {} } options: options,
locals: options.delete(:locals) || {},
page: options.delete(:data) || {}
}
# If the path is a regexp if path.is_a?(String) && !path.include?('*')
unless path.is_a?(Regexp) || path.include?('*') # Normalize path
# Normalized path path = Middleman::Util.normalize_path(path)
path = '/' + Middleman::Util.normalize_path(path)
if path.end_with?('/') || File.directory?(File.join(@app.source_dir, path)) if path.end_with?('/') || File.directory?(File.join(@app.source_dir, path))
path = File.join(path, @app.config[:index_file]) 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 end
if path.is_a?(String)
path = '/' + Util.strip_leading_slash(path)
end end
@page_configs << [path, metadata] @page_configs << [path, metadata]

View file

@ -15,19 +15,24 @@ module Middleman
end end
# Setup a proxy from a path to a target # Setup a proxy from a path to a target
# @param [String] path # @param [String] path The new, proxied path to create
# @param [String] target # @param [String] target The existing path that should be proxied to. This must be a real resource, not another proxy.
# @param [Hash] opts options to apply to the proxy, including things like # @option opts [Boolean] ignore Ignore the target from the sitemap (so only the new, proxy resource ends up in the output)
# :locals, :ignore to hide the proxy target, :layout, and :directory_indexes. # @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] # @return [void]
def create_proxy(path, target, opts={}) def create_proxy(path, target, opts={})
options = opts.dup options = opts.dup
metadata = { options: {}, locals: {} }
metadata[:locals] = options.delete(:locals) || {}
@app.ignore(target) if options.delete(:ignore) @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) @proxy_configs << ProxyConfiguration.new(path: path, target: target, metadata: metadata)