# COPYRIGHT: 2007 Andrew Neil (andrew.jr.neil@googlemail.com) # LICENSE: MIT require 'fileutils' require 'pp' module NavigationHelper @@pages = [] @@page_index = {} @@current_page = nil INDEX_PATTERN = /index\.(haml|html)$/i STRING_PATTERN = /('(\w|\s)*')|("(\w|\s)*")/ FIXNUM_PATTERN = /-?\d+/ LINK_TEXT = "link_text" NAVIGATION_INDEX = "nav_index" DEFAULT_NAV_INDEX = 0 INDEX_CHOP = true EXTENSION_CHOP = true DIV_WRAP = true # non english sites may want to override "home" as they see fit. HOME = "home" # constants used in building navigation BREAD_SEPARATORS = true CURRENT_AS_SPAN = true # When false, the current item in a navigation is a hyperlink to current page (duh!) @@breadcrumb = false #when true, only ancestors are displayed @@start_depth = 1 @@descendants = 0 @@separator = "»" # Builds a breadcrumb trail def breadcrumbs(separator="»", start=0) @@separator=separator if DIV_WRAP tag(:div, :class => "breadcrumbs"){ navigation(start, false, true) } else navigation(start, false, true) end end # Starting at the site root, builds a fully expanded sitemap def sitemap if DIV_WRAP tag(:div, :class => "sitemap"){ navigation(0, false) } else navigation(0, false) end end # Displays the top level only (i.e. contents of root, without "Home" link) def topnav if DIV_WRAP tag(:div, :class => "topnav"){ navigation(1,0) } else navigation(1,0) end end # In conjunction with topnav, this expands to a context-relevant navigation below top def subnav if DIV_WRAP tag(:div, :class => "subnav"){ navigation(2,1) } else navigation(2,1) end end # create a default navigation list, wrapped in a div.navigation def nav if DIV_WRAP tag(:div, :class => "navigation"){ navigation } else navigation end end # Create a navigation list, by default expanding to show the contents of any folders def navigation(start=1, descendants=1, breadcrumb=false) setup reset(start, descendants, breadcrumb) nav = unordered_list(navigation_origin) reset return nav end # ======================================================================================== private # ======================================================================================== # scan the /src/pages directory for all pages, # building @@pages list (where order is significant) # and @@page_index for easy retreival of any page by its relative path def setup if @@current_page != relative_path_of_current_page @@current_page = relative_path_of_current_page @@pages = scan_directory(File.join(@staticmatic.src_dir, "pages", "*")) cur_page = @@page_index[@@current_page] if cur_page cur_page.set_current mark_ancestors(cur_page) end end end # Ensure that default values are restored after using any helper methods by calling this def reset(start=1, descendants=1, breadcrumb=false) @@breadcrumb = breadcrumb @@start_depth = start @@descendants = descendants end # determine the starting point for constructing a navigation. # If given start_depth = 0 or 1, start with root folder or its contents # start_depth >= 2 must find the origin w.r.t. the current page. def navigation_origin if @@start_depth == 0 origin = [@@pages] elsif @@start_depth == 1 origin = @@pages.child else origin = find_origin(@@page_index[@@current_page]) end end def find_origin(page) if page.depth == @@start_depth return page.siblings elsif page.depth > @@start_depth return find_origin(page.parent) else page.child.each do |ch| return find_origin(ch) end return nil end end # create a