Class: Middleman::CoreExtensions::Routing

Inherits:
Extension
  • Object
show all
Defined in:
middleman-core/lib/middleman-core/core_extensions/routing.rb

Defined Under Namespace

Classes: PageDescriptor

Constant Summary

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER, Contracts::ResourceList

Instance Attribute Summary

Attributes inherited from Extension

#app, #options

Instance Method Summary (collapse)

Methods inherited from Extension

activated_extension, #after_build, #after_configuration, after_extension_activated, #after_extension_activated, #before_build, clear_after_extension_callbacks, config, helpers, option

Methods included from Contracts

#Contract

Constructor Details

- (Routing) initialize(app, options_hash = {}, &block)



9
10
11
12
13
# File 'middleman-core/lib/middleman-core/core_extensions/routing.rb', line 9

def initialize(app, options_hash={}, &block)
  super

  @page_configs = Set.new
end

Instance Method Details

- (Object) before_configuration



15
16
17
# File 'middleman-core/lib/middleman-core/core_extensions/routing.rb', line 15

def before_configuration
  app.add_to_config_context :page, &method(:page)
end

- (Object) manipulate_resource_list(resources)



21
22
23
24
25
26
27
# File 'middleman-core/lib/middleman-core/core_extensions/routing.rb', line 21

def manipulate_resource_list(resources)
  resources.each do |resource|
    @page_configs.each do |p|
      resource.(p[:metadata]) if Middleman::Util.path_match(p[:path], "/#{resource.path}")
    end
  end
end

- (Object) page(path, opts = {})



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'middleman-core/lib/middleman-core/core_extensions/routing.rb', line 49

def page(path, opts={})
  options = opts.dup

  # Default layout
   = {
    options: options,
    locals: options.delete(:locals) || {},
    page: options.delete(:data) || {}
  }

  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

  path = '/' + Util.strip_leading_slash(path) if path.is_a?(String)

  @page_configs << PageDescriptor.new(path, )
end

- String

This method returns an undefined value.

The page method allows options to be set for a given source path, regex, or glob. Options that may be set include layout, locals, proxy, andx ignore.

Examples:

page '/about.html', layout: false
page '/index.html', layout: :homepage_layout
page '/foo.html', locals: { foo: 'bar' }


48
# File 'middleman-core/lib/middleman-core/core_extensions/routing.rb', line 48

Contract String, Hash => Any