Module: Nanoc::Helpers::LinkTo
- Includes:
- HTMLEscape
- Defined in:
- lib/nanoc/helpers/link_to.rb
Overview
Contains functions for linking to items and item representations.
Instance Method Summary collapse
-
#link_to(text, target, attributes = {}) ⇒ String
Creates a HTML link to the given path or item representation, and with the given text.
-
#link_to_unless_current(text, target, attributes = {}) ⇒ String
Creates a HTML link using #link_to, except when the linked item is the current one.
-
#relative_path_to(target) ⇒ String
Returns the relative path from the current item to the given path or item representation.
Methods included from HTMLEscape
Methods included from Capturing
Instance Method Details
#link_to(text, target, attributes = {}) ⇒ String
Creates a HTML link to the given path or item representation, and with
the given text. All attributes of the a
element, including the href
attribute, will be HTML-escaped; the contents of the a
element, which
can contain markup, will not be HTML-escaped. The HTML-escaping is done
using HTMLEscape#html_escape.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/nanoc/helpers/link_to.rb', line 44 def link_to(text, target, attributes = {}) # Find path path = case target when String target when Nanoc::ItemWithRepsView, Nanoc::ItemWithoutRepsView, Nanoc::ItemRepView raise "Cannot create a link to #{target.inspect} because this target is not outputted (its routing rule returns nil)" if target.path.nil? target.path else raise ArgumentError, "Cannot link to #{target.inspect} (expected a string or an item, not a #{target.class.name})" end # Join attributes attributes = attributes.reduce('') do |memo, (key, value)| memo + key.to_s + '="' + h(value) + '" ' end # Create link "<a #{attributes}href=\"#{h path}\">#{text}</a>" end |
#link_to_unless_current(text, target, attributes = {}) ⇒ String
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/nanoc/helpers/link_to.rb', line 90 def link_to_unless_current(text, target, attributes = {}) # Find path path = target.is_a?(String) ? target : target.path if @item_rep && @item_rep.path == path # Create message "<span class=\"active\">#{text}</span>" else link_to(text, target, attributes) end end |
#relative_path_to(target) ⇒ String
Returns the relative path from the current item to the given path or item representation. The returned path will not be HTML-escaped.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/nanoc/helpers/link_to.rb', line 116 def relative_path_to(target) require 'pathname' # Find path if target.is_a?(String) path = target else path = target.path if path.nil? # TODO: get proper error raise "Cannot get the relative path to #{target.inspect} because this target is not outputted (its routing rule returns nil)" end end # Handle Windows network (UNC) paths if path.start_with?('//', '\\\\') return path end # Get source and destination paths dst_path = Pathname.new(path) if @item_rep.path.nil? # TODO: get proper error raise "Cannot get the relative path to #{path} because the current item representation, #{@item_rep.inspect}, is not outputted (its routing rule returns nil)" end src_path = Pathname.new(@item_rep.path) # Calculate the relative path (method depends on whether destination is # a directory or not). relative_path = if src_path.to_s[-1, 1] != '/' dst_path.relative_path_from(src_path.dirname).to_s else 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 |