Summary: Extends {(...)} expression markups
Version: 2016-05-24
Prerequisites: pmwiki 2.1.27 to 2.2.x and eventually MarkupExpressions
Status: Stable
Maintainer: Dfaure
Users: (View? / Edit)

for pmwiki versions <= 2.2.56: markupexprplus-old.phpΔ [version 2010-09-01]

Questions answered by this recipe

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


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.5+1.5)*(4-2)+30 = {(add (mul (add 4.5 1.5) (sub 4 2)) 30)}

(4.5+1.5)*(4-2)+30 = (add (mul (add 4.5 1.5) (sub 4 2)) 30)

{(add 0.5 4.2 5.9)}

(add 0.5 4.2 5.9)

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)}
  • (rev Hello world!)
  • (rot13 MarkupExpressions)

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)}
  • (urlencode foo @+%/)
  • (urldecode foo%20bar%40baz)

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')}
  • (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)

wikiword, nomarkup, unaccent

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

{(wikiword 'Make a wiki word')}

(wikiword Make a wiki word)

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

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

(nomarkup !!You are here)

The "unaccent" expression transform accentuated chars to unaccented equivalents (UTF-8 compliant).

* àéîöù
* {(toupper àéîöù)}
* {(unaccent àéîöù)}
* {(unaccent (toupper àéîöù))}
  • àéîöù
  • (unaccent àéîöù)
  • (unaccent ÀÉÎÖÙ)

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: (test true)
  • false: (test expr true and false)

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...")}

(if (test expr true and false) Cool! 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.

By default, the recipe patches the "ftime" expression to enables some of the format specifier missing on Win32 platforms. This can be disabled by setting to 1 the $EnableExprOriginalFtime configuration flag.

Modified behavior / Configuration flags

When set to 1, lets the recipe handle expressions defined across several lines.
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 (set xx 41), and now (setq xx (add 1)) is .


The working considerations already expressed for MarkupExpressions apply here also.


See Also

  • PmWiki.MarkupExpressions (core)
  • Cookbook.MarkupExpressionSamples (adding a number of other markup expressions)
  • PowerTools - markup expressions for multi page processing incl. plist, pagelist, rename, pagecount, wordcount, trail, serialname, serial, newticket, sumdata, allptvs, random
  • WikiSh - a basic scripting language built using markup expressions
  • MiscMX - Implement miscellaneous PHP functions by means of Markup Expressions

Release Notes

Internal cosmetic changes.
Restored the multiline handling and the variable manipulation features. Thanks to ChuckG for his work.
Update for PHP 5.5 compatibility, with some exotic features missing. Added the {(unaccent)} function. Thanks to HansB for his initial PHP 5.5 port.
Fixed arithmetic evaluation (thanks to Jjs).
Added the {(nomarkup)} function.
Corrected variable evaluation when $EnableExprVarManip parameter is set. Added compatibility with HttpVariables recipe. Added the {(sprintf...)} function.
Removed 'notice' messages.
Added the {(mod...)} function.
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).
Initial public release


See Discussion at MarkupExprPlusTest-Talk?


{(substr {$Name} 2)}


User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.