Extension:Pipe Escape

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png
Pipe Escape

Release status: stable

Implementation Parser function
Description Escape pipe characters in parser function arguments/template arguments
Author(s) David M. Sledge (Sledgedtalk)
Latest version 0.1.2 (2012-01-19)
MediaWiki ≥ 1.12
Database changes No
License GNU General Public License 2.0 or later
Download Template:WikimediaDownload/svn
log
Hooks used
ParserFirstCallInit

Translate the Pipe Escape extension if it is available at translatewiki.net

Check usage and version matrix; code metrics

The Pipe Escape extension allows for pipe characters in parser function arguments (and template argument calls) avoid being interpreted as an argument delimiter. This is primarily for the purpose of using wiki tables (or parts thereof) inside parser function calls or templates.

Use cases[edit | edit source]

For example, suppose you have the following wikitable in a template:

{|
|-
!       || Heading 1 || Heading 2
|-
| Row a || a1        || a2
|-
| Row b || b1        || b2
|}

but you only want the table to show up when a non-whitespace value is supplied for template argument targ. One possible solution is to use HTML instead of wiki syntax

{{#if: {{{targ|}}}|<table>
<tr>
<th> <th> Heading 1 <th> Heading 2
<tr>
<td> Row a <td> a1 <td> a2
<tr>
<td> Row b <td> b1 <td> b2
</table>}}

but this is less intuitive and therefore more error-prone, and, in part, defeats the purpose of a wiki. Another solution is to hide the pipe characters from the parser function in a template. (Common practice is to use the template Template:! with its includable contents being only a raw pipe character.)

{{#if: {{{targ|}}}
|{{{!}}
{{!}}-
!           {{!}}{{!}} Heading 1 {{!}}{{!}} Heading 2
{{!}}-
{{!}} Row a {{!}}{{!}} a1        {{!}}{{!}} a2
{{!}}-
{{!}} Row b {{!}}{{!}} b1        {{!}}{{!}} b2
{{!}}}
}}

This is a better solution, but things are a bit messy when you're trying to hide multiple pipes in an argument. This is where this simple parser function comes in handy.

Usage[edit | edit source]

{{#!: wiki text }}

This hides the pipe characters in wiki text so that outer parser functions and outer templates do not interpret them as argument delimiters. With this extension, the above example can be rewritten as

{{#if: {{{targ|}}}
| {{#!:
{|
|-
!       || Heading 1 || Heading 2
|-
| Row a || a1        || a2
|-
| Row b || b1        || b2
|}
}}
}}

In reality, the pipe characters are still treated as argument delimiters in this parser function, but it concatenates the arguments using a pipe character as a separator which outer parser functions do not register as delimiters.

Unlike most parser functions, pipe escape preserves the leading and trailing whitespace around its arguments except the first argument (due to the way MediaWiki handles the first argument in parser functions; See bug 12842), and the trailing whitespace in the last argument.

Pipe escape only affects top-level pipe characters. It does not affect pipe characters in other parser functions nested within the pipe escape parser function call. The same is true for pipe characters in template calls, template arguments, intrawiki links, tag extensions, etc...

Installation[edit | edit source]

  1. Download the files from SVN with
    svn checkout http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/PipeEscape/
    or
    copy the files from SVN
  2. Place the files under $IP/extensions/PipeEscape
  3. Add to the end of LocalSettings.php:
    require_once("$IP/extensions/PipeEscape/PipeEscape.php");
  4. Installation can now be verified through Special:Version on your wiki