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

Hans design5 at softflow.co.uk
Sun Jan 20 06:31:19 CST 2008


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[] = " ";
        }
        return implode("\n", $newrows);
}




  ~Hans




More information about the pmwiki-users mailing list