[pmwiki-users] automatic link creation

Peter Bowers pbowers at pobox.com
Thu Aug 27 09:00:21 CDT 2009


On Wed, Aug 26, 2009 at 11:24 PM, adam overton<a at plus1plus1plus.org> wrote:
> # all keys must be completely lowercase in order for this to work
> $NameToLinkArray = array(
> 'john doe' => 'http://www.johndoe.com',
> 'sally struthers' => 'http://www.def.com'
> );
> Markup('names', 'inline',
> '/(`?)\b(' . join('|', array_keys($NameToLinkArray)) . ')\b/ei',
> 'AutoNameToLinkFunc("$2","$1")'
> );
> function AutoNameToLinkFunc($name, $escape) {
> global $NameToLinkArray;
> if ($escape) return $name;
> $link =
> '%newwin%[['.$NameToLinkArray[strtolower($name)].'|'.Keep($name).']]';
> return $link;
> }
> * i had to substitute the AutoNameToLinkFunc because i couldn't figure out
> how to get pmwiki to find $NameToLinkArray - i got errors, and had to resort
> to calling it as a global in a function. perhaps someone knows how to
> finagle this without having to declare a separate function, as PM did in his
> recent example?

Use the $GLOBALS super-global, thus $GLOBALS['NameToLinkArr...']

> question/next step:
> is there a way to set up my list of names on a wiki page (like
> Site.NamesToLinks), like InterMap is setup?

Just set up your page using one link-pair per line and some delimiter
(colon?) between the name and the link.  Then read in the page and
parse the $page['text'] into an array:

$AutoLinkPage = "SiteAdmin.AutoLink";
$page = ReadPage($AutoLinkPage);
$NameToLinkArray = array();
foreach (explode("\n", $page['text']) as $line) {
   if (!strpos($line, '|')) continue;
   $foo = explode("|",$line);
echo "<p>foo=".print_r($foo,true).", count=".count($foo)."<p><br>\n";
   if (count($foo) == 2)
       $NameToLinkArray[strtolower($foo[0])] = $foo[1];
}
echo "<p>N2LA=".print_r($NameToLinkArray,true)."<p><br>\n";
if ($NameToLinkArray)
    Markup('names', 'inline',
    '/(?<!\`)\b(' . join('|', array_keys($NameToLinkArray)) . ')\b/ei',
    '"%newwin%[[".$GLOBALS["NameToLinkArray"][strtolower("$1")]."|".Keep("$1")."]]"'
    );

-Peter



More information about the pmwiki-users mailing list