Extension:NumerAlpha

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
NumerAlpha

Release status: experimental

Implementation Tag
Description incremential tag (numbers, roman and alphabet)
Author(s) Thierry G. Veilleux (KronoxtTalk)
Last version 0.1 (15:31, 9 May 2009 (UTC))
MediaWiki 1.13+ didn't check other
License not specified
Download see below
Tags
<in> <ir> <ia>

Check usage (experimental)

Contents

[edit] Description

This is an incremental tag... it counts how many tag you insert in the page source. You can reset the count at any moment or begin to a specified number. You have three form :

  • a zero(s) padded number. (you can configure in the source code the length of your zero padded number. (0 = no zero padding)
  • Roman numeral (i, ii, iii, iv, etc.)
  • alphabetic (a,b,...,z,aa,ab,...,zz,...)

tags:

  • <in />
  • <ir />
  • <ia />

[edit] License

License not specified... you can use it, modify it, distribute it, etc.

[edit] Status

experimental

[edit] Why this extension?

First, it was for Semantic_Forms to add a table numbered when you use the multiple option... But you can use it in templates, using the {{#tag: }} magic word... and can be really useful.

You can reset it using the parameter reset, and restart the count with the parameter begin example:

<ir />
<ir />
<ir />
<ir />
<ir />
<ir />
<ir />
<ir />
<ir />
<ir />

<ir reset=1 />
<ir />
<ir />
<ir />
<ir />
<ir />

<ia />
<ia />
<ia />

<ir begin=10 />
<ir />
<ir />
<ir />

That will be render like

i ii iii iv v vi vii viii ix


i ii iii iv v


a b c

x xi xii xiii

So it's easy to list the alphabet:

<ia reset=1 /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia /><ia />

abcdefghijklmnopqrstuvwxyz

[edit] installation

You can install it by

  • copying the following php code:
  • save it with the NumerAlpha.php filename.
  • copy it into your extensions directory
  • and add the inclusion in your [localsettings.php] file. → require_once("$IP/extensions/NumerAlpha.php");
<?php
//NumerAlpha or Numeral Alpha extension!!
//KroNoxt extension kronoxt[at]izeta.org 
//free to use modify or anything if you want to give me credits you're free to do so.
//when you want to reset... you put the reset parameter to "1" //when you want to begin to a specified number, put this number in the begin parameter
 $wgExtensionCredits['parserhook'][] = array(
        'name' => 'NumerAlpha',
        'author' => 'KroNoxt',
        'description' => 'Incremental tag with zero padded number, roman and alpha numbers',
        'url' => 'http://www.mediawiki.org/wiki/Extension:NumerAlpha'
);
 
 
$wgExtNumerAlpha = new ExtNumerAlpha();
$wgExtensionFunctions[] = array( &$wgExtNumerAlpha, 'setup' );
class ExtNumerAlpha
{
  const VERSION = '0.1';
  private static $NumerACnt = 1; // start arabic numbering with "1"
  private static $NumerACnt2 = 0; //this is for roman character... 
  private static $NumerACnt3 = 0; //this for abc incremential... 
  public function setup() 
  {       
    global $wgParser;
$wgParser->setHook( 'ir', array( &$this, 'RomanRender' ) );
$wgParser->setHook( 'in', array( &$this, 'NumeralRender' ) );
$wgParser->setHook( 'ia', array( &$this, 'AlphaRender' ) );
}        //-->register these tags in the core <ir /> <in /><ia />
 
 
 function NumeralRender($text,$argv,&$parser){
        if (isset($argv['reset']) && $argv['reset'] == "yes" OR isset($argv['reset']) && $argv['reset'] == "1") {self::$NumerACnt = 1;}
        if (isset($argv['begin']) && $argv['begin'] != "") {self::$NumerACnt = $argv['begin'];}
        $num = self::$NumerACnt++;
        $num = intval($num);
        $length = 2;   ////////////////////YOU CAN CHANGE THE LENGHT for the zeros padding here.
        $output = str_pad($num,$length,"0",STR_PAD_LEFT);
        return  htmlspecialchars($output);
        }
 
function RomanRender($text,$argv,&$parser){
        if (isset($argv['reset']) && $argv['reset'] == "yes" OR isset($argv['reset']) && $argv['reset'] == "1") {self::$NumerACnt2 = 0;}
        if (isset($argv['begin']) && $argv['begin'] != "") {self::$NumerACnt2 = $argv['begin'];}
        $num = self::$NumerACnt2+;
        $n = intval($num);
     $result = '';
     $equival = array(
     'm' => 1000, 
     'cm' => 900, 
     'd' => 500, 
     'cd' => 400,
     'c' => 100, 
     'xc' => 90, 
     'l' => 50, 
     'xl' => 40,
     'x' => 10, 
     'ix' => 9, 
     'v' => 5, 
     'iv' => 4, 
     'i' => 1
     );
      foreach ($equival as $roma => $val) 
     {
         $concordances = intval($n / $val);
          $result .= str_repeat($roma, $concordances);
                 $n = $n % $val;
     }
 
        $output = $result;
        return  htmlspecialchars($output);
        }
 
function AlphaRender($text,$argv,&$parser){
        if (isset($argv['reset']) && $argv['reset'] = "yes" OR isset($argv['reset']) && $argv['reset'] = "1") {self::$NumerACnt3 = 0;}
        if (isset($argv['begin']) && $argv['begin'] != "") {self::$NumerACnt3 = $argv['begin'];}
        $num = self::$NumerACnt3++;
        $num = intval($num);
        $alpha = "";  
  while($num >= 1) {   
    $num = $num - 1;
    $alpha = chr(($num % 26)+97).$alpha; //we use the ascii table. //I don't remember where I pick this idea... but  it's not from me... well I coded it... 
    $num = $num / 26; }
 
        $output = $alpha;
        return  htmlspecialchars($output);
        }
 
 
 
}

For version 1.18, this works just the same

<?php
 
#USE THIS MARKUP SYNTAX INSTEAD
#<ia>hello</ia>
#<ia>world</ia>
#<ia reset=1>goodbye</ia>
#<ia>world</ia>
#<ia begin=11>jupiter</ia>
#<ia>jazz</ia>
#
#<in>hello</in>
#<in>world</in>
#<in reset=1>goodbye</in>
#<in>world</in>
#<in begin=11>jupiter</in>
#<in>jazz</in>
#
#<ir>hello</ir>
#<ir>world</ir>
#<ir reset=1>goodbye</ir>
#<ir>world</ir>
#<ir begin=11>jupiter</ir>
#<ir>jazz</ir>

 
$wgHooks['ParserFirstCallInit'][] = 'wfSampleParserInit';
 
// Hook our callback function into the parser
function wfSampleParserInit( Parser &$parser ) {
        // When the parser sees the <sample> tag, it executes
        // the wfSampleRender function (see below)
        $parser->setHook( 'ia', 'wfAlphaRender' );
        $parser->setHook( 'ir', 'wfRomanRender' );
        $parser->setHook( 'in', 'wfNumeralRender' );
        // Always return true from this function. The return value does not denote
        // success or otherwise have meaning - it just must always be true.
        return true;
}
$numer = array();
$numer[0] = 1; //alpha
$numer[1] = 1; //arabic
$numer[2] = 1; //roman
// Execute
function wfAlphaRender( $input, array $argv, Parser $parser, PPFrame $frame ) {
    global $numer;
 
    if (isset($argv['reset']) && $argv['reset'] == "yes" OR isset($argv['reset']) && $argv['reset'] == "1") {$numer[0] = 1;}
    if (isset($argv['begin']) && $argv['begin'] != "") {$numer[0] = $argv['begin'];}
    $num = $numer[0]++;
    $num = intval($num);
    $alpha = "";
    while($num >= 1) {
        $num = $num - 1;
        $alpha = chr(($num % 26)+97).$alpha; //we use the ascii table. //I don't remember where I pick this idea... but  it's not from me... well I coded it...
        $num = $num / 26;
    }
 
    $output = $alpha;
    return  htmlspecialchars($output .'. '. $input ).'<br/>';
}
 
 
function wfNumeralRender( $input, array $argv, Parser $parser, PPFrame $frame ) {
    global $numer;
 
    if (isset($argv['reset']) && $argv['reset'] == "yes" OR isset($argv['reset']) && $argv['reset'] == "1") {$numer[1] = 1;}
    if (isset($argv['begin']) && $argv['begin'] != "") {$numer[1] = $argv['begin'];}
    $num = $numer[1]++;
    $num = intval($num);
    $length = 1;   ////////////////////YOU CAN CHANGE THE LENGHT for the zeros padding here.
    $output = str_pad($num,$length,"0",STR_PAD_LEFT);
    return  htmlspecialchars($output .'. '. $input).'<br/>';
}
 
 
function wfRomanRender( $input, array $argv, Parser $parser, PPFrame $frame ) {
    global $numer;
 
    if (isset($argv['reset']) && $argv['reset'] == "yes" OR isset($argv['reset']) && $argv['reset'] == "1") {$numer[2] = 1;}
    if (isset($argv['begin']) && $argv['begin'] != "") {$numer[2] = $argv['begin'];}
    $num = $numer[2]++;
    $n = intval($num);
    $result = '';
    $equival = array(
    'm' => 1000, 
    'cm' => 900, 
    'd' => 500, 
    'cd' => 400,
    'c' => 100, 
    'xc' => 90, 
    'l' => 50, 
    'xl' => 40,
    'x' => 10, 
    'ix' => 9, 
    'v' => 5, 
    'iv' => 4, 
    'i' => 1
    );
    foreach ($equival as $roma => $val) 
    {
        $concordances = intval($n / $val);
         $result .= str_repeat($roma, $concordances);
                $n = $n % $val;
    }
 
    $output = $result;
 
    return  htmlspecialchars($output .'. '. $input).'<br/>';
}
?>
Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox