Module: Nanoc2::Helpers::LinkTo
- Includes:
- HTMLEscape
- Included in:
- Filters::RelativizePathsInCSS, Filters::RelativizePathsInHTML
- Defined in:
- lib/nanoc2/helpers/link_to.rb
Overview
Instance Method Summary collapse
-
#link_to(text, path_or_rep, attributes = {}) ⇒ Object
Creates a HTML link to the given path or page/asset representation, and with the given text.
-
#link_to_unless_current(text, path_or_rep, attributes = {}) ⇒ Object
Creates a HTML link using link_to, except when the linked page is the current one.
-
#relative_path_to(path_or_rep) ⇒ Object
Returns the relative path from the current page to the given path or page/asset representation.
Methods included from HTMLEscape
Instance Method Details
#link_to(text, path_or_rep, attributes = {}) ⇒ Object
Creates a HTML link to the given path or page/asset representation, and with the given text.
path_or_rep
-
the URL or path (a String) that should be linked to, or the page or asset representation that should be linked to.
text
-
the visible link text.
attributes
-
a hash containing HTML attributes that will be added to the link.
Examples:
link_to('Blog', '/blog/')
# => '<a href="/blog/">Blog</a>'
page_rep = @pages.find { |p| p.page_id == 'special' }.reps(:default)
link_to('Special Page', page_rep)
# => '<a href="/special_page/">Special Page</a>'
link_to('Blog', '/blog/', :title => 'My super cool blog')
# => '<a href="/blog/" title="My super cool blog">Blog</a>
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/nanoc2/helpers/link_to.rb', line 36 def link_to(text, path_or_rep, attributes={}) # Find path path = path_or_rep.is_a?(String) ? path_or_rep : path_or_rep.path # Join attributes attributes = attributes.inject('') do |memo, (key, value)| memo + key.to_s + '="' + h(value) + '" ' end # Create link "<a #{attributes}href=\"#{path}\">#{text}</a>" end |
#link_to_unless_current(text, path_or_rep, attributes = {}) ⇒ Object
Creates a HTML link using link_to, except when the linked page is the current one. In this case, a span element with class “active” and with the given text will be returned.
Examples:
link_to_unless_current('Blog', '/blog/')
# => '<a href="/blog/">Blog</a>'
link_to_unless_current('This Page', @page_rep)
# => '<span class="active">This Page</span>'
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/nanoc2/helpers/link_to.rb', line 60 def link_to_unless_current(text, path_or_rep, attributes={}) # Find path path = path_or_rep.is_a?(String) ? path_or_rep : path_or_rep.path if @page_rep and @page_rep.path == path # Create message "<span class=\"active\" title=\"You're here.\">#{text}</span>" else link_to(text, path_or_rep, attributes) end end |
#relative_path_to(path_or_rep) ⇒ Object
Returns the relative path from the current page to the given path or page/asset representation.
path_or_rep
-
the URL or path (a String) to where the relative should point, or the page or asset representation to which the relative should point.
Example:
# if the current item's path is /foo/bar/
relative_path('/foo/qux/')
# => '../qux/'
84 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 |
# File 'lib/nanoc2/helpers/link_to.rb', line 84 def relative_path_to(path_or_rep) require 'pathname' # Find path path = path_or_rep.is_a?(String) ? path_or_rep : path_or_rep.path # Get source and destination paths dst_path = Pathname.new(path) src_path = Pathname.new((defined?(@page) ? @page : @asset).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 |