Extension:WhiteSpace

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
Crystal Clear action run.svg
WhiteSpace
Release status: beta
Implementation Tag, Extended syntax
Description Tags to control whitespace: <dws/> discards itself and all whitespace (including newlines) after the tag, <nl/> replaces itself with newline.
Author(s) Van de Buggertalk
MediaWiki 1.18+
Database changes No
License GNU Affero General Public License 3.0 or later
Download
Tags
<dws/>,
<nl/>
Hooks used
ParserBeforeStrip
Translate the WhiteSpace extension if it is available at translatewiki.net
Check usage and version matrix.

What?[edit]

Two tags to control whitespace in templates: <dws/> discards itself and all whitespace (including newlines) after the tag, <nl/> replaces itself with a newline.

Why?[edit]

In wikitext spaces and newlines do matter. Leading space causes a paragraph to be verbatim, newline terminates a list item, etc. This means that template code cannot be formatted freely. For example, two subsequent template calls:

{{ Template1 }}{{ Template2 }}

being reformatted as:

{{ Template1 }}
{{ Template2 }}

may produce different result (e. g. two list items instead of one). This is not a big deal for a simple template, but it quickly become a real issue for complicated templates. Let us consider a real example, a small part of Template:Extension:

   {{#if: {{{username|}}} |<!--
   -->{{#ifeq: {{{username}}}|{{{author|{{{username}}}<!-- -->}}}|<!--
      nothing
   -->|<!--else--><!--
      -->{{{author}}} (<!--
   -->}}<!--
   -->[[User:{{{username}}}|{{{username}}}]]<!--
   --><sup><small>[[User talk:{{{username}}}|{{int:talkpagelinktext}}]]</small></sup><!--
   -->{{#ifeq: {{{username}}}|{{{author|{{{username}}}<!-- -->}}}|<!--
      nothing
   -->|<!--else--><!--
      -->)<!--
   -->}}<!--
-->|<!--else--><!--
   -->{{{author}}}<!--
-->}}

HTML comments (<!-- -->) allows splitting template code to lines but it pollutes template code so the overall result is not so good. Using <dws/> the code can be reformatted as:

{{  #if: {{{ username | }}} 
    |   {{  #ifeq: {{{ username }}} | {{{ author | {{{ username }}} }}}
            |   nothing
            |   {{{ author }}}<dws/>
                (
        }}<dws/>
        [[User:{{{ username }}}|{{{ username }}}]]<dws/>
        <sup><small>[[User talk:{{{ username }}}|{{ int: talkpagelinktext }}]]</small></sup><dws/>
        {{  #ifeq: {{{ username }}} | {{{ author | {{{ username }}} }}}
            |   nothing
            |   )
        }}
    |   {{{ author }}}
}}

Of course, it is just another style of formatting, but <dws/> allows using this style regularly and uniformly.

Download[edit]

git clone https://gerrit.wikimedia.org/r/p/mediawiki/extensions/WhiteSpace.git

Installation[edit]

Note: This extension requires patching MediaWiki core. The one-line patch introduces a parser hook which makes implementation of this extension possible. See Requests for comment/New hook: ParserBeforePreprocess for rationale.

In the MediaWiki root directory:

$ patch -p0 -i extensions/WhiteSpace/MediaWiki-1.18.1.patch

The add to your `LocalSettings.php':

require_once( "$IP/extensions/WhiteSpace/WhiteSpace.php" );

Configuration[edit]

No configuration.

Effect[edit]

Known issues[edit]

  • Patching the MediaWiki core required. :-(

License[edit]

GNU Affero General Public License, version 3 or any later version. See AGPL-3.0.txt file for the full license text.