Module: Nanoc::Helpers::LinkTo
Overview
Instance Method Summary collapse
- #link_to(text, target, attributes = {}) ⇒ String
- #link_to_unless_current(text, target, attributes = {}) ⇒ String
- #relative_path_to(target) ⇒ String
Methods included from HTMLEscape
Methods included from Capturing
Instance Method Details
#link_to(text, target, attributes = {}) ⇒ String
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/nanoc/helpers/link_to.rb', line 14 def link_to(text, target, attributes = {}) # Find path path = case target when String target when Nanoc::Core::CompilationItemView, Nanoc::Core::BasicItemView, Nanoc::Core::BasicItemRepView if target.path.nil? raise "Cannot create a link to #{target.inspect} because this " \ 'target is not outputted (its routing rule returns nil)' end 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}=\"#{h(value)}\" " end # Create link "<a #{attributes}href=\"#{h path}\">#{text}</a>" end |
#link_to_unless_current(text, target, attributes = {}) ⇒ String
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/nanoc/helpers/link_to.rb', line 47 def link_to_unless_current(text, target, attributes = {}) # Find path path = target.is_a?(String) ? target : target.path if @item_rep&.path == path # Create message "<span class=\"active\">#{text}</span>" else link_to(text, target, attributes) end end |
#relative_path_to(target) ⇒ String
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/nanoc/helpers/link_to.rb', line 60 def relative_path_to(target) # 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). from = src_path.to_s.end_with?('/') ? src_path : src_path.dirname relative_path = dst_path.relative_path_from(from).to_s # Add trailing slash if necessary if dst_path.to_s.end_with?('/') relative_path << '/' end # Done relative_path end |