class Link
Represents a link to some content with associated metadata.
Definitions
def initialize(kind, name, locale, path, info, title = nil)
Implementation
def initialize(kind, name, locale, path, info, title = nil)
@kind = kind
@name = name
@locale = locale
@path = Path.create(path)
@info = info || {}
@title = XRB::Strings.to_title(title || name)
end
def key
Build the filesystem key, including the locale suffix when present.
Signature
-
returns
String | Nil The link key.
Implementation
def key
if @path
if locale
"#{@path.last}.#{@locale}"
else
@path.last
end
end
end
def full_path(root, extension = XNODE_EXTENSION)
Resolve the backing content file beneath a root directory.
Signature
-
parameter
rootString The root directory.
-
parameter
extensionString The file extension.
-
returns
String | Nil The backing file path, or
nilfor links without file paths.
Implementation
def full_path(root, extension = XNODE_EXTENSION)
if @path&.file?
File.join(root, @path.dirname, self.key + XNODE_EXTENSION)
end
end
def href
Resolve this link's target URI.
Signature
-
returns
String | Nil The explicit target URI or one derived from the content path.
Implementation
def href
@href ||= @info.fetch(:uri) do
@info.fetch(:href) do
# Omit the variant suffix from inferred content links:
(@path.dirname + @path.basename).to_url_path.encoded if @path
end
end
end
def [](key)
Look up from the links.yaml metadata with a given symbolic key.
Implementation
def [] key
@info[key]
end
def href?
Check whether this link has a target URI.
Signature
-
returns
Boolean Whether this link has a target URI.
Implementation
def href?
!!href
end
def index?
Check whether this link refers to an index document.
Signature
-
returns
Boolean Whether this link represents an index document.
Implementation
def index?
@kind == :index
end
def virtual?
Check whether this link is virtual.
Signature
-
returns
Boolean Whether this link exists only in metadata.
Implementation
def virtual?
@kind == :virtual
end
def relative_href(base = nil)
Resolve this link's target relative to a base path.
Signature
-
parameter
basePath | String | Nil The source path.
-
returns
String | Nil The relative or unchanged target.
Implementation
def relative_href(base = nil)
target = href
return unless target
return target unless base
url = Protocol::URL[target]
relative_url = url.relative_to(Path[base].to_url_path)
# Preserve unchanged targets exactly and avoid allocating a serialized replacement:
if relative_url.equal?(url)
return target
end
return relative_url.to_s
end
def title
Return the display title from metadata or the inferred title.
Signature
-
returns
String The display title.
Implementation
def title
@info.fetch(:title, @title)
end
def to_anchor(base: nil, content: self.title, builder: nil, **attributes)
Render this link as an anchor element.
Signature
-
parameter
basePath | String | Nil The source path for relative links.
-
parameter
contentString The content.
-
parameter
builderXRB::Builder The markup builder.
-
parameter
attributesHash The attributes.
-
returns
XRB::Builder::Fragment The rendered anchor or span fragment.
Implementation
def to_anchor(base: nil, content: self.title, builder: nil, **attributes)
attributes[:class] ||= "link"
XRB::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
def to_s
Convert this object to a string.
Signature
-
returns
String The resulting string.
Implementation
def to_s
"\#<#{self.class}(#{self.kind}) title=#{title.inspect} href=#{href.inspect}>"
end
def eql?(other)
Check whether this object is equivalent to another object.
Signature
-
parameter
otherObject The object to compare.
-
returns
Boolean Whether both links have equivalent metadata.
Implementation
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
def ==(other)
Compare this object with another object.
Signature
-
parameter
otherObject The object to compare.
-
returns
Boolean | Nil Whether both links have the same kind, name, and path.
Implementation
def == other
other and kind == other.kind and name == other.name and path == other.path
end
def default_locale?
Check whether this link uses the default locale.
Signature
-
returns
Boolean Whether this link has no locale override.
Implementation
def default_locale?
@locale == nil
end