Recent Changes - Search:

PmWiki

pmwiki.org

MarkupExpressions

authors (advanced)

The {(...)} "expression markup" allows for a variety of string and formatting operations to be performed from within markup. Operations defined by this recipe include substr, ftime, strlen, rand, toupper / tolower, ucfirst, ucwords, pagename and asspaced.

Markup Expressions were introduced in PmWiki 2.2.0-beta43.

substr

The "substr" expression extracts portions of a string. The first argument is the string to be processed, the second argument is the initial position of the substring, and the third argument is the number of characters to extract. Note that the initial position argument is zero-based (i.e., the first character is referenced via a "0").

 {(substr "PmWiki" 2 3)}
 {(substr "PmWiki" 2)}
 {(substr "PmWiki" 0 1)}
 Wik
 Wiki
 P

ftime

"Ftime" expressions are used for date and time formatting. The generic form is

{(ftime "fmt" "when")}
{(ftime fmt="fmt" when="when")}

where fmt is a formatting string and when is the time to be formatted. The arguments can be in either order and may use the optional "fmt=" and "when=" labels.

Examples:

 {(ftime)}
 {(ftime fmt="%F %H:%M")}
 {(ftime %Y)}
 {(ftime fmt=%T)}
 {(ftime when=tomorrow)}
 {(ftime fmt="%Y-%m-%d" yesterday)}
 {(ftime week %F)}
 {(ftime fmt=%D month)}
 {(ftime fmt="%a%e %b" when="next
week")}
 July 04, 2008, at 02:30 PM
 2008-07-04 14:30
 2008
 14:30:57
 July 05, 2008, at 12:00 AM
 2008-07-03
 2008-07-11
 08/04/08
 Fri11 Jul

The fmt parameter is whatever is given by "fmt=", the first parameter containing a '%', or else the site's default. The formatting codes are described at http://php.net/strftime. In addition to those, 's' produces Unix timestamps. Some common formatting strings:

     %F                # ISO-8601 dates      "2008-07-04"
     %s                # Unix timestamp      "1215199857"
     %H:%M:%S          # time as hh:mm:ss    "14:30:57"
     %m/%d/%Y          # date as mm/dd/yyyy  "07/04/2008"
     "%A, %B %d, %Y"   # in words            "Friday, July 04, 2008"

The when parameter understands many different date formats. The when parameter is whatever is given by "when=", or whatever parameter remains after determining the format parameter. Some examples:

    2007-04-11            # ISO-8601 dates
    20070411              # dates without hyphens, slashes, or dots
    2007-03               # months
    @1176304315           # Unix timestamps (seconds since 1-Jan-1970 00:00 UTC)
    now                   # the current time
    today                 # today @ 00:00:00
    yesterday             # yesterday @ 00:00:00
    "next Monday"         # relative dates
    "last Thursday"       # relative dates
    "-3 days"             # three days ago
    "+2 weeks"            # two weeks from now

The when parameter uses PHP's strtotime function to convert date strings according to the GNU date input formats; as of this writing it only understands English phrases in date specifications.

The variable $FTimeFmt can be used to override the default date format used by the "ftime" function. The default $FTimeFmt is $TimeFmt.

strlen

The "strlen" expression returns the length of a string. The first argument is the string to be measured.

 {(strlen "{$:Summary}")}
 32

rand

The "rand" expression returns a random integer. The first argument is the minimum number to be returned and the second argument is the maximum number to be returned. If called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and RAND_MAX. If you want a random number between 5 and 15 (inclusive), for example, use rand (5, 15).

 {(rand)}
 1762784880

toupper / tolower

The "toupper" and "tolower" expressions convert a string into uppercase or lowercase. The first argument is the string to be processed.

 {(toupper "{$:Summary}")}
 {(tolower "{$:Summary}")}
 STRING AND FORMATTING OPERATIONS
 string and formatting operations

ucfirst

The "ucfirst" expression converts the first character of a string to uppercase. The first argument is the string to be processed.

 {(ucfirst "{$:Summary}")}
 String and formatting operations

ucwords

The "ucwords" expression converts the first character of each word in a string to uppercase. The first argument is the string to be processed.

 {(ucwords "{$:Summary}")}
 String And Formatting Operations

pagename

The "pagename" expression builds a pagename from a string. The first argument is the string to be processed.

 {(pagename "{$:Summary}")}
 PmWiki.StringAndFormattingOperations

asspaced

The "asspaced" expression formats wikiwords. The first argument is the string to be processed.

 {(asspaced "{$FullName}")}
 Pm Wiki.Markup Expressions

Nesting expressions

Markup expressions can be nested:

 {(tolower (substr "Hello World" 2))}
 llo world

Notes

  • Some of the string-processing markups may not work properly on UTF-8 characters or escaped sequences.
  • The ftime markup does not work with some ISO 8601 dates (because a time of 24:00 is invalid)such as:
 {(ftime fmt="%m/%d/%Y @ %H:%M:%S"
when="20070626T2400")}
 {(ftime fmt="%H:%M:%S"
when="20070626T2400")}
 06/27/2007 @ 00:00:00
 00:00:00
  • is it possible to display the time in another time zone, eg
NowTime: {(ftime fmt="%F %H:%M")}
 {(ftime when='TZ=:Europe/London" ')}
 {(ftime when='TZ=Europe/London" 0
days')}
 {(ftime when="TZ='Pacific/Auckland'
2004-10-31 08:30")}
 {(ftime when='TZ="Pacific/Auckland"
{$:NowTime}')}

NowTime: 2008-07-04 14:30

 December 31, 1969, at 05:59 PM
 December 31, 1969, at 05:59 PM
 October 31, 2004, at 12:00 AM
 (ftime when='TZ=Pacific/Auckland {2008-07-04 14:30')}

Requests for further markup expressions

  • allow markup expressions to be nested
  • rtrim - remove training 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)}
  • 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;
  }

See also

<< Page text variables | Documentation Index | Forms >>

Edit - History - Print - Recent Changes - Search
Page last modified on July 03, 2008, at 07:16 PM