Extension:TagParser

From MediaWiki.org

Jump to: navigation, search
Note: Wikimedia wikis use a different form of this extension; there may be slight differences in the #tag instructions from this page and the functionality on Wikimedia wikis.
Manual on MediaWiki Extensions
List of MediaWiki Extensions
TagParser

Release status: beta

Implementation Parser function
Description Making expressions in <tags> available by parser function
Author(s) René Kijewski
Version 0.1.2 (2007-12-04)
MediaWiki 1.8 to 1.11 (not needed in 1.12)
License zlib License
Download →Source code
Hooks used LanguageGetMagic

Contents

[edit] Purpose

There are several cases where it could be wanted that expressions like {{{1}}} would be evaluated in <tags> like imagemap or math.

This extension makes this available by the parser function {{#tag}}.

[edit] Usage

There are more or less 3 similar ways to use #tag:

general example
1st {{#tag:name}}

<name />

{{#tag:br}}

<br />

2nd {{#tag:name|content}}

<name>content</name>

{{#tag:imagemap|Image:Test.png

default [[Main Page]]
desc none}}

<imagemap>Image:Test.png
default [[Main Page]]
desc none<imagemap>

3rd {{#tag:name|attribute1|..|attributeN|content}}

<name attribute1 ... attributeN>content</name>

{{#tag:source|lang="js"| {{MediaWiki:Common.js}} }}

<source lang="js">Content of MediaWiki:Common.js</source>

4th {{#tag:|some content}}

empty result

 

[edit] Example

If Template:Imagemap contains

{{#tag:imagemap|
Image:{{{1|test.png}}}{{!}}{{{width|50px}}}{{!}}{{{alt|}}}
default [[{{{2|Main Page}}}|{{{caption|{{{target|Go to the Main Page}}}}}}]]
desc {{{description|bottom-left}}}
}}

you may write {{Imagemap|Test.png|{{FULLPAGENAME}}|width=100px|alt=&nbsp;|caption=This Page}} and you would get

as source code: rendered:
<imagemap>
Image:Test.png|100px|&nbsp;
default [[Extension:TagParser|This Page]]
desc none
</imagemap>
 About this image

[edit] Download instructions

Please cut and paste the code found below and place it in $IP/extension/TagParser/TagParser.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Installation

To install this extension, add the following line to LocalSettings.php:

require_once( $IP.'/extensions/TagParser/TagParser.php' );

before

require_once( $IP.'/extensions/ParserFunctions/ParserFunctions.php' );

[edit] Code

<?php
/*
 * Extension: TagParser
 * Copyright (C) 2007 [[de:User:Revolus]]
 *
 * Provided under the terms of the zlib/libpng License:
 *      http://www.opensource.org/licenses/zlib-license.php
 */
 
$wgExtensionFunctions[] = 'wfTagParser_Setup';
$wgHooks['LanguageGetMagic'][] = 'wfTagParser_Magic';
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'TagParser',
        'description' => 'Providing <tt>{{#tag}}</tt> to enable parser functions in XML-tags',
        'author' => 'René Kijewski',
        'url' => 'http://www.mediawiki.org/wiki/Extension:TagParser',
);
 
function wfTagParser_Setup() {
        global $wgParser;
        $wgParser->setFunctionHook('tag', 'wfTagParser_Render');
}
 
function wfTagParser_Magic(&$magicWords, $langCode) {
        $magicWords['tag'] = array(0, 'tag');
        return true;
}
 
function wfTagParser_Render( &$parser ) {
        $attributes = func_get_args();
        array_shift($attributes); // 0th parameter is the $parser
        $tag = array_shift($attributes); // the $tag is always given, but it may be empty
        $content = array_pop($attributes); // $content may be NULL
        if($tag === '') {
                $output = '';
        } else {
                if($content === NULL) {
                        $output = '<'. $tag .' />';
                } else {
                        $output = '<' .$tag. ((count($attributes) === 0) ? '' : ' '.implode(' ', $attributes)) .'>'. $content .'</'. $tag .'>';
                }
        }
        return array($output, 'noparse' => false, 'isHTML' => false);
}

[edit] See also

Personal tools