User:Paxed/ReplaceCharsBlock
Jump to navigation
Jump to search
------- ------- -------
ReplaceCharsBlock extension.
NOTE: Still experimental, DO NOT USE!
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.
<replacecharsblock> <replacements> b=foobar </replacements> abc </replacecharsblock>
would show "afoobarc"
- <replacecharsblock> accepts one parameter, newlines. With a value of 1, all newlines will be replaced with <br>. Any other value will be used as is.
- 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.
For example:
<div class='ttyscreen'> <replacecharsblock newlines="1"> <replacements> k=[[kobold|{{red|k}}]] f=[[cat|{{white|f}}]] @={{white|@}} |={{lightgray||}} default={{lightgray|$1}} </replacements> ------- ------- ------- ------- |...... |...... |...... |kkkkk. |.....| |.....| |.....| |kkkkk| |..k..| |.....| |..f..| |kkfkk| |.kfk.| |..kk.| |.kkk.| |k...k| |.k@k.| |..@fk| |..@..| |kk@kk| |.....| |..kk.| |.....| |kkkkk| -.----- -.----- -.----- -.----- </replacecharsblock> </div>
<replacements> k=k f=f @=@ |=| default=$1 </replacements>
------- ------- -------
|...... |...... |...... |kkkkk. |.....| |.....| |.....| |kkkkk| |..k..| |.....| |..f..| |kkfkk| |.kfk.| |..kk.| |.kkk.| |k...k| |.k@k.| |..@fk| |..@..| |kk@kk| |.....| |..kk.| |.....| |kkkkk| -.----- -.----- -.----- -.-----
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.1',
'author' => '[http://nethackwiki.com/wiki/User:Paxed Pasi Kallinen]',
'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 ) {
/*$parser->disableCache();*/
$foo = preg_split("/\n<\/replacements>\n/", $input, 2);
if (count($foo) == 2) {
$reps = array();
if (isset($argv['newlines'])) {
if (intval($argv['newlines']) == 1) $reps["\n"] = "<br>";
else $reps["\n"] = $argv['newlines'];
}
$len = preg_match_all("/^(.|default)=([^\n\r]+)$/m", $foo[0], $matches);
/*can't use $input = str_replace($matches[1], $matches[2], $foo[1]);*/
for ($i = 0; $i < count($matches[1]); $i++) {
$reps[$matches[1][$i]] = $matches[2][$i];
}
$input = "";
$str = $foo[1];
for ($i = 0; $i < strlen($str); $i++) {
$c = $str[$i];
if (isset($reps[$c])) $input .= $reps[$c];
else if (isset($reps['default']) && ($c >= ' ' && $c <= '~')) {
$input .= str_replace("$1", $c, $reps['default']);
} else {
$input .= $c;
}
}
return $parser->recursiveTagParse( $input );
} else {
return '<strong class="error">replacecharsblock format error, no replacements?</strong>';
}
}
?>