Module: Nanoc::Helpers::LinkTo

Includes:
HTMLEscape
Included in:
Filters::RelativizePaths
Defined in:
lib/nanoc/helpers/link_to.rb

Overview

Instance Method Summary collapse

Methods included from HTMLEscape

#html_escape

Methods included from Capturing

#capture, #content_for

Instance Method Details

Parameters:

  • text (String)
  • attributes (Hash) (defaults to: {})

Returns:

  • (String)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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
      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

Parameters:

  • text (String)
  • attributes (Hash) (defaults to: {})

Returns:

  • (String)


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nanoc/helpers/link_to.rb', line 42

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

Returns:

  • (String)


55
56
57
58
59
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
# File 'lib/nanoc/helpers/link_to.rb', line 55

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