MarkupExpressions-Talk

This is a talk page for improving MarkupExpressions.


Requests for further markup expressions

Please vote up if you need these 01393

  • ftime add ordinal suffix for date format - st, nd, rd, th. Added to PITS - please vote up if you requested this. XES
  • allow markup expressions to be nested (already possible - see below)
  • rtrim - remove trailing spaces (default) or character if optional parameter supplied (useful for page text variables);
    {(rtrim string optionalchar)}
  • ltrim - remove leading spaces (default) or character if optional parameter supplied (useful for page text variables);
    {(ltrim string optionalchar)}
  • trim - remove leading and trailing spaces (default) or character if optional parameter supplied (useful for page text variables);
    {(trim string optionalchar)}
  • nospace - remove all spaces or character is optional parameter supplied
    {(nospace string optionalcharacter)}
  • nomarkup - remove markup such as separators (! headings, ----), lists (*, #, : :), emphasis ('', ''', [++], [--], etc), paragraphs ([[<<]], -<, ->), and optionally groups ([[x|y]] becomes y)
    {(nomarkup string nolinks)}
  • word - return specified word(s) (alfanum+"_"+"-"). If second number is greater than no of word, return all words til the end (KAL)
    {(word string 1)} = return first word
    {(word string 2 4)} = return second, third and fourth word
  • token - return parsed string, list of valid separators is specified as 1. argument, no of token as 2. argument (KAL)
    {(token string 'list-of-sep-chars' 3)}
  • return string before a target string (KAL)
    • e.g.: {(stringbefore "sample string to be parsed" "ng")} will return "sample stri"
    • PHP-code to add to markupexpressions.php:
# return substring of string before targetstring:
##  expressions like {(stringbefore string targetstring)}
$MarkupExpr['stringbefore'] = 'stringbefore($args[0],$args[1])';

function stringbefore($text,$target)
{
  list($text2,$null) = explode($target,$text);
  return $text2;
}
  • and for returning string after
# return substring of string after targetstring:
##  expressions like {(stringafter string targetstring)}
$MarkupExpr['stringafter'] = 'stringafter($args[0],$args[1])';

function stringafter($text,$target)
{
  list($null,$text2) = explode($target,$text);
  return $text2;
}


how do I achieve the following

  • obtain the last n characters of a string
Use {(substr "string" -n)}
  • truncate the last n characters of a string
Use {(substr "string" 0 -n)}

Working examples

* "{$Name}"
# {(substr "{$Name}" 6 -5)}
# {(substr "{$Name}" -5)}
# {(substr "{$Name}" 0)}

# {(substr "{$Name}" 0 -5)}
# {(substr "{$Name}" -5 4)}
# {(substr "{$Name}" -5 -6)}
  • "MarkupExpressions-Talk"
  1. Expressions
  2. -Talk
  3. MarkupExpressions-Talk
  4. MarkupExpressions
  5. -Tal

Is it possible to nest markup expressions?

looks like the answer is no
{(substr "{$FullName}" 0 -5)}

{(asspaced '{$FullName}')}

{(asspaced '{(substr "{$FullName}" 0 -5)}')}

PmWiki.MarkupExpressions

Pm Wiki.Markup Expressions-Talk

(asspaced '{PmWiki.MarkupExpressions')}

Ah, but with a different syntax and being careful of quoting...
{(asspaced (substr "{$FullName}" 0 -5))}

Pm Wiki.Markup Expressions

Points to note:

  • Nested MXes work with just parens on the inner MX, not curly+paren
  • If you put it in quotes then it will be processed as a literal rather than as an expression, as expected

How is it possible to use page variables as follows

(:Month:{(substr 2013May 4)}:) 
(:MonthNum:{(ftime fmt="%m" when="{$:Month}")}:)
{$:Month} ''as a number is'' {$:MonthNum}

May as a number is (ftime fmt=%m when="{May")}

Just note that when you have in a page a {$:PageTextVariable} the wikitext it contains is pasted in its place in the page without any change (except for relative links), and without evaluation. The evaluation will be done at a later point. That means, your {$:MonthNum} variable will actually contain {(ftime fmt="%m" when="{(substr 2013May 4)}")} after {$:Month} has been changed to its text content. When at a later point the markup expressions are evaluated, this is an incorrect syntax - in order to embed markup expressions, only the outer one needs curly brackets. So PmWiki does exactly what it is designed to do, but probably not what you expect. --Petko May 03, 2013, at 03:32 PM

Fix by:

  • removing the curly braces from the definition of PTV Month
  • adding curly braces around the call to PTV Month in the final line
  • removing quotes around the when parameter in the PTV MonthNum definition
(:Month2:(substr 2013May 4):) 
(:MonthNum2:{(ftime fmt="%m" when={$:Month2})}:)
{{$:Month2}} ''as a number is'' {$:MonthNum2}

May as a number is 05

ChuckG December 08, 2017, at 06:05 PM


This is working fine in MarkupExpressions-Sandbox but not here. Is there anything wrong with this? -- Luigi 2 Sept 2014

MyDate:2007-04-12


              {(ftime '%A %B %e, %Y' {$:MyDate})}
              {(ftime '%A %B %e, %Y' '{$:MyDate} -2days')}

              Thursday April 12, 2007
              Tuesday April 10, 2007

Update! Thanks to Petko for enhancing the system: date subtraction now works!
Please see the thread about Adding/subtracting days to ftime Page Text Variable -- Luigi 5 Sept 2014


* {(ftime fmt="%Y-%m-%d" when="first tue of jan 2015")}
* {(ftime fmt="%Y-%m-%d" when="first tue of next jan")}
* {(ftime fmt="%Y-%m-%d" when="first tue of jan")}
  • 2015-01-06
  • 2024-04-20
  • 2024-01-02

On the 31st of August (possibly on every 31st or perhaps on every last day of a month, not sure) there is an odd result, something like:

  • Jul
  • Jul
  • Aug 2016
  • Oct
  • Oct
  • Dec
* {(ftime fmt="%b" "-2 month")}
* {(ftime fmt="%b" "-1 month")}
* {(ftime fmt="%b %Y")}
* {(ftime fmt="%b" "+1 month")}
* {(ftime fmt="%b" "+2 month")}
* {(ftime fmt="%b" "+3 month")}
  • Feb
  • Mar
  • Apr 2024
  • May
  • Jun
  • Jul

A possible fix is here http://www.pmichaud.com/pipermail/pmwiki-users/2016-August/063609.html thanks to Petko.

-- Luigi 31 August 2016

This is a talk page for improving PmWiki.MarkupExpressions.