lib/nanoc3/helpers/link_to.rb in nanoc3-3.1.9 vs lib/nanoc3/helpers/link_to.rb in nanoc3-3.2.0a1
- old
+ new
@@ -15,14 +15,14 @@
# using {Nanoc3::Helpers::HTMLEscape#html_escape}.
#
# @param [String] text The visible link text
#
# @param [String, Nanoc3::Item, Nanoc3::ItemRep] target The path/URL,
- # item or item representation that should be linked to
+ # item or item representation that should be linked to
#
# @param [Hash] attributes A hash containing HTML attributes (e.g.
- # `rel`, `title`, …) that will be added to the link.
+ # `rel`, `title`, …) that will be added to the link.
#
# @return [String] The link text
#
# @example Linking to a path
#
@@ -45,16 +45,11 @@
#
# link_to('Blog', '/blog/', :title => 'My super cool blog')
# # => '<a title="My super cool blog" href="/blog/">Blog</a>'
def link_to(text, target, attributes={})
# Find path
- if target.is_a?(String)
- path = target
- else
- path = target.path
- raise RuntimeError, "Cannot create a link to #{target.inspect} because this target is not outputted (its routing rule returns nil)" if path.nil?
- end
+ path = target.is_a?(String) ? target : target.path
# Join attributes
attributes = attributes.inject('') do |memo, (key, value)|
memo + key.to_s + '="' + h(value) + '" '
end
@@ -69,14 +64,14 @@
# {#link_to} apply here as well.
#
# @param [String] text The visible link text
#
# @param [String, Nanoc3::Item, Nanoc3::ItemRep] target The path/URL,
- # item or item representation that should be linked to
+ # item or item representation that should be linked to
#
# @param [Hash] attributes A hash containing HTML attributes (e.g.
- # `rel`, `title`, …) that will be added to the link.
+ # `rel`, `title`, …) that will be added to the link.
#
# @return [String] The link text
#
# @example Linking to a different page
#
@@ -101,12 +96,12 @@
# 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
+ # item or item representation to which the relative path should be
+ # generated
#
# @return [String] The relative path to the target
#
# @example
#
@@ -115,23 +110,17 @@
# # => '../qux/'
def relative_path_to(target)
require 'pathname'
# Find path
- if target.is_a?(String)
- path = target
- else
- path = target.path
- raise RuntimeError, "Cannot get the relative path to #{target.inspect} because this target is not outputted (its routing rule returns nil)" if path.nil?
- end
+ path = target.is_a?(String) ? target : target.path
# Get source and destination paths
dst_path = Pathname.new(path)
- raise RuntimeError, "Cannot get the relative path to #{path} because the current item representation, #{@item_rep.inspect}, is not outputted (its routing rule returns nil)" if @item_rep.path.nil?
src_path = Pathname.new(@item_rep.path)
- # Calculate the relative path (method depends on whether destination is
- # a directory or not).
+ # 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