[pmwiki-users] Page variables in monospaced text

Peter Bowers pbowers at pobox.com
Tue Sep 25 01:17:44 CDT 2012


On Mon, Sep 24, 2012 at 11:14 PM, Allister Jenks <zkarj at me.com> wrote:
> Inside any [@ @] block,
> On 25 Sep, 2012,at 03:58 AM, Peter Bowers <pbowers at pobox.com> wrote:
>
> On Mon, Sep 24, 2012 at 3:38 AM, Allister Jenks <zkarj at me.com> wrote:
>> Is there a way to turn on the variable substitution inside the [@ @]
>> blocks?
>
> Is this only for your 5250 blocks or inside any [@...@]?

I would over-ride the definition of markup "[=" and, depending on your
needs, either
* use a preg_replace() according to "{$var}"
-or-
* install Cookbook/Toolbox and use RunMarkupRules()

If you are certain that the only rule you will want to run over your
[@...@] block is the variable substitution then just use the
preg_replace.  If you think that at some point in the future you will
have additional rules that you will want to run then it is simpler to
just name the rules in an array and let RunMarkupRules() handle the
rest.

Below I've both overridden the [@...@] markup as well as introducing a
new [$...$] markup.  The idea is if you want to keep the normal
definition of [=...=] and [@...@] then you can just comment out that
call to Markup() and use the [$...$] in the situations where you want
to run rules (such as variable substitution).  But if you just want to
always have certain rules run on [@...@] blocks then just comment out
and ignore the [$...$] Markup() call.

I've chosen the toolbox solution to have it be a little more flexible
for future use.  I've chosen the ''' and [+ rules just at random to be
the rules in addition to {$var} that will be processed.  You can
override this either by setting $PreMarkupRules outside the function
or by just editing the initialization of that variable inside the
MyPreserveText() function.

(FYI MyPreserveText() is just a straight copy from PreserveText() in
scripts/stdmarkup.php with a little processing thrown in.)

===(snip)===
# [$...$] markup (run some extra rules inside pre)
Markup('[=','_begin',"/(\n[^\\S\n]*)?\\[([=@])(.*?)\\2\\]/se",
    "MyPreserveText(\$pagename, '$2', PSS('$3'), '$1')");
Markup('[$','_begin',"/(\n[^\\S\n]*)?\\[([\$])(.*?)\\2\\]/se",
    "MyPreserveText(\$pagename, '$2', PSS('$3'), '$1')");

$EnablePreMarkup = true;
include_once("$FarmD/cookbook/toolbox.php");
## first we preserve text in [=...=] and [@...@]
function MyPreserveText($pagename, $sigil, $text, $lead) {
  global $EnablePreMarkup, $PreMarkupRules;
  if (IsEnabled($EnablePreMarkup, false)) {
	  if (!isset($PreMarkupRules))
		  $PreMarkupRules = array('{$var}', "'''", '[+');
	  $text = RunMarkupRules($pagename, $PreMarkupRules, $text);
  }
  if ($sigil=='=') return $lead.Keep($text);
  if (strpos($text, "\n")===false)
    return "$lead<code class='escaped'>".Keep($text)."</code>";
  $text = preg_replace("/\n[^\\S\n]+$/", "\n", $text);
  if ($lead == "" || $lead == "\n")
    return "$lead<pre class='escaped'>".Keep($text)."</pre>";
  return "$lead<:pre,1>".Keep($text);
}
===(snip)===

I may or may not get get around to documenting this as a new recipe.
If you feel like doing it yourself, feel free...

-Peter



More information about the pmwiki-users mailing list