Jump to content

Extension:Character Escapes

From mediawiki.org
MediaWiki extensions manual
Character Escapes
Release status: unmaintained
Implementation Parser extension , Tag
Description Provides a convenience tag for escaping tags, templates, magic words, and parser function calls nested in tags and parser functions that support character escaping
Author(s) David M. Sledge
Maintainer(s) Alexia E. Smith
Latest version 1.0.0 (2016-01-04)
MediaWiki 1.25+
Database changes No
License GNU General Public License 2.0 or later
Download
‎<esc>

The Character Escapes extension provides a convenience tag for escaping tags, templates, magic words, and parser function calls nested in tags and parser functions that support character escaping.

Rationale

[edit]

Sometimes it is desired that wiki markup be parsed (or remain unparsed) under certain conditions. Since certain characters or character sequences are processed before reaching the parser function, we have to use escapes to prevent markup being parsed prematurely. MediaWiki does not have built-in mechanism for this, so we have to make our own:

  • \l (less than) is translated to <
  • \g (greater than) is translated to >

  • \o (open double curly braces) is translated to {{
  • \c (close double curly braces) is translated to }}
  • \p (pipe) is translated to |

  • \\ is translated to \

  • \n is translated to a newline

The first two translations make it possible to embed a wiki tag extension into a parameter of a parser function call. The next three translations make it possible to call a template, invoke a magic word, or call a parser function which prevents them from executing until conditions dictate that the results of such a call will be displayed. The next one is for times where you want to display text like "\p" without having it converted into a pipe, which is done by writing it as "\\p". The last one is for tags that use newline characters as delimiters for parameters. It allows a newline character to be passed as part of the parameter instead of indicating the beginning/ending of a parameter.

Example

[edit]
Syntax
{{#vardefine: i |0 }}{{#while: expr
  | <esc>{{#var: i }} < 5</esc>
  |* <esc>{{#var: i }}{{#vardefine: i |{{#expr: {{#var: i }} + 1 }} }}</esc>
}}
Result
  • 0
  • 1
  • 2
  • 3
  • 4

Note that the example uses the Variables and Loops extensions.

Limitations

[edit]

MediaWiki does not support nested tags of the same type (see task T3310). Given the following:

<esc>{{#ifexpr:... | <esc>{{ templateB | param }}</esc> | param }}</esc>

The text:

{{#ifexpr:... | <esc>{{ templateB | param }}

is passed to the underlying function instead of:

{{#ifexpr:... | <esc>{{ templateB | param }}</esc> | param }}

A workaround is to explicitly write out the nested escape sequences:

<esc>{{#ifexpr:... | \o templateB \p param \c | param }}</esc>

Another solution is to apply the modification given in the discussion of bug #1310.

Installation

[edit]
  • Download, extract and place the file(s) in a directory called CharacterEscapes in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php file:
    wfLoadExtension( 'CharacterEscapes' );
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.