, Copyright 2007. * see http://www.pmwiki.org/wiki/Cookbook/Hg * Modified by: Kathryn Andersen * Further mods by Hans Bracker * An mangled again by Feral, 2007-03-20 */ $RecipeInfo['Cluster']['Version'] = '2007-03-20'; /* -[Feral:078/07@18:04]--------------------------------------------------- * Feral's Running list of changes * * *[@ClusterBreadCrumb()@] * **Changed ''level'' param to @@left@@; * **Added @@right@@ param. * ***These both will trim the count levels off of the trail; * ****For the ever popular Kingdom-Animal-Canine.pagename * *****left=-1 to drop the first item (Kingdom) resulting in Animal-Canine * *****right=-1 to drop the last item (Canine) resulting in Kingdom-Animal * **@@title@@'s value has meaning: * ***=0 for Do not show the title; use the base name. * ***=1 for Show the title and run it though via [@$AsSpacedFunction@]. * ***=2 for Show the title. * **This method works with groups with or without titles; Those groups that have a title will display that, those groups without will show just their crumb name, I.e. Canine, rather than Kingdom-Animal-Canine * **Note that group titles can come from: * ***[@breadcrumb_group.GroupAttributes@] -- I.e. @@Kingdom-Animal.GroupAttributes@@ * ***[@breadcrumb_group.GroupHeader@] -- I.e. @@Kingdom-Animal.GroupHeader@@ * ***[@breadcrumb_group.GroupFooter@] -- I.e. @@Kingdom-Animal.GroupFooter@@ * ***[@breadcrumb_group.''breadcrumb_group''@] -- I.e. @@Kingdom-Animal.Kingdom-Animal@@ * ***[@breadcrumb_group.$DefaultName@] -- I.e. @@Kingdom-Animal.HomePage@@ * * *I have rewritten [@ClusterGroupTitle()@] into something that does the same thing in a different way. It now functions similar to [@ClusterBreadCrumb()@], which is to say you can use it as directive or page var; It also functions like I think it should. * **Params: * ***space = @@true@@ or @@false@@; Should the results be spaced (Ran though [@$AsSpacedFunction@]); @@false@@ is default. * ***index = @@last@@ or numerical index to denote which group we care about; @@last@@ is default (I.e. Kingdom-Animal-Canine = Canine) * ***onebasedindex = @@true@@ or @@false@@; Determins if @@index@@ starts at 0 (false) or 1 (true); * *NOTE: * **Someone '''please'' say if you want to keep [@{$g0}@],[@{$g1}@] ... [@{$g7}@] and [@{$n0}@],[@{$n1}@] ... [@{$n7}@], as I do not see their point and, to me, they seem like legacy code suitable to be pruned from future releases. * * * --------------------------------------------------------------------- */ #==== Customisation Variables for use in local config file ====== SDV($ClusterSeparator, '-'); SDV($ClusterMaxLevels, 7); SDV($ClusterBreadCrumbSeparator, " $ClusterSeparator "); SDV($EnableBreadCrumbName, 1); SDV($EnableBreadCrumbTitles, 0); /* ================================================================ * Page Variables */ $FmtPV['$BreadCrumb'] = 'ClusterBreadCrumb($pagename, "")'; $FmtPV['$BreadCrumbTitle'] = 'ClusterBreadCrumb($pagename, "title=1")'; $FmtPV['$GroupTitle'] = 'ClusterGroupTitle($pagename, "")'; $FmtPV['$GroupTitleSpaced'] = 'ClusterGroupTitle($pagename, "space=true index=last")'; $FmtPV['$ClusterSideBar'] = 'ClusterPageName($group, "SideBar")'; /* ================================================================ * Markup for link shortcuts */ Markup('[[cluster','= count($parts)) return 'null'; else return $parts[$ind]; } # return the pagename of the closest ancestor function ClusterPageName($group, $name) { global $ClusterSeparator; global $SiteGroup; $groups = explode($ClusterSeparator, $group); while (count($groups) != 0) { $cluster_group = implode ($ClusterSeparator, $groups); if (PageExists("$cluster_group.$name")) { // short-circuit return! return "$cluster_group.$name"; } array_pop($groups); } return "$SiteGroup.$name"; } function ClusterLinks($pagename, $prefix, $inlink) { global $ClusterSeparator; $group = FmtPageName('$Group', $pagename); if ($prefix == "-") { return "[[$group$ClusterSeparator$inlink]]"; } $groups = explode($ClusterSeparator, $group); $c = strlen($prefix); $i = 0; while ($i < $c) { if (substr($prefix, 0, 1) == "*") $link = $link . $groups[$i] . $ClusterSeparator; if (substr($prefix, 0, 1) == "^") array_pop($groups); $i = $i + 1; } if (substr($prefix, 0, 1) == "*") $link = substr ($link, 0, -1); if (substr($prefix, 0, 1) == "^") $link = implode($ClusterSeparator, $groups); return "[[$link$inlink]]"; } # make a "breadcrumb trail" of group links function ClusterBreadCrumb($pagename, $opt) { // {{{ // For $usetitles = true; // In pmwiki.php: $PagePathFmt = array('{$Group}.$1','$1.$1','$1.{$DefaultName}'); global $DefaultName; global $AsSpacedFunction; global $ClusterSeparator, $ClusterBreadCrumbSeparator, $EnableBreadCrumbName, $EnableBreadCrumbTitles, $FmtPV; $defaults = array( 'left' => 0, // Num elements to remove from left side of group 'right' => 0, // Num elements to remove from right side of group 'separator' => $ClusterBreadCrumbSeparator, 'title' => $EnableBreadCrumbTitles, 'name' => $EnableBreadCrumbName, ); $opt = array_merge($defaults, ParseArgs($opt)); $group = PageVar($pagename,'$Group'); $groups_orig = explode($ClusterSeparator, $group); $groups = array(); $index = 0; foreach($groups_orig as $item) { $path = ''; for ($num = 0; $num <= $index; $num++) { if ( $num != 0) { $path.= $ClusterSeparator; } $path.= $groups_orig[$num]; } $groups[] = array('name' => $item, 'path' =>$path); $index++; } unset($groups_orig); # Remove right side first; $trim = abs($opt['right']); if ( $trim >= count($groups) ) { $trim = count($groups)-1; } $index = 0; while ($index < $trim) { array_pop($groups); $index++; } # Remove left side second, but leave at least 1 element (right most); $trim = abs($opt['left']); if ( $trim >= count($groups) ) { $trim = count($groups)-1; } $index = 0; while ($index < $trim) { array_shift($groups); $index++; } $index = 0; foreach($groups as $item) { if ( $index != 0) { $out.= $opt['separator']; } $out.= '[['; $out.= $item['path'].'/|'; if ( $opt['title']) { // $pagepossibles = array("{$item['path']}.{$item['path']}","{$item['path']}.$DefaultName"); $pagepossibles = array( "{$item['path']}.GroupAttributes", "{$item['path']}.GroupHeader", "{$item['path']}.GroupFooter", "{$item['path']}.{$item['path']}", "{$item['path']}.$DefaultName", ); $path_name = ''; foreach($pagepossibles as $try) { if ( PageExists($try) ) { $page = ReadPage($try, READPAGE_CURRENT); if ( $page['title']) { $path_name = $page['title']; break; } } } if ( !$path_name) { if ( $opt['title']==1) { $path_name = $AsSpacedFunction($item['name']); } else { $path_name = $item['name']; } } $out.= $path_name; } else { $out.= $groups[$index]['name']; } $out.= ']]'; $index++; } # Page Name if ( $opt['name']) { $out .= $opt['separator']."[[$pagename|+]]"; } return $out; } // }}} function ClusterGroupTitle($pagename, $opt) { // {{{ global $ClusterSeparator; global $AsSpacedFunction; $defaults = array( 'space' => false, 'index' => 'last', 'onebasedindex' => true, ); $opt = array_merge($defaults, ParseArgs($opt)); $group = PageVar($pagename,'$Group'); $groups_orig = explode($ClusterSeparator, $group); $groups = array(); $index = 0; foreach($groups_orig as $item) { $path = ''; for ($num = 0; $num <= $index; $num++) { if ( $num != 0) { $path.= $ClusterSeparator; } $path.= $groups_orig[$num]; } $groups[] = array('name' => $item, 'path' =>$path); $index++; } unset($groups_orig); $index_wanted = $opt['index']; if ( $index_wanted == 'last') { $group_piece = array_pop($groups); } else { // [Feral:079/07@02:59] we presume $index_wanted is a number; convert it to such. $index_wanted=abs($index_wanted); if ( $opt['onebasedindex']) { $index_wanted--; } if ( $index_wanted < count($groups) ) { $group_piece = $groups[$index_wanted]; } else { // [Feral:079/07@03:13] out of bounds; *shrug* return null; } } $pagepossibles = array( "{$group_piece['path']}.GroupAttributes", "{$group_piece['path']}.GroupHeader", "{$group_piece['path']}.GroupFooter", "{$group_piece['path']}.{$group_piece['path']}", "{$group_piece['path']}.$DefaultName", ); $group_title=null; foreach($pagepossibles as $try) { if ( PageExists($try) ) { $page = ReadPage($try, READPAGE_CURRENT); if ( $page['title']) { $group_title = $page['title']; break; } } } if ( !$group_title) { $group_title = $group_piece['name']; } if ( $opt['space']) { $group_title = $AsSpacedFunction($group_title); } return $group_title; } // }}}