Included Modules

Class/Module Index [+]

Quicksearch

Nanoc3::Helpers::LinkTo

Contains functions for linking to items and item representations.

Public Instance Methods

relative_path_to(target) click to toggle source

Returns the relative path from the current item to the given path or item representation. The returned path will not be HTML-escaped.

@param [String, Nanoc3::Item, Nanoc3::ItemRep] target The path/URL, item or item representation to which the relative path should be generated

@return [String] The relative path to the target

@example

# if the current item's path is /foo/bar/
relative_path_to('/foo/qux/')
# => '../qux/'
# File lib/nanoc3/helpers/link_to.rb, line 110
def relative_path_to(target)
  require 'pathname'

  # Find path
  path = target.is_a?(String) ? target : target.path

  # Get source and destination paths
  dst_path   = Pathname.new(path)
  src_path   = Pathname.new(@item_rep.path)

  # Calculate elative path (method depends on whether destination is a
  # directory or not).
  if src_path.to_s[-1,1] != '/'
    relative_path = dst_path.relative_path_from(src_path.dirname).to_s
  else
    relative_path = dst_path.relative_path_from(src_path).to_s
  end

  # Add trailing slash if necessary
  if dst_path.to_s[-1,1] == '/'
    relative_path << '/'
  end

  # Done
  relative_path
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.