MarkupExprPlus

Extends {(...)} expression markups

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) Categories: Markup, Page Variables Markup Expressions PHP55 Download: Attach:markupexprplus.php Δ for pmwiki versions <= 2.2.56: Attach: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,... ?

Description

The Attach: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)}

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

%apply=item id=EnableExprMultiline?%$EnableExprMultiline
When set to 1, lets the recipe handle expressions defined across several lines.
%apply=item id=EnableExprVarManip?%$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 (set xx 41), and now (setq xx (add 1)) is .

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

2016-05-24
Internal cosmetic changes.
2016-02-29
Restored the multiline handling and the variable manipulation features. Thanks to ChuckG for his work.
2014-12-04
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.
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)}

rkupExprPlus

 0: 00.00 00.00 config start
 1: 00.01 00.00 config end
 2: 00.08 00.02 MarkupToHTML begin
 3: 00.09 00.02 MarkupToHTML begin
 4: 00.10 00.03 MarkupToHTML end
 5: 00.10 00.03 MarkupToHTML begin
 6: 00.10 00.03 MarkupToHTML end
 7: 00.10 00.03 MarkupToHTML begin
 8: 00.11 00.03 MarkupToHTML end
 9: 00.11 00.03 MarkupToHTML begin
10: 00.11 00.03 MarkupToHTML end
11: 00.11 00.03 MarkupToHTML begin
12: 00.12 00.03 MarkupToHTML end
13: 00.12 00.03 MarkupToHTML begin
14: 00.12 00.03 MarkupToHTML end
15: 00.12 00.03 MarkupToHTML begin
16: 00.12 00.03 MarkupToHTML end
17: 00.12 00.03 MarkupToHTML begin
18: 00.13 00.04 MarkupToHTML end
19: 00.13 00.04 MarkupToHTML begin
20: 00.13 00.04 MarkupToHTML end
21: 00.13 00.04 MarkupToHTML begin
22: 00.13 00.04 MarkupToHTML end
23: 00.13 00.04 MarkupToHTML begin
24: 00.13 00.04 MarkupToHTML end
25: 00.13 00.04 MarkupToHTML begin
26: 00.13 00.04 MarkupToHTML end
27: 00.20 00.06 ReadApprovedUrls SiteAdmin.ApprovedUrls begin
28: 00.20 00.06 ReadApprovedUrls SiteAdmin.ApprovedUrls end
29: 00.51 00.14 MarkupToHTML end
30: 00.52 00.15 MarkupToHTML begin
31: 00.59 00.16 MarkupToHTML end
32: 00.59 00.16 MarkupToHTML begin
33: 00.60 00.16 MarkupToHTML end
34: 00.61 00.16 now
Peak memory: 3,227,720 bytes