Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

MarkupExpressions

Summary: Enable {(...)} expression markups (recipe superseded by core)
Version: 2007-04-11
Prerequisites: pmwiki 2.1.27 to 2.2.0-beta42
Status: Superseded by PmWiki.Markup Expressions from PmWiki 2.2.0-beta43.
Maintainer: Pm

Questions answered by this recipe

  • Can I do simple substring and formatting operations in markup?
  • Can I format dates and times using markup?

Description

The markupexpr.phpΔ recipe enables the {(...)} "expression markup", which 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.

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)} produces "Wik"
  • {(substr "PmWiki" 2)} produces "Wiki"
  • {(substr "PmWiki" 0 1)} produces "P"

ftime

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

    {(ftime "fmt" "when")}

where fmt is a formatting string and when is the time to be formatted. The formatting codes are described at http://www.php.net/strftime. Some common formatting strings:

   %F                # ISO-8601 dates      "2008-07-04"
   %H:%M:%S          # time as hh:mm:ss    "21:29:55"
   %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. 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
   yesterday             # this time yesterday
   "next Monday"         # relative dates
   "last Thursday"       # relative dates
   "-3 days"             # three days ago
   "+2 weeks"            # two weeks from now
   "2007-04-11 -4 days"  # four days before April 11

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

strlen

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

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

toupper

The "toupper" expression converts a string to uppercase. The first argument is the string to be processed.

tolower

The "tolower" expression converts a string to lowercase. The first argument is the string to be processed.

ucfirst

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

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.

pagename

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

asspaced

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

Nesting expressions

Markup expressions can be nested:

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

Notes

  • Feel free to experiment with markup expressions in the MarkupExpressions-Sandbox
  • See the comments in the recipe for information about adding other expressions to the markup.
  • Some of the string-processing markups may not work properly on UTF-8 characters or escaped sequences.

Release Notes

Comments

See Also

Contributors

Edit - History - Print - Recent Changes - Search
Page last modified on March 21, 2008, at 01:12 PM