Difference between revisions of "User:Paxed/ReplaceCharsBlock"

From NetHackWiki
Jump to navigation Jump to search
(updated.)
(more updates)
Line 3: Line 3:
 
NOTE: Still experimental, DO NOT USE!
 
NOTE: Still experimental, DO NOT USE!
  
Adds a <nowiki><replacecharsblock></nowiki> -tag, which allows replacing characters in the block with other characters or strings, per char.
+
Adds a <nowiki><replacecharsblock></nowiki> -tag, which allows replacing characters in the block with other characters or strings, per char. Replacements are defined first, and the block of text that will be replaced after an empty line.
  
 
TODO:
 
TODO:
Line 9: Line 9:
 
**Maybe also in random order, though that'll invalidate cache.
 
**Maybe also in random order, though that'll invalidate cache.
  
 
+
{| class="wikitable"
 +
|width="50%"|
 
<pre>
 
<pre>
 
<replacecharsblock>
 
<replacecharsblock>
Line 17: Line 18:
 
</replacecharsblock>
 
</replacecharsblock>
 
</pre>
 
</pre>
would show "afoocfoo"
+
|width="50%"|
 +
<replacecharsblock>
 +
b=foo
 +
 
 +
abcb
 +
</replacecharsblock>
 +
|}
  
*<nowiki><replacecharsblock></nowiki> accepts one parameter, ''newlines''. With a value of '''1''', all newlines will be replaced with <nowiki><br></nowiki>. 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.
 
*''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:
 
For example:
 +
{| class="wikitable"
 +
|width="50%"|
 
<pre>
 
<pre>
 
<div class='ttyscreen'>
 
<div class='ttyscreen'>
<replacecharsblock newlines="1">
+
<replacecharsblock>
 
k=[[kobold|{{red|k}}]]
 
k=[[kobold|{{red|k}}]]
 
f=[[cat|{{white|f}}]]
 
f=[[cat|{{white|f}}]]
Line 31: Line 40:
 
|={{lightgray|&amp;#124;}}
 
|={{lightgray|&amp;#124;}}
 
default={{lightgray|$1}}
 
default={{lightgray|$1}}
 +
newline=<br>
  
 
------- ------- ------- -------
 
------- ------- ------- -------
Line 43: Line 53:
 
</div>
 
</div>
 
</pre>
 
</pre>
 
+
|width="50%"|
 
<div class='ttyscreen'>
 
<div class='ttyscreen'>
<replacecharsblock newlines="1">
+
<replacecharsblock>
 
k=[[kobold|{{red|k}}]]
 
k=[[kobold|{{red|k}}]]
 
f=[[cat|{{white|f}}]]
 
f=[[cat|{{white|f}}]]
Line 51: Line 61:
 
|={{lightgray|&#124;}}
 
|={{lightgray|&#124;}}
 
default={{lightgray|$1}}
 
default={{lightgray|$1}}
 +
newline=<br>
  
 
------- ------- ------- -------
 
------- ------- ------- -------
Line 62: Line 73:
 
</replacecharsblock>
 
</replacecharsblock>
 
</div>
 
</div>
 +
|}
  
 
==Code for ReplaceCharsBlock.php==
 
==Code for ReplaceCharsBlock.php==

Revision as of 09:17, 7 January 2011

ReplaceCharsBlock extension.

NOTE: Still experimental, DO NOT USE!

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

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>
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}}]]
f=[[cat|{{white|f}}]]
@={{white|@}}
|={{lightgray|&#124;}}
default={{lightgray|$1}}
newline=<br>

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

------- ------- ------- -------
|...... |...... |...... |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 );
}