Module: Nanoc3::Helpers::LinkTo

Included Modules

Nanoc3::Helpers::HTMLEscape

Nanoc3::Helpers::LinkTo contains functions for linking to items.

To activate this helper, include it, like this:

  include Nanoc3::Helpers::LinkTo

Public Visibility

Public Instance Method Summary

#link_to(text, path_or_rep, attributes = {})

Creates a HTML link to the given path or item representation, and with the given text.

#link_to_unless_current(text, path_or_rep, attributes = {})

Creates a HTML link using link_to, except when the linked item is the current one.

#relative_path_to(target)

Returns the relative path from the current item to the given path or item representation.

Public Instance Methods Included from Nanoc3::Helpers::HTMLEscape

html_escape

Public Instance Method Details

link_to

link_to_unless_current

relative_path_to

public relative_path_to(target)

Returns the relative path from the current item to the given path or item representation.

path_or_rep:the URL or path (a String) to where the relative should point, or the item representation to which the relative should point.

Example:

  # if the current item's path is /foo/bar/
  relative_path('/foo/qux/')
  # => '../qux/'
[View source]


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/nanoc3/helpers/link_to.rb', line 85

def relative_path_to(target)
  require 'pathname'

  # Find path
  if target.is_a?(String)
    path = target
  elsif target.respond_to?(:reps)
    path = target.reps.find { |r| r.name == :default }.path
  else
    path = target.path
  end

  # 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
Generated on Sunday, August 09 2009 at 01:43:07 PM by YARD 0.2.3.2 (ruby-1.8.7).