Difference between revisions of "User:Paxed/ReplaceCharsBlock"

From NetHackWiki
Jump to navigation Jump to search
m (todo.)
(here, I played with it a little :))
Line 71: Line 71:
 
  *
 
  *
 
  */
 
  */
 
+
 
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.1',
+
'version' => '0.2devel',
'author' => '[http://nethackwiki.com/wiki/User:Paxed Pasi Kallinen]',
+
'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',
 
'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 ) {
    /*$parser->disableCache();*/
+
/*$parser->disableCache();*/
 
+
    $foo = preg_split("/\n<\/replacements>\n/", $input, 2);
+
list( $defs, $text ) = preg_split( '/\n\s*\n/', $input, 2 );
 
+
if ( !isset( $text ) ) {
    if (count($foo) == 2) {
+
$text = $defs;
 
+
$defs = "";
 +
}
 +
 
$reps = array();
 
$reps = array();
if (isset($argv['newlines'])) {
+
$default = false;
    if (intval($argv['newlines']) == 1) $reps["\n"] = "<br>";
+
if ( isset( $argv['newlines'] ) ) {
    else $reps["\n"] = $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);
+
$defs = explode( "\n", $defs );
 
+
foreach ( $defs as $i => $line ) {
/*can't use $input = str_replace($matches[1], $matches[2], $foo[1]);*/
+
if ( !preg_match( '/\S/u', $line ) ) {
 
+
continue;
for ($i = 0; $i < count($matches[1]); $i++) {
+
}
    $reps[$matches[1][$i]] = $matches[2][$i];
+
if ( !preg_match( '/^(\X|default)=(.+)$/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 {
 +
$reps[ $match[1] ] = $match[2];
 +
}
 
}
 
}
 
+
$input = "";
+
if ( $default !== false ) {
$str = $foo[1];
+
preg_match_all( '/(?>[^\\pM\\pC]\\pM*)/u', $text, $chars );
for ($i = 0; $i < strlen($str); $i++) {
+
$chars = array_diff( array_unique( $chars[0] ), array_keys( $reps ) );
    $c = $str[$i];
+
foreach ( $chars as $char ) {
    if (isset($reps[$c])) $input .= $reps[$c];
+
$reps[$char] = str_replace( '$1', $char, $default );
    else if (isset($reps['default']) && ($c >= ' ' && $c <= '~')) {
+
// wfDebug( __METHOD__ . ": adding default map from $char to $reps[$char]\n" );
$input .= str_replace("$1", $c, $reps['default']);
+
}
    } else {
 
$input .= $c;
 
    }
 
 
}
 
}
 
+
return $parser->recursiveTagParse( $input );
+
$text = strtr( $text, $reps );
    } else {
+
return $parser->recursiveTagParse( $text );
return '<strong class="error">replacecharsblock format error, no replacements?</strong>';
 
    }
 
 
}
 
}
 
?>
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 00:37, 7 January 2011

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|&#124;}}
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.2devel',
	'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 ) {
	/*$parser->disableCache();*/
 
	list( $defs, $text ) = preg_split( '/\n\s*\n/', $input, 2 );
	if ( !isset( $text ) ) {
		$text = $defs;
		$defs = "";
	}
 
	$reps = array();
	$default = false;
	if ( isset( $argv['newlines'] ) ) {
		if ( intval( $argv['newlines'] ) == 1 ) {
			$reps["\n"] = "<br>";
		} else {
			$reps["\n"] = $argv['newlines'];
		}
	}
 
	$defs = explode( "\n", $defs );
	foreach ( $defs as $i => $line ) {
		if ( !preg_match( '/\S/u', $line ) ) {
			continue;
		}
		if ( !preg_match( '/^(\X|default)=(.+)$/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 {
			$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 );
}