class Links
A collection of links resolved from the filesystem content hierarchy.
Nested
Definitions
def self.for(root, path, locale = nil)
Build links for the given root and path.
Signature
-
parameter
rootString The root directory.
-
parameter
pathUtopia::Path | String The path.
-
parameter
localeString The locale.
-
returns
Link | Nil The resolved link.
Implementation
def self.for(root, path, locale = nil)
warn "Using uncached links metadata!"
self.new(root).for(path, locale)
end
def for(path, locale = nil)
Resolve a link for the specified path, which must be a path to a specific link. for(Path["/index"])
Implementation
def for(path, locale = nil)
links(path.dirname).lookup(path.last, locale)
end
def self.index(root, path, **options)
Build an index of links for the given path.
Signature
-
parameter
rootString The root directory.
-
parameter
pathUtopia::Path | String The path.
-
parameter
optionsHash The options.
-
returns
Array(Link) The indexed links.
Implementation
def self.index(root, path, **options)
warn "Using uncached links metadata!"
self.new(root).index(path, **options)
end
def index(path, name: nil, locale: nil, display: :display, sort: :order, sort_default: 0, directories: true, files: true, virtuals: true, indices: false)
Give an index of all links that can be reached from the given path.
Implementation
def index(path, name: nil, locale: nil, display: :display, sort: :order, sort_default: 0, directories: true, files: true, virtuals: true, indices: false)
ordered = links(path).ordered.dup
# Ignore specific kinds of links:
ignore = []
ignore << :directory unless directories
ignore << :file unless files
ignore << :virtual unless virtuals
ignore << :index unless indices
if ignore.any?
ordered.reject!{|link| ignore.include?(link.kind)}
end
# Filter links by display key:
if display
ordered.reject!{|link| link.info[display] == false}
end
# Filter links by name:
if name
# We use pattern === name, which matches either the whole string, or matches a regexp.
ordered.select!{|link| name === link.name}
end
# Filter by locale:
if locale
locales = {}
ordered.each do |link|
if link.locale == locale
locales[link.name] = link
elsif link.locale == nil
locales[link.name] ||= link
end
end
ordered = locales.values
end
# Order by sort key:
if sort
# Sort by sort_key, otherwise by title.
ordered.sort_by!{|link| [link[sort] || sort_default, link.title]}
end
return ordered
end
def initialize(root, extension: XNODE_EXTENSION)
Initialize cached link resolution beneath a content root.
Signature
-
parameter
rootString The root directory.
-
parameter
extensionString The file extension.
Implementation
def initialize(root, extension: XNODE_EXTENSION)
@root = root
@extension = extension
@file_filter = /\A(?<key>(?<name>[^.]+)(\.(?<locale>.+))?)#{Regexp.escape extension}\Z/
@index_filter = /\A(?<key>(?<name>index)(\.(?<locale>.+))?)#{Regexp.escape extension}\Z/
@metadata_cache = Concurrent::Map.new
@links_cache = Concurrent::Map.new
end
def metadata(path)
Load and cache metadata for a content directory.
Signature
-
parameter
pathUtopia::Path | String The path.
-
returns
Hash The content metadata.
Implementation
def metadata(path)
@metadata_cache.fetch_or_store(path.to_s) do
load_metadata(path)
end
end
def links(path)
Load and cache the link resolver for a content directory.
Signature
-
parameter
pathUtopia::Path | String The path.
-
returns
Resolver The link resolver.
Implementation
def links(path)
@links_cache.fetch_or_store(path.to_s) do
load_links(path)
end
end