[pmwiki-users] automatic Trail with random back & next

Hans design5 at softflow.co.uk
Mon Jul 21 11:55:35 CDT 2008

Monday, July 21, 2008, 1:54:53 PM, Stéphane Heckel wrote:

> Looking for a way to implement an automatic random trail that would look
> like

> << Title of random page X | Groupname | Title of random page Y >>

> X & Y are 2 pages, selected from "Groupname"

> Each time you refresh the page, X & Y would change, ...

> Is there any cookbook I could look at ?

you could try this markup expression definition to construct what you
want. Add to config.php

## {(rndpage label trailpagename group=GroupName)}
$MarkupExpr['rndpage'] = 'MakeRandomNext($pagename, $args[0], $args[1], $argp)';
function MakeRandomNext($pagename, $label, $trailname='', $args) {
        $trail = array();
        //if no trailname make trail from all group pages
        if ($trailname=='') {
                if (isset($args['group'])) $group = $args['group'];
                else $group = PageVar($pagename, '$Group');
                $gplist = ListPages("/^$group\\..+/");
                foreach($gplist as $i => $p)
                        $trail[$i] = $p;
        else  $trail = ReadTrail($pagename, $trailname);
        $cnt = count($trail);
        $i = rand(0, $cnt);
        $pn = $trail[$i];
        if ($label=='title') $label = '+';
        if ($label=='') $label = 'next';        
        if ($label=='name') $label = PageVar($pn, '$Name'); 
        return "[[".$pn."|".$label."]]";

This will give you a markup expression {(rndpage)}, which return a
link to a random page in the curent group. The link will be labelled

{(rndpage 'label text')} will return a random pagelink labeled 'label

{(rndpage title)} will return a random page link labelled with th
title of the page.

{(rndpage name)} will return a random page link with the name of the
page as label.

{(rndpage label TrailPageName)} will return a random page link from
links of the trail on TrailPageName.

{(rndpage label group=GroupName)} will select random page link from
pages of group 'GroupName'.

For your trail you could just use two of these mrkup expressions:

<< {(rndpage title)} [[{$Group}/]] {(rndpage title)} >>

It is possible that both links shown will link to the same page, the
markup expression function does not keep count on names used already.

I might add this expression to the powertools recipe. I modified the
powertools {(trail )} expression to create this {(rndpage)}


More information about the pmwiki-users mailing list