# = SiteMap # # Encapsulates the page structure and additional metadata. # # === TODO: # # move out of the UI namespace. # include url remapping functionality. # # === Design # # Unlike the original version, this is presentation agnostic, and # greatly simplified. # # === Todo: # # - support strings as titles. # - add support to read the map from a config file. # - rename to appmap ? # # code: # George Moschovitis # # (c) 2004 Navel, all rights reserved. # $Id: sitemap.rb 71 2004-10-18 10:50:22Z gmosx $ require "n/utils/hash" module N # = SitePage # class SitePage # the uri for this page attr_accessor :uri # the real uri to this page calculated by the page overloader) attr_accessor :real_uri attr_accessor :realm # the shader for this page, overrided the shader calculated # by the engine attr_accessor :shader attr_accessor :level # title of the page attr_accessor :title # description of the page attr_accessor :description attr_accessor :parent, :children attr_accessor :flag # the realm this page belongs to, typically the part name. # put symbols in this variable attr_accessor :realm def initialize(uri, title = nil, parent = nil, realm = nil) @uri, @title, @parent = uri, title, parent # inherit realm from parent if exists if parent and parent.realm @realm = parent.realm else @realm = realm end @children = [] @parent.children << self if parent # gmosx: is this good? # automatically add to the sitemap. $sitemap << self end # Return string representation # def to_s return @title end end # = SiteMap # # === TODO: # Use a second class in the UI namespace for rendering # related stuff? # class SiteMap < N::SafeHash # The root page for this sitemap attr_accessor :root # The separator used when creating paths attr_accessor :separator def initialize(separator = " > ") super @separator = separator end def << (page) self[page.uri] = page unless page.parent @root = page page.realm = :root end end # Path as array of pages # def path(uri) return nil unless uri if page = self[uri] res = Array.new; res << page while page = page.parent res.unshift(page) end return res else $log.warn "The uri #{uri} is not registered in the SiteMap!" end return nil end # Calculates the linked path to the given uri. # def linked_path(uri, lc = nil, args = nil) if the_path = path(uri) i = -1 j, s = 0, the_path.size() return the_path.collect { |p| j += 1 if p.title.is_a?(String) j == s ? p.title : %|#{p.title}| elsif p.title.is_a?(Symbol) j == s ? lc[p.title] : %|#{lc[p.title]}| else i += 1 title, qs = p.title.call(args[i]) j == s ? title : %|#{title}| end }.join(@separator) else return nil end end # Calculates the path to the given uri. # def str_path(uri, lc = nil, args = nil) if the_path = path(uri) i = -1 return the_path.collect { |p| if p.title.is_a?(String) p.title elsif p.title.is_a?(Symbol) lc[p.title] else i += 1 p.title.call(args[i])[0] end }.join(@separator) else return nil end end # Returns a String representation of the Sitemap. # def to_s str = "#{root}" arr = [] self.each_pair { |page, title| arr << "#{title}: #{page}" } return "#{str} {" + arr.join(",") + "}" end end end # module # the default sitemap. $sitemap = N::SiteMap.new