Module: Nanoc3::Helpers::LinkTo

### Home Classes Methods

### In Files

- lib/nanoc3/helpers/link_to.rb

### Methods

- #link_to
- #link_to_unless_current
- #relative_path_to

### Included Modules

- Nanoc3::Helpers::HTMLEscape

### Files

- ChangeLog
- LICENSE

### Class/Module Index

Quicksearch

- Nanoc3
- Nanoc3::ArrayExtensions
- Nanoc3::CLI
- Nanoc3::CLI::Base
- Nanoc3::CLI::Commands
- Nanoc3::CLI::Commands::Autocompile
- Nanoc3::CLI::Commands::Compile
- Nanoc3::CLI::Commands::CreateItem
- Nanoc3::CLI::Commands::CreateLayout
- Nanoc3::CLI::Commands::CreateSite
- Nanoc3::CLI::Commands::Debug
- Nanoc3::CLI::Commands::Help
- Nanoc3::CLI::Commands::Info
- Nanoc3::CLI::Commands::Update
- Nanoc3::CLI::Commands::View
- Nanoc3::CLI::Logger
- Nanoc3::CodeSnippet
- Nanoc3::Compiler
- Nanoc3::CompilerDSL
- Nanoc3::Context
- Nanoc3::DataSource
- Nanoc3::DataSources
- Nanoc3::DataSources::Delicious
- Nanoc3::DataSources::Filesystem
- Nanoc3::DataSources::FilesystemCombined
- Nanoc3::DataSources::FilesystemCompact
- Nanoc3::DataSources::FilesystemUnified
- Nanoc3::DataSources::FilesystemVerbose
- Nanoc3::DataSources::LastFM
- Nanoc3::DataSources::Twitter
- Nanoc3::DependencyTracker
- Nanoc3::DirectedGraph
- Nanoc3::Errors
- Nanoc3::Errors::CannotDetermineFilter
- Nanoc3::Errors::CannotLayoutBinaryItem
- Nanoc3::Errors::CannotUseBinaryFilter
- Nanoc3::Errors::CannotUseTextualFilter
- Nanoc3::Errors::DataNotYetAvailable
- Nanoc3::Errors::Generic
- Nanoc3::Errors::NoMatchingCompilationRuleFound
- Nanoc3::Errors::NoMatchingRoutingRuleFound
- Nanoc3::Errors::NoRulesFileFound
- Nanoc3::Errors::RecursiveCompilation
- Nanoc3::Errors::UnknownDataSource
- Nanoc3::Errors::UnknownFilter
- Nanoc3::Errors::UnknownLayout
- Nanoc3::Errors::UnmetDependency
- Nanoc3::Extra
- Nanoc3::Extra::AutoCompiler
- Nanoc3::Extra::CHiCk
- Nanoc3::Extra::CHiCk::CacheController
- Nanoc3::Extra::CHiCk::Client
- Nanoc3::Extra::CHiCk::RackClient
- Nanoc3::Extra::Context
- Nanoc3::Extra::Deployers
- Nanoc3::Extra::Deployers::Rsync
- Nanoc3::Extra::FileProxy
- Nanoc3::Extra::TimeExtensions
- Nanoc3::Extra::VCS
- Nanoc3::Extra::VCSes
- Nanoc3::Extra::VCSes::Bazaar
- Nanoc3::Extra::VCSes::Dummy
- Nanoc3::Extra::VCSes::Git
- Nanoc3::Extra::VCSes::Mercurial
- Nanoc3::Extra::VCSes::Subversion
- Nanoc3::Extra::Validators
- Nanoc3::Extra::Validators::Links
- Nanoc3::Extra::Validators::Links::EachPairEnumerator
- Nanoc3::Extra::Validators::W3C
- Nanoc3::Filter
- Nanoc3::Filters
- Nanoc3::Filters::BlueCloth
- Nanoc3::Filters::CodeRay
- Nanoc3::Filters::ColorizeSyntax
- Nanoc3::Filters::ERB
- Nanoc3::Filters::Erubis
- Nanoc3::Filters::Haml
- Nanoc3::Filters::Kramdown
- Nanoc3::Filters::Less
- Nanoc3::Filters::Markaby
- Nanoc3::Filters::Maruku
- Nanoc3::Filters::RDiscount
- Nanoc3::Filters::RDoc
- Nanoc3::Filters::Rainpress
- Nanoc3::Filters::RedCloth
- Nanoc3::Filters::RelativizePaths
- Nanoc3::Filters::RubyPants
- Nanoc3::Filters::Sass
- Nanoc3::HashExtensions
- Nanoc3::Helpers
- Nanoc3::Helpers::Blogging
- Nanoc3::Helpers::Breadcrumbs
- Nanoc3::Helpers::Capturing
- Nanoc3::Helpers::Filtering
- Nanoc3::Helpers::HTMLEscape
- Nanoc3::Helpers::LinkTo
- Nanoc3::Helpers::Rendering
- Nanoc3::Helpers::Tagging
- Nanoc3::Helpers::Text
- Nanoc3::Helpers::XMLSitemap
- Nanoc3::Item
- Nanoc3::ItemRep
- Nanoc3::Layout
- Nanoc3::NotificationCenter
- Nanoc3::PluginRegistry
- Nanoc3::PluginRegistry::PluginMethods
- Nanoc3::Rule
- Nanoc3::RuleContext
- Nanoc3::Site
- Nanoc3::StringExtensions
- Nanoc3::Tasks
- Nanoc3::Tasks::Clean
- Array
- Enumerable
- Hash
- Object
- OrderedHash
- String
- Time

