[pmwiki-users] cookbook "ShellTools" (was: Include specific lines of text on a page)

Peter & Melodye Bowers pbowers at pobox.com
Sun Jan 20 12:23:57 CST 2008


Based entirely on Hans' work on grep, I've put together a kludge of what
tail would look like (see at the bottom of this post).  This will
theoretically solve bialy663's earlier question although the n=10 argument
problem needs to be solved.

I much prefer swapping the option/filepattern order as options can be
relatively easily delineated from filepatterns -- then anything coming after
the last option is automatically called a filepattern and then there is the
option of having multiple filename/patterns.  Unfortunately my PHP ability
is just above nil and so I'm groping in the dark in terms of how to process
the arguments and etc...

(When I put n=5 as the argument it somehow doesn't even appear in $args[0]
-- how is that possible?)

Many of the shell tools follow this same basic pattern and so obviously a
lot of the early processing would want to be made into functions to process
the arguments, figure out a list of files, etc.

There's another interesting question to consider...  Should option names,
tool names, etc reflect their shell equivalents or should they be invented
from scratch?  Those with a linux background would find the transition much
eased by re-using the linux equivalents, but maybe working in the pmwiki
context maybe we should be maintaining consistency with a different
standard...

One final question regarding the use of these MarkupExpressions: How does
the nesting work?  Let's say I want to "tail" the result of a "grep" -- how
does that happen?  (I would play with it, but I'm having too much trouble
with the arguments to be able to figure it out with any confidence...)

-Peter

===(snip)===
# {(tail PAGEPATTERN OPTIONS)} 
# Options: 
# n=number - display n lines from the end of the file
# PAGEPATTERN - source pages from PageName or Group.Pagename 
# 		allowing wiki wildcards * and ?
$MarkupExpr['tail'] = 'Tail($pagename, @$args[0], @$args[1])'; 
function Tail($pagename, $opt, $src) 
{
	echo "Tail: opt=$opt, src=$src<br>\n";
        if ($src=='') return '';
	$n = 10;
	if (preg_match("/n=(\d*)/", $opt, $m)) $n = $m[1];
	echo "Tail: n=$n <br>\n";
        $grp = PageVar($pagename, '$Group');
        //check for group.name pattern
        if (strstr($src,'.')) 
                $pat = $src;
        else $pat = $grp.".".$src;
        //make preg pattern from wildcard pattern
        $prpat = GlobToPCRE(FixGlob($pat));
        //make list from preg name pattern
        $sourcelist =   ListPages("/$prpat[0]/");
        $newrows = array();
        //process each source page in turn
        foreach($sourcelist as $source) {
                if ($source==$pagename) continue;
                $page = RetrieveAuthPage($source, 'read', true);
                if ($page) {
                        $m = 0;
                        $text = $page['text'];
                        $textrows = explode("\n",$text);
			echo "Count=" . count($textrows) . "<br>\n";
			for ($i = count($textrows)-$n; $i <=
count($textrows); $i++) {
				echo "i=$i, row=$textrows[$i]<br>\n";
				$newrows[] = $textrows[$i];
                        }
                }
        }
        return implode("\\\\\n", $newrows);
}
===(snip)===

> -----Original Message-----
> From: Hans [mailto:design5 at softflow.co.uk]
> Sent: Sunday, January 20, 2008 1:31 PM
> To: Peter & Melodye Bowers
> Cc: pmwiki-users at pmichaud.com
> Subject: Re: [pmwiki-users] cookbook "ShellTools" (was: Include specific
> lines of text on a page)
> 
> Sunday, January 20, 2008, 11:58:03 AM, Peter & Melodye Bowers wrote:
> 
> > The initial suggestion is this:
> 
> > (:grep "regex" filename/pattern ...")
> 
> I did some more work, and here is some code for a working grep markup
> expression. Please try it. I will put it into a cookbook recipe, once
> I know the markup syntax etc is okay.
> 
> Text pattern can be a word or a regex expression.
> Cut pattern similar with cut=PATTERN option.
> Page pattern is  a PmWiki page name, allowing wildcards.
> The fmt= option works right now for fmt=link, which will place a
> pagelink on the line above the text extract, and a space afterwards.
> This will look more like a searchresult with text extract.
> More could be done there no doubt.
> There is no attempt yet to restrict procesing time. This markup
> expression can be expensive when processing lots of pages, since each
> page will be opened.
> 
> Suggestions most welcome!
> 
> Add to config.php or create new cookbook php file and include it in
> config.php:
> 
> # {(grep TEXTPATTERN PAGEPATTERN OPTIONS)}
> # TEXTPATTERN - display lines matching regex pattern
> # PAGEPATTERN - source pages from PageName or Group.Pagename allowing wiki
> wildcards * and ?
> # Otions:
> # fmt=link - display page link above extract
> # cut=PATTERN - do not display lines matching PATTERN
> $MarkupExpr['grep'] = 'Grep($pagename, @$args[0], @$args[1], @$argp)';
> function Grep($pagename, $expr, $src, $opt = NULL) {
>         if ($expr=='' || $src=='') return '';
>         $fmt = (@$opt['fmt'] ? $opt['fmt'] : '');
>         $cut = (@$opt['cut'] ? $opt['cut'] : '');
>         $grp = PageVar($pagename, '$Group');
>         //check for group.name pattern
>         if (strstr($src,'.'))
>                 $pat = $src;
>         else $pat = $grp.".".$src;
>         //make preg pattern from wildcard pattern
>         $prpat = GlobToPCRE(FixGlob($pat));
>         //make list from preg name pattern
>         $sourcelist =   ListPages("/$prpat[0]/");
>         $newrows = array();
>         //process each source page in turn
>         foreach($sourcelist as $source) {
>                 if ($source==$pagename) continue;
>                 $page = RetrieveAuthPage($source, 'read', true);
>                 if ($page) {
>                         $m = 0;
>                         $text = $page['text'];
>                         $textrows = explode("\n",$text);
>                         foreach ($textrows as $row) {
>                                 if (preg_match("/\\(:/", $row)) continue;
>                                 //check each row for $expr text pattern,
> exclude $cut pattern
>                                 if (preg_match("/($expr)/", $row) &&
> ($cut=='' || !preg_match("/($cut)/", $row))) {
>                                         if($fmt=='link' && $m==0)
> $newrows[] = "[[$source]]";
>                                         $newrows[] = $row;
>                                         $m = 1;
>                                 }
>                         }
>                 }
>                 if ($fmt=='link' && $m==1) $newrows[] = "&nbsp;";
>         }
>         return implode("\n", $newrows);
> }
> 
> 
> 
> 
>   ~Hans




More information about the pmwiki-users mailing list