class Utopia::Content::Link

Represents a link to some content with associated metadata.

Attributes

info[R]
kind[R]
locale[R]
name[R]
path[R]

Public Class Methods

new(kind, name, locale, path, info, title = nil) click to toggle source

@param kind [Symbol] the kind of link.

# File lib/utopia/content/link.rb, line 36
def initialize(kind, name, locale, path, info, title = nil)
        @kind = kind
        @name = name
        @locale = locale
        @path = Path.create(path)
        @info = info || {}
        @title = Trenni::Strings.to_title(title || name)
end

Public Instance Methods

==(other) click to toggle source
# File lib/utopia/content/link.rb, line 131
def == other
        other and kind == other.kind and name == other.name and path == other.path
end
[](key) click to toggle source

Look up from the `links.yaml` metadata with a given symbolic key.

# File lib/utopia/content/link.rb, line 68
def [] key
        @info[key]
end
default_locale?() click to toggle source
# File lib/utopia/content/link.rb, line 135
def default_locale?
        @locale == nil
end
eql?(other) click to toggle source
# File lib/utopia/content/link.rb, line 127
def eql? other
        self.class.eql?(other.class) and kind.eql?(other.kind) and name.eql?(other.name) and path.eql?(other.path) and info.eql?(other.info)
end
full_path(root, extension = XNODE_EXTENSION) click to toggle source
# File lib/utopia/content/link.rb, line 53
def full_path(root, extension = XNODE_EXTENSION)
        if @path&.file?
                File.join(root, @path.dirname, self.key + XNODE_EXTENSION)
        end
end
href() click to toggle source
# File lib/utopia/content/link.rb, line 59
def href
        @href ||= @info.fetch(:uri) do
                @info.fetch(:href) do
                        (@path.dirname + @path.basename).to_s if @path
                end
        end
end
href?() click to toggle source
# File lib/utopia/content/link.rb, line 78
def href?
        !!href
end
index?() click to toggle source
# File lib/utopia/content/link.rb, line 82
def index?
        @kind == :index
end
key() click to toggle source
# File lib/utopia/content/link.rb, line 45
def key
        if locale
                "#{@path.last}.#{@locale}"
        else
                @path.last
        end
end
relative_href(base = nil) click to toggle source
# File lib/utopia/content/link.rb, line 90
def relative_href(base = nil)
        if base and href.start_with? '/'
                Path.shortest_path(href, base)
        else
                href
        end
end
title() click to toggle source
# File lib/utopia/content/link.rb, line 98
def title
        @info.fetch(:title, @title)
end
to_anchor(base: nil, content: self.title, builder: nil, **attributes) click to toggle source
# File lib/utopia/content/link.rb, line 102
def to_anchor(base: nil, content: self.title, builder: nil, **attributes)
        attributes[:class] ||= 'link'
        
        Trenni::Builder.fragment(builder) do |inner_builder|
                if href?
                        attributes[:href] ||= relative_href(base)
                        attributes[:target] ||= @info[:target]
                        
                        inner_builder.inline('a', attributes) do
                                inner_builder.text(content)
                        end
                else
                        inner_builder.inline('span', attributes) do
                                inner_builder.text(content)
                        end
                end
        end
end
Also aliased as: to_href
to_href(base: nil, content: self.title, builder: nil, **attributes)
Alias for: to_anchor
to_s() click to toggle source
# File lib/utopia/content/link.rb, line 123
def to_s
        "\#<#{self.class}(#{self.kind}) title=#{title.inspect} href=#{href.inspect}>"
end
virtual?() click to toggle source
# File lib/utopia/content/link.rb, line 86
def virtual?
        @kind == :virtual
end