Re: Markup Extension writing
Hans Bracker <[email protected]> Mon, 9 Feb 2026 21:56:45 +0000
| Newsgroups | gmane.comp.web.wiki.pmwiki.user |
|---|---|
| Message-ID | <[email protected]> |
This is a multi-part message in MIME format.
--===============6584925320786966044==
Content-Type: multipart/alternative;
boundary="------------XkINhOPczXqLZocpVHW0okap"
Content-Language: en-GB
This is a multi-part message in MIME format.
--------------XkINhOPczXqLZocpVHW0okap
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
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?
>
> This is the new strcount markup expression:
>
> // (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($name,'#');
> $name = MakePageName($pn, $p);
> $source = $name.$sect;
> $text = RetrieveAuthSection($pn, $source);
> $cnt += substr_count($text, $find);
> }
> return $cnt;
> }
>
> thanks! Hans
>
> _______________________________________________
> pmwiki-users mailing list
> [email protected]
> http://www.pmichaud.com/mailman/listinfo/pmwiki-users
--------------XkINhOPczXqLZocpVHW0okap
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>PS: sorry, that code was not quite right. Here it is corrected:</p>
<p>// (strcount <needle> <HayStack>), (strcount
<needle>), <br>
//(strcount <needle> page=<HayStack>),(strcount
<needle> pages=<Page1,Page2,Page3>) <br>
$MarkupExpr['strcount'] = 'MxStringCount($pagename, @$argp)';<br>
function MxStringCount($pn, $argp) {<br>
$find = $argp['find'] ?? $argp[''][0] ?? '';<br>
if ($find=='') return '';<br>
$pgs = $argp['pages'] ?? $argp['page'] ?? $argp[''][1] ?? $pn;<br>
if (strpos($pgs,',')) $pages = explode(',',$pgs); <br>
else $pages = array($pgs);<br>
$cnt = 0;<br>
foreach ($pages as $p) { <br>
$sect = strstr($p,'#'); <br>
$name = MakePageName($pn, $p); <br>
$source = $name.$sect;<br>
$text = RetrieveAuthSection($pn, $source);<br>
$cnt += substr_count($text, $find);<br>
}<br>
return $cnt;<br>
}</p>
<div class="moz-cite-prefix">On 09/02/2026 21:52, Hans Bracker
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:[email protected]">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<p><font size="2">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. </font></p>
<p><font size="2">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));<br>
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?</font></p>
<p><font size="2">This is the new strcount markup expression:</font></p>
<p><font size="2">// (strcount <needle> <HayStack>),
(strcount <needle>), <br>
//(strcount <needle> page=<HayStack>),(strcount
<needle> pages=<Page1,Page2,Page3>) <br>
$MarkupExpr['strcount'] = 'MxStringCount($pagename, @$argp)';<br>
function MxStringCount($pn, $argp) {<br>
$find = $argp['find'] ?? $argp[''][0] ?? '';<br>
if ($find=='') return '';<br>
$pgs = $argp['pages'] ?? $argp['page'] ?? $argp[''][1] ??
$pn;<br>
if (strpos($pgs,',')) $pages = explode(',',$pgs); <br>
else $pages = array($pgs);<br>
$cnt = 0;<br>
foreach ($pages as $p) { <br>
$sect = strstr($name,'#'); <br>
$name = MakePageName($pn, $p); <br>
$source = $name.$sect;<br>
$text = RetrieveAuthSection($pn, $source);<br>
$cnt += substr_count($text, $find);<br>
}<br>
return $cnt;<br>
}</font></p>
thanks! Hans <br>
<fieldset class="moz-mime-attachment-header"></fieldset>
<pre wrap="" class="moz-quote-pre">_______________________________________________
pmwiki-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:[email protected]">[email protected]</a>
<a class="moz-txt-link-freetext" href="http://www.pmichaud.com/mailman/listinfo/pmwiki-users">http://www.pmichaud.com/mailman/listinfo/pmwiki-users</a>
</pre>
</blockquote>
</body>
</html>
--------------XkINhOPczXqLZocpVHW0okap--
--===============6584925320786966044==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
pmwiki-users mailing list
[email protected]
http://www.pmichaud.com/mailman/listinfo/pmwiki-users
--===============6584925320786966044==--