Class: Nanoc2::Router

Inherits:
Plugin
  • Object
show all
Defined in:
lib/nanoc2/base/router.rb

Overview

A Nanoc2::Router is an abstract superclass that determines the paths of page and asset representations, both the path on the disk (relative to the site’s output directory) and the path as it appears on the web site.

Constant Summary

Constants inherited from Plugin

Plugin::MAP

Instance Method Summary collapse

Methods inherited from Plugin

identifier, identifiers, named, register

Constructor Details

#initialize(site) ⇒ Router

Creates a new router for the given site.



9
10
11
# File 'lib/nanoc2/base/router.rb', line 9

def initialize(site)
  @site = site
end

Instance Method Details

#disk_path_for(obj) ⇒ Object

Returns the disk path for the given page or asset representation, i.e. the page or asset’s custom path or routed path relative to the output directory.



63
64
65
66
67
68
# File 'lib/nanoc2/base/router.rb', line 63

def disk_path_for(obj)
  @site.config[:output_dir] + (
    obj.attribute_named(:custom_path) ||
    (obj.is_a?(Nanoc2::PageRep) ? path_for_page_rep(obj) : path_for_asset_rep(obj))
  )
end

#path_for_asset_rep(asset_rep) ⇒ Object

Returns the routed path for the given asset representation, including the filename and the extension. It should start with a slash, and should be relative to the web root (i.e. should not include any references to the output directory). There is no need to let this method handle custom paths.

Subclasses must implement this method.

Raises:

  • (NotImplementedError)


31
32
33
# File 'lib/nanoc2/base/router.rb', line 31

def path_for_asset_rep(asset_rep)
  raise NotImplementedError.new("Nanoc2::Router subclasses must implement #path_for_asset_rep.")
end

#path_for_page_rep(page_rep) ⇒ Object

Returns the routed path for the given page representation, including the filename and the extension. It should start with a slash, and should be relative to the web root (i.e. should not include any references to the output directory). There is no need to let this method handle custom paths.

Subclasses must implement this method.

Raises:

  • (NotImplementedError)


20
21
22
# File 'lib/nanoc2/base/router.rb', line 20

def path_for_page_rep(page_rep)
  raise NotImplementedError.new("Nanoc2::Router subclasses must implement #path_for_page_rep.")
end

#web_path_for(obj) ⇒ Object

Returns the web path for the given page or asset representation, i.e. the page or asset rep’s custom path or routed path with index filenames stripped.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nanoc2/base/router.rb', line 38

def web_path_for(obj)
  # Get actual path
  path ||= obj.attribute_named(:custom_path)
  if obj.is_a?(Nanoc2::PageRep) # Page rep
    path ||= path_for_page_rep(obj)
  else # Asset rep
    path ||= path_for_asset_rep(obj)
  end

  # Try stripping each index filename
  @site.config[:index_filenames].each do |index_filename|
    if path[-index_filename.length..-1] == index_filename
      # Strip and stop
      path = path[0..-index_filename.length-1]
      break
    end
  end

  # Return possibly stripped path
  path
end