[pmwiki-users] Markup Extension writing
Petko Yotov
5ko at 5ko.fr
Tue Feb 10 02:44:10 PST 2026
This seems to work for me.
I tried your code, and the {(sum)} expression on the Cookbook, with this
markup:
{(sum (strcount are PmWiki.BasicEditing) -6)}
The string "are" appears 16 times in my BasicEditing.
The result is correctly 16 - 6 = 10.
Definition:
$MarkupExpr['sum'] = 'array_sum($args)';
$MarkupExpr['strcount'] as below.
Petko
--
If you upgrade : https://www.pmwiki.org/Upgrades
On 09/02/2026 22:56, Hans Bracker wrote:
> PS: sorry, that code was not quite right. Here it is corrected:
>
> // (strcount <needle> <HayStack>), (strcount <needle>),
> //(strcount <needle> page=<HayStack>),(strcount <needle>
> pages=<Page1,Page2,Page3>)
> $MarkupExpr['strcount'] = 'MxStringCount($pagename, @$argp)';
> function MxStringCount($pn, $argp) {
> $find = $argp['find'] ?? $argp[''][0] ?? '';
> if ($find=='') return '';
> $pgs = $argp['pages'] ?? $argp['page'] ?? $argp[''][1] ?? $pn;
> if (strpos($pgs,',')) $pages = explode(',',$pgs);
> else $pages = array($pgs);
> $cnt = 0;
> foreach ($pages as $p) {
> $sect = strstr($p,'#');
> $name = MakePageName($pn, $p);
> $source = $name.$sect;
> $text = RetrieveAuthSection($pn, $source);
> $cnt += substr_count($text, $find);
> }
> return $cnt;
> }
>
> On 09/02/2026 21:52, Hans Bracker wrote:
>
>> Hi Petko, I am trying to define some Markup Extensions which can be
>> embedded in other MEs for use in calculations. I just wrote one for
>> counting strings. It returns a number. But when it is inside a ME
>> like {(sum (strcount <needle> HaystackPage) 10 )} to add 10 to
>> count number then this fails. The sum ME does not receive the count
>> number from ME strcount, but a keep token.
>>
>> I can only solve this by rewriting the 'sum' ME to use $params
>> instead of $argp and add to the function at the top $p =
>> ParseArgs(MarkupRestore($params));
>> to get the parameters without Keep token. This seems like too much
>> code hacking in the wrong places. Is there a way that my strcount ME
>> can return the value without it being made into a token in the ME
>> process, so other MEs do not need to be hacked?
More information about the pmwiki-users
mailing list