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

    This script implements an alternative page naming scheme, suitable 
    for sites which space wiki words. It turns spaces into underscores, 
    rather than turning references into WikiWords. By default, WikiWord 
    references produce spaced page names, so WikiWord becomes a page 
    called Wiki_Word. [[ ... ]] references are processed 'as is', so 
    [[Apple iPod]] becomes a page called Apple_iPod. Underscores are 
    suppressed on display. Only names are treated this way; group
    references are not affected. An author can suppress wikiword
    spacing by writing [[WikiWord]] or, as a shortcut, @WikiWord.
    
    The script makes it easy to give pages natural names, such as
    [[War and Peace]] or [[Novell NetWare]] -- and it 'just works'.
    
    Copyright 2004, 2005 John Rankin (john.rankin@affinity.co.nz)
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    
*/
## unspaced wikilinks
Markup('@wikilink','>urllink',"/@\\b($GroupPattern([\\/.]))?($WikiWordPattern)/e",
  "Keep(WikiLink(\$pagename,'$1'.'$3'),'L')");

## bare wikilinks with spaced wikiwords
Markup('wikilink','>@wikilink',"/\\b($GroupPattern([\\/.]))?($WikiWordPattern)/e",
  "Keep(WikiLinkSpaced(\$pagename,'$0'),'L')");

$AsSpacedFunction = 'ULAsSpaced';
$MakePageNameFunction = 'MakeULPageName';
$DefaultName = 'Home_Page';
$AltDefaultName = 'HomePage';
Markup('${DN}','<${var}','/{\$(DefaultName)}/e',"ULAsSpaced(\$GLOBALS['$1'])");
$DefaultPage = "$DefaultGroup.$AltDefaultName";
$PagePathFmt = array('$Group.$1','$Group.$2',
                     '$1.$1','$2.$1','$2.$2',
                     '$1.$DefaultName','$2.$DefaultName',
                     '$1.$AltDefaultName');

function ULAsSpaced($text) {
  return str_replace('_',' ',$text);
}

function MakeULPageName($basepage,$x) {
  global $PageNameChars,$PagePathFmt;
  SDV($PageNameChars,'-[:alnum:]');
  if (!preg_match('/(?:([^.\\/]+)[.\\/])?([^.\\/]+)$/',$x,$m)) return '';
  $name=ucfirst(str_replace(' ','_',trim(preg_replace("/[^$PageNameChars]+/",' ',$m[2]))));
  $altname=str_replace(' ','',
    preg_replace('/\\b(\\w)/e',"strtoupper('$1')",
      str_replace('_',' ',$name)));
  if ($m[1]) {
    $group = str_replace(' ','',
      preg_replace('/\\b(\\w)/e',"strtoupper('$1')",
        preg_replace("/[^$PageNameChars]+/",' ',$m[1])));
    $pn = "$group.$altname";
    if (PageExists($pn)) return $pn;
    return "$group.$name";
  }
  foreach((array)$PagePathFmt as $pg) {
    $pn = FmtPageName(str_replace('$1',$name,
    str_replace('$2',$altname,$pg)),$basepage);
    if (PageExists($pn)) return $pn;
  }
  $group=preg_replace('/[\\/.].*$/','',$basepage);
  return "$group.$name";
}

function WikiLinkSpaced($pagename,$word) {
  global $AsSpacedFunction, $SpaceWikiWordsFunction;
  $oAsSpacedFunction = $AsSpacedFunction;
  $AsSpacedFunction = $SpaceWikiWordsFunction;
  $r = WikiLink($pagename,$word);
  $AsSpacedFunction = $oAsSpacedFunction;
  return $r;
}

?>