Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

MarkupExprPlus

Summary: Extends {(...)} expression markups
Version: 2010-09-01
Prerequisites: pmwiki 2.1.27 to 2.2.x and eventually MarkupExpressions
Status: Beta
Maintainer: Dfaure
Users: +5 (View / Edit)

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

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

{(add 0.5 4.2 5.9)}

10.6

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

Makelambda_4lambda_4ikilambda_4ord

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 1.

Notes

The working considerations already expressed for MarkupExpressions apply here also.

Contributors

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

2010-09-01
Fixed arithmetic evaluation (thanks to Jjs).
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

See Discussion at MarkupExprPlus-Talk

Sandbox

{(substr {$Name} 2)}

ambda_19

User notes +5: 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.

Edit - History - Print - Recent Changes - Search
Page last modified on November 16, 2014, at 02:08 AM