Difference between revisions of "User:Paxed/ReplaceCharsBlock"

From NetHackWiki
Jump to navigation Jump to search
m (put in one k̂=kobold shaman in the example to test use of unicode combining marks as modifiers)
m (slightly improved version)
Line 1: Line 1:
 
==ReplaceCharsBlock extension==
 
==ReplaceCharsBlock extension==
  
Adds a <nowiki><replacecharsblock></nowiki> -tag, which allows replacing (or ''translating'') characters in the block with other characters or strings. Replacements are defined first, and the block of text to be replaced after an empty line.
+
Adds a <nowiki><replacecharsblock></nowiki> -tag, which allows replacing (or ''translating'') characters and strings in the block with other characters or strings. Replacements are defined first, and the block of text to be replaced after an empty line.
  
 
{| class="wikitable"
 
{| class="wikitable"
Line 34: Line 34:
 
@={{white|@}}
 
@={{white|@}}
 
|={{lightgray|&amp;#124;}}
 
|={{lightgray|&amp;#124;}}
 +
bar=[[foo|{{yellow|foo}}]]
 
default={{lightgray|$1}}
 
default={{lightgray|$1}}
 
newline=<br>
 
newline=<br>
Line 39: Line 40:
 
------- ------- ------- -------
 
------- ------- ------- -------
 
|...... |...... |...... |kkkk̂k.
 
|...... |...... |...... |kkkk̂k.
|.....| |.....| |.....| |kkkkk|
+
|.....| |.bar.| |.....| |kkkkk|
 
|..k..| |.....| |..f..| |kkfkk|
 
|..k..| |.....| |..f..| |kkfkk|
 
|.kfk.| |..kk.| |.kkk.| |k...k|
 
|.kfk.| |..kk.| |.kkk.| |k...k|
Line 56: Line 57:
 
@={{white|@}}
 
@={{white|@}}
 
|={{lightgray|&#124;}}
 
|={{lightgray|&#124;}}
 +
bar=[[foo|{{yellow|foo}}]]
 
default={{lightgray|$1}}
 
default={{lightgray|$1}}
 
newline=<br>
 
newline=<br>
Line 61: Line 63:
 
------- ------- ------- -------
 
------- ------- ------- -------
 
|...... |...... |...... |kkkk̂k.
 
|...... |...... |...... |kkkk̂k.
|.....| |.....| |.....| |kkkkk|
+
|.....| |.bar.| |.....| |kkkkk|
 
|..k..| |.....| |..f..| |kkfkk|
 
|..k..| |.....| |..f..| |kkfkk|
 
|.kfk.| |..kk.| |.kkk.| |k...k|
 
|.kfk.| |..kk.| |.kkk.| |k...k|
Line 79: Line 81:
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
 
<?php
 
<?php
/**
+
/**                                                                                                                                        
  * ReplaceCharsBlock -- replace chars in a block with strings.
+
  * ReplaceCharsBlock -- replace chars in a block with strings.                                                                              
  *
+
  *                                                                                                                                          
 
  */
 
  */
+
 
 
if( !defined( 'MEDIAWIKI' ) ) {
 
if( !defined( 'MEDIAWIKI' ) ) {
die( "This is not a valid entry point to MediaWiki.\n" );
+
        die( "This is not a valid entry point to MediaWiki.\n" );
 
}
 
}
+
 
 
$wgHooks['ParserFirstCallInit'][] = 'wfReplaceCharsBlock';
 
$wgHooks['ParserFirstCallInit'][] = 'wfReplaceCharsBlock';
+
 
 
$wgExtensionCredits['parserhook'][] = array(
 
$wgExtensionCredits['parserhook'][] = array(
'name' => 'ReplaceCharsBlock',
+
        'name' => 'ReplaceCharsBlock',
'version' => '0.3',
+
        'version' => '0.3',
'author' => array(
+
        'author' => array(
'[http://nethackwiki.com/wiki/User:Paxed Pasi Kallinen]',
+
                '[http://nethackwiki.com/wiki/User:Paxed Pasi Kallinen]',
'[http://nethackwiki.com/wiki/User:Ilmari_Karonen Ilmari Karonen]',
+
                '[http://nethackwiki.com/wiki/User:Ilmari_Karonen Ilmari Karonen]',
),
+
        ),
'url' => 'http://nethackwiki.com/wiki/User:Paxed/ReplaceCharsBlock',
+
        'url' => 'http://nethackwiki.com/wiki/User:Paxed/ReplaceCharsBlock',
'description' => 'Replace characters in a block of text with strings.',
+
        'description' => 'Replace characters in a block of text with strings.',
 
);
 
);
+
 
 
function wfReplaceCharsBlock( &$parser ) {
 
function wfReplaceCharsBlock( &$parser ) {
$parser->setHook( 'replacecharsblock', 'renderCharReplacement' );
+
        $parser->setHook( 'replacecharsblock', 'renderCharReplacement' );
return true;
+
        return true;
 
}
 
}
+
 
function renderCharReplacement( $input, $argv, $parser ) {
+
function renderCharReplacement( $input, $argv, $parser, $frame ) {
/*$parser->disableCache();*/
+
        /*$parser->disableCache();*/
+
 
list( $defs, $text ) = preg_split( '/\n\s*\n/', $input, 2 );
+
        list( $defs, $text ) = preg_split( '/\n\s*\n/', $input, 2 );
if ( !isset( $text ) ) {
+
        if ( !isset( $text ) ) {
$text = $defs;
+
                $text = $defs;
$defs = "";
+
                $defs = "";
}
+
        }
+
 
$reps = array();
+
        $reps = array();
$default = false;
+
        $default = false;
+
 
$defs = explode( "\n", $defs );
+
        $defs = explode( "\n", $defs );
foreach ( $defs as $i => $line ) {
+
        foreach ( $defs as $i => $line ) {
if ( !preg_match( '/\S/u', $line ) ) {
+
                if ( !preg_match( '/\S/u', $line ) ) {
continue;
+
                        continue;
}
+
                }
if ( !preg_match( '/^(\X|default|newline)=(.+)$/u', $line, $match ) ) {
+
                if ( !preg_match( '/^([^=]+)=(.+)$/u', $line, $match ) ) {
return "<strong class='error'>Replacecharsblock error on line $i: invalid definition " . htmlspecialchars( $line ) . "</strong>";
+
                        return "<strong class='error'>Replacecharsblock error on line $i: invalid definition " . htmlspecialchars( $line ) . "</strong>";
}
+
                }
// wfDebug( __METHOD__ . ": mapping $match[1] to $match[2]\n" );
+
                // wfDebug( __METHOD__ . ": mapping $match[1] to $match[2]\n" );                                                            
if ( $match[1] === 'default' ) {
+
                if ( $match[1] === 'default' ) {
$default = $match[2];
+
                        $default = $match[2];
 
                 } else if ( $match[1] === 'newline' ) {
 
                 } else if ( $match[1] === 'newline' ) {
                        $reps["\n"] = $match[2];
+
                    $reps["\n"] = $match[2];
} else {
+
                } else {
$reps[ $match[1] ] = $match[2];
+
                        $reps[ $match[1] ] = $match[2];
}
+
                }
}
+
        }
+
 
if ( $default !== false ) {
+
        if ( $default !== false ) {
preg_match_all( '/(?>[^\\pM\\pC]\\pM*)/u', $text, $chars );
+
                preg_match_all( '/(?>[^\\pM\\pC]\\pM*)/u', $text, $chars );
$chars = array_diff( array_unique( $chars[0] ), array_keys( $reps ) );
+
                $chars = array_diff( array_unique( $chars[0] ), array_keys( $reps ) );
foreach ( $chars as $char ) {
+
                foreach ( $chars as $char ) {
$reps[$char] = str_replace( '$1', $char, $default );
+
                        $reps[$char] = str_replace( '$1', $char, $default );
// wfDebug( __METHOD__ . ": adding default map from $char to $reps[$char]\n" );
+
                        // wfDebug( __METHOD__ . ": adding default map from $char to $reps[$char]\n" );                                    
}
+
                }
}
+
        }
+
 
$text = strtr( $text, $reps );  
+
        $text = strtr( $text, $reps );
return $parser->recursiveTagParse( $text );
+
        return $parser->recursiveTagParse( $text, $frame );
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 09:17, 30 January 2011

ReplaceCharsBlock extension

Adds a <replacecharsblock> -tag, which allows replacing (or translating) characters and strings in the block with other characters or strings. Replacements are defined first, and the block of text to be replaced after an empty line.

<replacecharsblock>
b=foo

abcb
</replacecharsblock>

afoocfoo

  • default as a replacement: used for any characters that don't have explicitly defined replacement. $1 in the replacement will be substituted with the character which is being replaced.
  • newline as a replacement: each newline character will be replaced with this.

For example:

<div class='ttyscreen'>
<replacecharsblock>
k=[[kobold|{{red|k}}]]
k̂=[[kobold shaman|{{brightblue|k}}]]
f=[[cat|{{white|f}}]]
@={{white|@}}
|={{lightgray|&#124;}}
bar=[[foo|{{yellow|foo}}]]
default={{lightgray|$1}}
newline=<br>

------- ------- ------- -------
|...... |...... |...... |kkkk̂k.
|.....| |.bar.| |.....| |kkkkk|
|..k..| |.....| |..f..| |kkfkk|
|.kfk.| |..kk.| |.kkk.| |k...k|
|.k@k.| |..@fk| |..@..| |kk@kk|
|.....| |..kk.| |.....| |kkkkk|
-.----- -.----- -.----- -.-----
</replacecharsblock>
</div>

------- ------- ------- -------
|...... |...... |...... |kkkkk.
|.....| |.foo.| |.....| |kkkkk|
|..k..| |.....| |..f..| |kkfkk|
|.kfk.| |..kk.| |.kkk.| |k...k|
|.k@k.| |..@fk| |..@..| |kk@kk|
|.....| |..kk.| |.....| |kkkkk|
-.----- -.----- -.----- -.-----

TODO

  • Allow multiple replacement definitions for same char; use the replacements in the order they're defined, looping.
    • Maybe also in random order, though that'll invalidate cache.
  • Exceptions? (eg. maybe one 'k' is different from all the other 'k' chars) Format, how to define?

Code for ReplaceCharsBlock.php

<?php
/**                                                                                                                                          
 * ReplaceCharsBlock -- replace chars in a block with strings.                                                                               
 *                                                                                                                                           
 */

if( !defined( 'MEDIAWIKI' ) ) {
        die( "This is not a valid entry point to MediaWiki.\n" );
}

$wgHooks['ParserFirstCallInit'][] = 'wfReplaceCharsBlock';

$wgExtensionCredits['parserhook'][] = array(
        'name' => 'ReplaceCharsBlock',
        'version' => '0.3',
        'author' => array(
                '[http://nethackwiki.com/wiki/User:Paxed Pasi Kallinen]',
                '[http://nethackwiki.com/wiki/User:Ilmari_Karonen Ilmari Karonen]',
        ),
        'url' => 'http://nethackwiki.com/wiki/User:Paxed/ReplaceCharsBlock',
        'description' => 'Replace characters in a block of text with strings.',
);

function wfReplaceCharsBlock( &$parser ) {
        $parser->setHook( 'replacecharsblock', 'renderCharReplacement' );
        return true;
}

function renderCharReplacement( $input, $argv, $parser, $frame ) {
        /*$parser->disableCache();*/

        list( $defs, $text ) = preg_split( '/\n\s*\n/', $input, 2 );
        if ( !isset( $text ) ) {
                $text = $defs;
                $defs = "";
        }

        $reps = array();
        $default = false;

        $defs = explode( "\n", $defs );
        foreach ( $defs as $i => $line ) {
                if ( !preg_match( '/\S/u', $line ) ) {
                        continue;
                }
                if ( !preg_match( '/^([^=]+)=(.+)$/u', $line, $match ) ) {
                        return "<strong class='error'>Replacecharsblock error on line $i: invalid definition " . htmlspecialchars( $line ) . "</strong>";
                }
                // wfDebug( __METHOD__ . ": mapping $match[1] to $match[2]\n" );                                                             
                if ( $match[1] === 'default' ) {
                        $default = $match[2];
                } else if ( $match[1] === 'newline' ) {
                    $reps["\n"] = $match[2];
                } else {
                        $reps[ $match[1] ] = $match[2];
                }
        }

        if ( $default !== false ) {
                preg_match_all( '/(?>[^\\pM\\pC]\\pM*)/u', $text, $chars );
                $chars = array_diff( array_unique( $chars[0] ), array_keys( $reps ) );
                foreach ( $chars as $char ) {
                        $reps[$char] = str_replace( '$1', $char, $default );
                        // wfDebug( __METHOD__ . ": adding default map from $char to $reps[$char]\n" );                                      
                }
        }

        $text = strtr( $text, $reps );
        return $parser->recursiveTagParse( $text, $frame );
}