ProjectSource

Utopia::Content::Links

Nested

Definitions

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