<?php if (!defined('PmWiki')) exit();

####### DESCRIPTION:  The Hg recipe adds Pseudo Hierarchical Groups functionality to PmWiki.  For info, see http://www.pmwiki.org/wiki/Cookbook/Hg.  Author: Dan Vis  <editor àt fast döt st>, Copyright 2007.

####### Set up various variables
$RecipeInfo['Hg']['Version'] = '2007-02-04';
SDV($hgseparator, '-');
SDV($hgmaxlevels, 7);
SDV($hgname, '');
SDV($hggroup, '');
SDV($hgcleanurls, true);
SDV($hglinkprefix, '');

$pagename = hgResolvePageName($pagename);
if($hgcleanurls == true) $FmtPV['$PageUrl'] = 'PUE("$ScriptUrl?n=" . hgUrl($group, $name))';

$FmtPV['$g0'] = "'" . hgCountLevels($hggroup) . "'";
$FmtPV['$n0'] = "'" . hgCountLevels($hgname) . "'";
for ($i=0; $i < $hgmaxlevels; $i++) {
	$hg = "g" . ($i + 1);
	$hn = "n" . ($i + 1);
	$FmtPV['$' . $hg] = "'" . hgSplitName($hggroup, $i) . "'";
	$FmtPV['$' . $hn] = "'" . hgSplitName($hgname, $i) . "'";
	}

####### Set up hierarchical configs, headers, footers, sidebars, attributes & styles
$EnablePGCust = 0;
$hgc = 0; $hgh = 0; $hgf = 0; $hgs = 0; $hga = 0;
if (file_exists("$LocalDir/$hggroup.$hgname.php")) {
	include_once("$LocalDir/$hggroup.$hgname.php");
	$hgc = 1;
	}
$hggroups = explode($hgseparator, $hggroup);
while (count($hggroups) != 0) {
	$hg = implode ($hgseparator, $hggroups);
	if ($hgc == 0 && file_exists("$LocalDir/$hg.php")) {
		include_once("$LocalDir/$hg.php");
		$hgc = 1;
		}
	if ($hgh == 0 && PageExists("$hg.GroupHeader")) {
		$GroupHeaderFmt = "(:include $hg.GroupHeader:)";
		$hgh = 1;
		}
	if ($hgf == 0 && PageExists("$hg.GroupFooter")) {
		$GroupFooterFmt = "(:include $hg.GroupFooter:)";
		$hgf = 1;
		}
	if ($hgs == 0 && PageExists("$hg.Sidebar")) {
		$FmtPV['$sidebar'] = "'" . "$hg.Sidebar" . "'";
		$hgs = 1;
		}
	if ($hga == 0 && PageExists("$hg.GroupAttributes")) {
		$GroupAttributesFmt = "$hg/GroupAttributes";
		$hga = 1;
		}
	$PageCSSListFmt["pub/css/$hg.css"] = "$PubDirUrl/css/$hg.css"; 
	array_pop($hggroups);
	}

####### Set up default configs, headers, footers, sidebars & styles
if ($hgc == 0 && file_exists("$LocalDir/default.php")) include_once("$LocalDir/default.php");
if ($hgh == 0 && PageExists("Site.GroupHeader")) $GroupHeaderFmt = "(:include Site.GroupHeader:)";
if ($hgf == 0 && PageExists("Site.GroupFooter")) $GroupFooterFmt = "(:include Site.GroupFooter:)";
if ($hgs == 0 && PageExists("Site.Sidebar")) $FmtPV['$sidebar'] = "'" . "Site.Sidebar" . "'";
$PageCSSListFmt["pub/css/local.css"] = "$PubDirUrl/css/local.css"; 
$PageCSSListFmt = array_reverse($PageCSSListFmt, true);

function hgCountLevels($name) {
    global $hgseparator;
    $parts = explode($hgseparator, $name);
    return count($parts);
	}

function hgSplitName($name, $ind) {
    global $hgseparator;
    $parts = explode($hgseparator, $name);
    if ($ind < 0 || $ind >= count($parts)) return '';
    else return $parts[$ind];
	}

function hgResolvePageName($pagename) {
	global $hgseparator, $hgname, $hggroup;
	$r = '/';
	if(! strpos($pagename, $r)) {
		$pagename = ResolvePageName($pagename);
		$hggroup = FmtPageName('$Group', $pagename);
		$hgname = FmtPageName('$Name', $pagename);
		return $pagename;
		}
	$hgpagename = explode($r, $pagename);
	if ($hgpagename[count($hgpagename) - 1 ] == '') array_pop($hgpagename);
	$hgname = $hgpagename[count($hgpagename) - 1];
	array_pop($hgpagename);
	$hggroup = implode($hgseparator, $hgpagename);
	$pagename = ResolvePageName("$hggroup.$hgname");
	return "$pagename";
	}

function hgURL($group, $name) {
	global $DefaultName, $hglinkprefix;
	$r = '/';
	if ($group == $name) $name = $DefaultName;
	$link = str_replace("-", $r, $group) . $r . $name;
	return $link;
	}

####### Hierarchical links shortcut notation.
Markup('[[hg','<var','/\\[\\[([\\*|\\^|\\:]+)(.*?)\\]\\]/e', 'hgLinks("$1","$2")');
function hgLinks($x,$y) {
	global $pagename, $hgseparator, $hggroup, $hglinkprefix;
	if ($x == ":") {
		if (! strpos($y, ".")) return "[[$hglinkprefix.$y]]";
		else return "[[$hglinkprefix-$y]]";
		}
	$hggroups = explode($hgseparator, $hggroup);
	$c = strlen($x);
	$i = 0;
	while ($i < $c) {
		if (substr($x, 0, 1) == "*") $link = $link . $hggroups[$i] . $hgseparator;
		if (substr($x, 0, 1) == "^") array_pop($hggroups);
		$i = $i + 1;
		}
	if (substr($x, 0, 1) == "*") $link = substr ($link, 0, -1);
	if (substr($x, 0, 1) == "^") $link = implode($hgseparator, $hggroups);
	return "[[$link$y]]";
	}