Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

MarkupExprPlus

Summary: Extends {(...)} expression markups
Version: 2008-06-18
Prerequisites: pmwiki 2.1.27 to 2.2.x and eventually MarkupExpressions
Status: Beta
Maintainer: Dfaure

Questions answered by this recipe

MarkupExpressions is cool for simple operations, but how could I handle more complex expressions involving: arithmetic operations, conditionnal values, regular expressions,... ?

Description

The markupexprplus.phpΔ script expands the original MarkupExpressions recipe (core built-in since version 2.2.0-beta43) essentially by enabling *real* expression nesting (ie. able to handle arithmetic operations). It also defines various extra functions ranging from basic math to advanced string manipulation.

The operations defined by this recipe include add, sub, mul, div, mod, rev, rot13, urlencode, urldecode, reg_replace, sprintf, wikiword, test, and if.

The recipe also provides some configuration flags applying deeper modifications to the original script behavior such as multiline expressions handling and variable manipulation.

add, sub, mul, div, mod

"add", "sub", "mul", "div" and "mod" expressions handle their arguments as numeric data and compute the resulting value.

(4+2)*(4-2)+30 = {(add (mul (add 4 2) (sub 4 2)) 30)}

(4+2)*(4-2)+30 = 42

rev, rot13

"rev" and "rot13" expressions transform strings by respectively reversing it and applying the ROT13 encoding on it. The ROT13 encoding simply shifts every letter by 13 places in the alphabet while leaving non-alpha characters untouched. Encoding and decoding are done by the same function, passing an encoded string as argument will return the original version.

* {(rev "Hello world!")}
* {(rot13 MarkupExpressions)}
  • !dlrow olleH
  • ZnexhcRkcerffvbaf

urlencode, urldecode

"urlencode" and "urldecode" expressions allow back and forth transformation of a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits. This is the encoding described in RFC 1738 for protecting literal characters from being interpreted as special URL delimiters, and for protecting URLs from being mangled by transmission media with character conversions (like some email systems).

* {(urlencode 'foo @+%/')}
* {(urldecode foo%20bar%40baz)}
  • foo%20%40%2B%25%2F
  • foo bar@baz

reg_replace , sprintf

"reg_replace" and "sprintf" are wrappers around PHP's preg_replace and sprintf functions to perform regular-expressions search & replace, and string formatting.

* {(reg_replace '/(\w+) (\d+), (\d+)/i' '${1}1,$3' 'April 15, 2003')}
* {(sprintf "The %2$s contains %1$d monkeys. That's a nice %2$s full of
%1$d monkeys." 5 'tree')}
  • April1,2003
  • The tree contains 5 monkeys. That's a nice tree full of 5 monkeys.

wikiword, nomarkup

The "wikiword" expression generates wiki words according to core configuration.

{(wikiword 'Make a wiki word')}

MakeAWikiWord

The "nomarkup" expression keeps only the text resulting from a regular markup expression.

{(nomarkup "!!You are ''[-[[{$FullName}|here]]-]''")}

You are here

test, if

The "test" expression evaluates arguments as a regular PmWiki condition, returning "0" or "1" accordingly.

* true: {(test true)}
* false: {(test expr true and false)}
  • true: 1
  • false: 0

The "if" expression outputs its 2nd or 3rd argument according to the value of the 1st one.

{(if (test expr true and false) "Cool!" "Aaargh...")}
Aaargh...

Portability enhancements

Dixit PHP documentation:

Not all conversion specifiers may be supported by your C library, in which case they will not be supported by PHP's strftime(). Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. T, D (there might be more) and dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems.

The recipe patches the "ftime" expression to enables some of the format specifier missing on Win32 platforms.

Modified behavior / Configuration flags

$EnableExprMultiline
When set to 1, lets the recipe handle expressions defined across several lines.
$EnableExprVarManip
When set to 1, enables variable manipulation through "set" and "setq" extra functions: They both handle value assignation to PageVariables, the latter performing quietly without any output.
Value was {(set xx 41)}, and now {(setq xx (add {$xx} 1))} is {$xx}
Value was 41, and now is 42.

Notes

The working considerations already expressed for MarkupExpressions apply here also.

Contributors

See Also

Release Notes

2008-06-18
Added the (nomarkup) function.
2008-03-05
Corrected variable evaluation when $EnableExprVarManip parameter is set. Added compatibility with HttpVariables recipe. Added the (sprintf...) function.
2007-12-10
Removed 'notice' messages.
2007-10-26
Added the (mod...) function.
2007-10-19
Added experimental extension of the (ftime...) function to take an optional third parameter 'lc' which takes a locale in which to render the date (thanks to StirlingWestrup).
2007-09-13
Initial public release

Comments

Along with the rev and rot13 functions an md5 function could be useful. Maybe keep it in mind for a future version of the recipe.

Blues September 18, 2007, at 04:29 AM

For now, you only need in your configuration file:
$MarkupExpr['md5'] = 'md5($args[0])';
include_once("$FarmD/cookbook/markupexprplus.php");
Dfaure September 18, 2007, at 08:09 AM

Is there a reason these expressions are called through a separate markup function rather than through the built-in $MarkupExpr array? I'd like to be able to use one or two of these without having to install the whole recipe. BenStallings

The "separate markup function" is only a drop-in replacement of the original one defined in the core which hasn't been tailored to handle complex expressions. Feel free to "extract" the expressions token definitions you need and make your own (sub)recipe with them --Dfaure March 03, 2008, at 03:35 PM

hi, is it possible to cut a Variable by String? for example: My Pagename "$Name" is Word1Word3. Now I´m searching for a solution to cut the variable infront of the String "Word3". My Problem - the Variable "$Name" can changed to "Word200"..., but the string "Word3" is set. Hope you can help me. Hendrik June 17, 2008, at 07:23 AM

This is a perfect job for a regular-expressions replacement such as: {(reg_replace '/(?:.*)(Word3)/' '$1' '{$Name}')} --Dfaure June 18, 2008, at 06:55 AM

Edit - History - Print - Recent Changes - Search
Page last modified on June 18, 2008, at 08:46 AM