No matching classes.

# Nanoc3::Helpers::LinkTo

Contains functions for linking to items and item representations.

### Public Instance Methods

link_to(text, target, attributes={}) click to toggle source

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 {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

@param [Hash] attributes A hash containing HTML attributes (e.g. `rel`, `title`, …) that will be added to the link.

@return [String] The link text

@example Linking to a path

    link_to('Blog', '/blog/')
    # => '<a href="/blog/">Blog</a>'

@example Linking to an item

    about = @items.find { |i| i.identifier == '/about/' }
    link_to('About Me', about)
    # => '<a href="/about.html">About Me</a>'

@example Linking to an item representation

    about = @items.find { |i| i.identifier == '/about/' }
    link_to('My vCard', about.rep(:vcard))
    # => '<a href="/about.vcf">My vCard</a>'

@example Linking with custom attributes

    link_to('Blog', '/blog/', :title => 'My super cool blog')
    # => '<a title="My super cool blog" href="/blog/">Blog</a>'

    # File lib/nanoc3/helpers/link_to.rb, line 47
    def link_to(text, target, attributes={})
      # Find path
      path = target.is_a?(String) ? target : target.path

      # Join attributes
      attributes = attributes.inject('') 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={}) click to toggle source

Creates a HTML link using link_to, except when the linked item is the current one. In this case, a span element with class “active” and with the given text will be returned. The HTML-escaping rules for {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

@param [Hash] attributes A hash containing HTML attributes (e.g. `rel`, `title`, …) that will be added to the link.

@return [String] The link text

@example Linking to a different page

    link_to_unless_current('Blog', '/blog/')
    # => '<a href="/blog/">Blog</a>'

@example Linking to the same page

    link_to_unless_current('This Item', @item)
    # => '<span class="active" title="You\'re here.">This Item</span>'

    # File lib/nanoc3/helpers/link_to.rb, line 84
    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\" title=\"You're here.\">#{text}</span>"
      else
        link_to(text, target, attributes)
      end
    end

relative_path_to(target) click to toggle source

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

@return [String] The relative path to the target

@example

    # if the current item's path is /foo/bar/
    relative_path_to('/foo/qux/')
    # => '../qux/'

    # File lib/nanoc3/helpers/link_to.rb, line 110
    def relative_path_to(target)
      require 'pathname'

      # Find path
      path = target.is_a?(String) ? target : target.path

      # Get source and destination paths
      dst_path   = Pathname.new(path)
      src_path   = Pathname.new(@item_rep.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

[Validate]

Generated with the Darkfish Rdoc Generator 2.