MarkupExprPlus

Summary: Extends {(...)} expression markups
Version: 2023-02-24
Prerequisites: pmwiki 2.1.27 to 2.3.x and eventually MarkupExpressions
Status: Stable
Maintainer: Dfaure
Users: +8 (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,... ?

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.

Installation

Install in config.php with

include_once("$FarmD/cookbook/markupexprplus.php");

Usage

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, unaccent

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

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

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

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

cat, echo

These are mainly helper functions useful when setting up complex expressions. The "cat" function concatenates its arguments into a single string and the "echo" function output its arguments as-is.

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 and only for previous versions of PmWiki which don't already handle it, 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

$EnableExprMultiline
When set to 1, lets the recipe handle expressions defined across several lines.
$EnableExprVarManip
When set to 1, enables variable manipulation through a set of extra functions:

Variable manipulation

set, setq

The "set" and "setq" functions 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.

pagevar

The "pagevar" function allows you to retrieve the Variables values from the given parameters.

(:PTV_Full: Full:)
(:PTV_Name: Name:)
* FullName: {$FullName}
* FullName: {(pagevar $FullName)}
* Variable: {$:PTV_Full}{$:PTV_Name}
* Value: {(pagevar ${$:PTV_Full}{$:PTV_Name})}

* Name variable: {$Name}
* Setting the variable "VarFor{$Name}" to "{(set VarFor{$Name} xxx)}"
* Retrieved value: {(pagevar $VarFor{$Name})}
  • FullName: Cookbook.MarkupExprPlus
  • FullName: Cookbook.MarkupExprPlus
  • Variable: FullName
  • Value: Cookbook.MarkupExprPlus
  • Name variable: MarkupExprPlus
  • Setting the variable "VarForMarkupExprPlus" to "xxx"
  • Retrieved value: xxx

Notes

The working considerations already expressed for MarkupExpressions apply here also.

While using together the MarkupExprPlus and HttpVariables recipes, the MarkupExprPlus script must be included after the HttpVariables one.

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
  • HttpVariables Access http variables in the page, such as Get variables, Post data, and Cookies.

Release Notes

2023-02-24
Updated recipe for PmWiki 2.3.20+
2021-04-06
Updated recipe for PHP 8.0 compatibility.
2021-03-31
Fixed {(test...)} not working with parameters containing spaces. Added the {(pagevar...)} function.
2019-11-12
Fixed partial PHP 7.2 compatibility.
2018-06-14
Update for PHP 7.2 compatibility - ChuckG .
2016-06-09
Fixed the {(test...)} function and the HttpVariables compatibility. Thanks again to ChuckG.
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

Please update running to uploaded version - ChuckG June 15, 2018, at 08:07 AM .

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