Extension talk:ShowHide

Latest comment: 14 years ago by 82.136.65.147 in topic 1.15.1

1.15.1

edit

Does anyone have this working in 1.15.1?--70.103.90.123 08:47, 22 January 2010 (UTC)Reply

To my opinion version v0.1 worked for me on 1.15.1 but then i upgraded my server to PHP 5.2.3 and now the whole text just disapears :-(

Please help.

--82.136.65.147 14:07, 23 July 2010 (UTC)Reply

The following discussion has been transferred from Meta-Wiki.
Any user names refer to users of that site, who are not necessarily users of MediaWiki.org (even if they share the same username).

MediaWiki 1.11.0

edit

If anyone has a working version of this code please paste the entire source here. Do not use that stupid patcher thing. Some of us still have to use windoze against our will.


Here is. This is nestable version, but it is used as normal. Note: Please edit content page to copy it.
If you use this, named "diff file from mediawiki 1.11.0" (new patch of below), nest of tags is possible. (That file is just small diff file. remove lines are appeared with '-' symbol, and added lines are appeared with '+' symbol. @@ line contains start line number. Thus hand patching is a little pain.) -- a.i.

<?php
# WikiMedia ShowHide extension v0.1
#
# Based on example code from
# http://meta.wikimedia.org/wiki/Write_your_own_MediaWiki_extension
# Contains code from MediaWiki's Skin.php and wikibits.js
#
# All other code is copyright © 2005 Nikola Smolenski <smolensk@eunet.yu>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# To install, copy the extension to your extensions directory and add line
# include("extensions/ShowHide.php");
# to the bottom of your LocalSettings.php
#
# Example syntax:
#
# <showhide>
# Some text (usually title) which will not be hidden
# <hide>Text which will be hidden</hide>
# </showhide>
#
# If <show></show> tags are used instead of <hide></hide>, the text will be
# shown by default
#
# For more information see its page at
# http://meta.wikimedia.org/wiki/ShowHide_Extension

$wgExtensionFunctions[] = "wfShowHideExtension";
$wgHooks['ParserAfterTidy'][] = 'addShowHideScriptHook';

function wfShowHideExtension() {
	$GLOBALS['wgParser']->setHook("showhide", "ShowHideExtension");
}

function ShowHideExtension($in, $params, &$parser) {
	static $numrun = 0;
	$run = $numrun++;

	$out = $parser->recursiveTagParse($in);
#	$out = $parser->unstrip($parser->recursiveTagParse($in), $parser->mStripState);
	if(
		(
			($s = strpos($out, "<show>")) !== FALSE &&
			strpos($out, "</show>") > $s
		) || (
			($h = strpos($out, "<hide>")) !== FALSE &&
			strpos($out, "</hide>") > $h
		)
	) {
		$GLOBALS['showhidescript'] = true;

		if($s !== FALSE)
			$act = "show";
		else
			$act = "hide";

		if (isset($params['showbuttontext']))
			$showbuttontext = $params['showbuttontext'];
		else
			$showbuttontext = wfMsg('showtoc');

		if (isset($params['hidebuttontext']))
			$hidebuttontext = $params['hidebuttontext'];
		else
			$hidebuttontext = wfMsg('hidetoc');

		$hideline = '<script type="text/javascript">showSHToggle("' . addslashes($showbuttontext) . '","' . addslashes($hidebuttontext) . '",' . $run . ')</script>';

		$out = rtrim(substr($out, 0, strpos($out, "<$act>")), "\n\r") . $hideline . substr($out, strpos($out, "<$act>"));
		$out = str_replace(
			array("<$act>", "</$act>"),
			array("<div id=\"shinside$run\">", "</div>"),
			$out
		);
		$out = "<span id=\"showhide$run\">" . trim($out) . "</span>";
		if($act == "hide")
			$out .= "<script type=\"text/javascript\">toggleSH($run)</script><p></p>";
	}
	return $out;
}

function addShowHideScriptHook(&$parser, &$text) {
	if ($GLOBALS['showhidescript']) {
		$GLOBALS['showhidescript'] = false;
		$text = "<script type=\"text/javascript\"><!--
shWas=new Array();
function showSHToggle(show,hide,num) {
        if(document.getElementById) {
                document.writeln('<span class=\'toctoggle\'>[<a href=\"javascript:toggleSH('+num+')\" class=\"internal\">' +
                '<span id=\"showlink'+num+'\" style=\"display:none;\">' + show + '</span>' +
                '<span id=\"hidelink'+num+'\">' + hide + '</span>' +
                '</a>]</span>');
        }
}
function toggleSH(num) {
        var shmain = document.getElementById('showhide'+num);
        var sh = document.getElementById('shinside'+num);
        var showlink=document.getElementById('showlink'+num);
        var hidelink=document.getElementById('hidelink'+num);
        if(sh.style.display == 'none') {
                sh.style.display = shWas[num];
                hidelink.style.display='';
                showlink.style.display='none';
                shmain.className = '';
        } else {
                shWas[num] = sh.style.display;
                sh.style.display = 'none';
                hidelink.style.display='none';
                showlink.style.display='';
                shmain.className = 'tochidden';
        }
} // --></script>" . $text;
	}

	return true;
}
?>

MediaWiki 1.8.2

edit

The 'UNIQ' issue seems to exist for any ShowHide content now (tested in Firefox 2.x and IE 6.x).

There is some "Other" patch code posted here that is claimed to work with 1.8.2. I haven't been able to find the code that v1.0patch3 is supposed to patch. There was a version jump from v0.1/v0.1.1 to v1.0patch3. This might have been a typo or the v1.0 code may have been mistakenly left unposted. The patch3 and patch4 versions both seem to have been contributed by "210.253.93.243" Jasonbrewer 15:45, 8 January 2007 (UTC)Reply

Sorry my typo in version number. This should be corrected v0.1+patch3. Please apply these pathces as

patch ShowHide.php ShowHide.php.v0.1+patch3
patch ShowHide.php ShowHide.php.v0.1+patch4

or by hand where ShowHide.php is version 0.1. These patches are still work on mediawiki 1.9.0 -- a.i.

patch and patch2 (Though this file is not exists in content page) are not needed. -- a.i.

On display some symbols are automatically changed from original source code by wiki. Please edit content page to copy the patches as other extention's source code. --a.i.


How exactly do I patch?

edit

Here are the md5sums I'm getting before I try the first patch:

 ShowHide.php (original v0.1): 14ae799c863dfe4412e1907e3ae13dd7
 ShowHide.php.v0.1+patch3:  48ea43296c1966d5e98bf4424af97f55

Here is my output from the first patch commmand:

 #patch ShowHide.php ShowHide.php.v0.1+patch3
 patching file ShowHide.php
 Hunk #2 FAILED at 36.
 Hunk #3 FAILED at 91.
 2 out of 3 hunks FAILED -- saving rejects to file ShowHide.php.rej

What am I doing wrong?


Jasonbrewer 20:51, 19 January 2007 (UTC)Reply

a.i., can you post your final/patched code somewhere or email it to me? My email address is in my profile.


Jasonbrewer 20:59, 19 January 2007 (UTC)Reply

OK, here is my patched (patch3 and patch4) code in running. -- a.i.

<?php
# WikiMedia ShowHide extension v0.1
#
# Based on example code from
# http://meta.wikimedia.org/wiki/Write_your_own_MediaWiki_extension
# Contains code from MediaWiki's Skin.php and wikibits.js
#
# All other code is copyright © 2005 Nikola Smolenski <smolensk@eunet.yu>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# To install, copy the extension to your extensions directory and add line
# include("extensions/ShowHide.php");
# to the bottom of your LocalSettings.php
#
# Example syntax:
#
# <showhide>
# Some text (usually title) which will not be hidden
# <hide>Text which will be hidden</hide>
# </showhide>
#
# If <show></show> tags are used instead of <hide></hide>, the text will be
# shown by default
#
# For more information see its page at
# http://meta.wikimedia.org/wiki/ShowHide_Extension

$wgExtensionFunctions[]="wfShowHideExtension";

function wfShowHideExtension()
{
	$GLOBALS['wgParser']->setHook("showhide","ShowHideExtension");
}

function ShowHideExtension($in,$params,&$parser)
{
	static $numrun=0;

	$out = $parser->unstrip($parser->recursiveTagParse($in),$parser->mStripState);
	if(
		(
			($s=strpos($out,"<show>"))!==FALSE &&
			strpos($out,"</show>")>$s
		) || (
			($h=strpos($out,"<hide>"))!==FALSE &&
			strpos($out,"</hide>")>$h
		)
	) {
		if($numrun==0) {
			$out=
"<script type=\"text/javascript\"><!--
shWas=new Array();
function showSHToggle(show,hide,num) {
	if(document.getElementById) {
		document.writeln('<span class=\'toctoggle\'>[<a href=\"javascript:toggleSH('+num+')\" class=\"internal\">' +
		'<span id=\"showlink'+num+'\" style=\"display:none;\">' + show + '</span>' +
		'<span id=\"hidelink'+num+'\">' + hide + '</span>' +
		'</a>]</span>');
	}
}
function toggleSH(num) {
	var shmain = document.getElementById('showhide'+num);
	var sh = document.getElementById('shinside'+num);
	var showlink=document.getElementById('showlink'+num);
	var hidelink=document.getElementById('hidelink'+num);
	if(sh.style.display == 'none') {
		sh.style.display = shWas[num];
		hidelink.style.display='';
		showlink.style.display='none';
		shmain.className = '';
	} else {
		shWas[num] = sh.style.display;
		sh.style.display = 'none';
		hidelink.style.display='none';
		showlink.style.display='';
		shmain.className = 'tochidden';
	}
} // --></script>
".$out;
		}
		$numrun++;

		if($s!==FALSE)
			$act="show";
		else
			$act="hide";

		if (isset($params['showbuttontext']))
			$showbuttontext = $params['showbuttontext'];
		else
			$showbuttontext = wfMsg('showtoc');

		if (isset($params['hidebuttontext']))
			$hidebuttontext = $params['hidebuttontext'];
		else
			$hidebuttontext = wfMsg('hidetoc');

		$hideline = ' <script type="text/javascript">showSHToggle("' . addslashes($showbuttontext) . '","' . addslashes($hidebuttontext) . '",' . $numrun . ')</script>';

		$out=rtrim(substr($out,0,strpos($out,"<$act>")),"\n\r") . $hideline . substr($out,strpos($out,"<$act>"));
		$out=str_replace(
			array("<$act>",                "</$act>"),
			array("<div id=\"shinside$numrun\">","</div>"),
			$out
		);
		$out="<span id=\"showhide$numrun\">$out</span>";
		if($act=="hide")
			$out.="<script type=\"text/javascript\">toggleSH($numrun)</script>";
	}
	return $out;
}
?>

MediaWiki 1.7.1

edit

All TeX-<Math>-Pngs before the <showhide>-section look like "�UNIQ2b16b3743d2879b7-math-5522ebf95c92a-QINU" All <math>-sections in and after the <showhide>-section work. All What to do?

it gives me the same UNIQ-QINU, with a few different extensions that use tags <poll> and <inputbox> but some simple tags still work <big> for example. And only if showhide occurs after them, not if it is before or in, just like above. The problem is more general. I would use template:hidden instead but i can't get it to work on my skin (anyone crack that one either?)65.96.127.125 19:34, 2 November 2006 (UTC)Reply

MediaWiki 1.5.4

edit

It does work with MediaWiki 1.5.4. too, but i wasnt able to get the parameters work i use in the template together with the ShowHide-function :( Any ideas (except of not working with parameters)? 11 January 2006 (UTC)

MediaWiki 1.5.3

edit

Does this extension work with MediaWiki 1.5.3?

I made it on 1.4.8, so I can't really tell. But it probably does, crash wiki runs on 1.5.2 and it works. I can test it in a week or so. Nikola 13:03, 20 December 2005 (UTC)Reply

with templates

edit

The extension, at this time, cannot be used inside a template that expects one or more parameters. Eg I created the template {{secret}} and put the following inside the template

<showhide>
'''secret:'''__HIDER__ <hide>{{{1}}}</hide>
</showhide>

and when I pass the parameter, like {{secret|my secret word}}, the output generated by the template displayes everything except the passed parameter. Instead of the parameter it shows/hides {{{1}}}. No value is substituted!

Can this be fixed? --202.1.192.199 13:50, 28 January 2006 (UTC)Reply

Same problem here... would be nice to use it like this {{answer|the answer is 42}} --84.154.160.1 20:55, 28 February 2006 (UTC)Reply

This problem is fixed to apply next patch (mediawiki 1.8.2). But if you use dangerous HTML tags, it treatment is changed (reference are lines of patch in Parser.php and removeHTMLtags() method of Sanitizer.php) --a.i.

--- Parser.php
+++ Parser.php.new
@@ -3096,6 +3096,7 @@
 				$text = preg_replace( '/<noinclude>.*?<\/noinclude>/s', '', $text );
 				$text = strtr( $text, array( '<includeonly>' => '' , '</includeonly>' => '' ) );
 
+				$text = $this->replaceVariables( $text, $assocArgs );
 				if( $this->ot['html'] || $this->ot['pre'] ) {
 					# Strip <nowiki>, <pre>, etc.
 					$text = $this->strip( $text, $this->mStripState );
@@ -3105,7 +3106,7 @@
 						$text = Sanitizer::removeHTMLcomments( $text );
 					}
 				}
-				$text = $this->replaceVariables( $text, $assocArgs );
+//				$text = $this->replaceVariables( $text, $assocArgs );
 
 				# If the template begins with a table or block-level
 				# element, it should be treated as beginning a new line.


Another Problem: on 1.5.4 v0.1 is not working...not even parsed i think...the __HIDER__ and all the tags are still there. v0.1.1 works at first attempt, but then this caching-bug comes again... :-/ does it work with 1.5.6 ? --84.154.165.109 18:59, 1 March 2006 (UTC)Reply

Can I change the show/hide text dynamically?

edit

For example, instead of a show/hide, what if the button was the name of the thing that was being shown and hidden? --69.250.40.75 19:06, 20 May 2006 (UTC)Reply

Nesting

edit

Is it possible to nest multiple layers of show-hide? I tried doing this at ChainsOfReason.org, but couldn't work it out. Also, the test page didn't work for me.

Anyway, I tried something like this:

: Statement __HIDER__
<hide>
::*Objection1 __HIDER__
<hide>
:::*CounterObjection1
:::*CounterObjection2
</hide>
::*Objection2
</hide>
</showhide>

What I want, is to click the Hider next to the statement to reveal the objections to that statement, and the Hider next to the first objection to reveal the counter-objections to that objection. Is it possible? It seems that the two hiders are linked--so either both are shown or both are hidden. Any thoughts? Thanks. AdamRetchless 15:30, 28 February 2007 (UTC)Reply


I think that mediawiki is not support nesting tags. But to use below, tag nesting is possible. To use this, Open the Parser.php in the include directory of mediawiki. Then replace the extractTagsAndParams function with below. And use new ShowHide.php in below for this. I checked these programs in 1.9.3. -- a.i.

Warning: This extractTagsAndParams code has a bug on comment treatment. Please see below for fixed version. -- a. i.

	function extractTagsAndParams( $elements, $text, &$matches, $uniq_prefix = '' ) {
		static $n = 1;
		$stripped = '';
		$matches = array();

		$taglist = implode( '|', $elements );
		$start = "/<($taglist)(\\s+[^>]*?|\\s*?)(\/?>)|<(!--)/i";

		while ( '' != $text ) {
			$p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE );
			$stripped .= $p[0][0];
			if( count( $p ) < 5 )
				break;

			if( count( $p ) > 5 ) {
				// comment
				$element    = $p[4][0];
				$attributes = '';
				$close      = '';
				$inside     = $p[5][0];
			} else {
				// tag
				$element    = $p[1][0];
				$attributes = $p[2][0];
				$close      = $p[3][0];
				$inside     = $p[4][0];
			}

			$marker = "$uniq_prefix-$element-" . sprintf('%08X', $n++) . '-QINU';
			$stripped .= $marker;

			if ( $close === '/>' ) {
				// Empty element tag, <tag />
				$content = null;
				$text = $inside;
				$tail = null;
			} else {
				if( $element == '!--' )
					$q = preg_split( '/(-->)/', $inside, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE );
				else {
					$nest = 1;
					$length = 0;
					do {
						// search end tag
						if ( count( $q = preg_split( "/(<\\/$element\\s*>)/i", $inside, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE ) ) < 3 )
							break;

						// search start tag
						$r = preg_split( "/(<$element(\\s+[^>]*?|\\s*?)(\/?)>)|(<!--)/i", $inside, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE );
						if ( count( $r ) == 6 and $r[4][1] < $q[1][1] ) {	// the next tag is a comment
							$length += $r[5][1];
							// skip the comment
							if ( count( $q = preg_split( '/(-->)/', $r[5][0], 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE ) ) < 3 )
								break;

							$inside = $q[2][0];
							$length += $q[2][1];
						} else if ( count( $r ) < 5 or $q[1][1] < $r[1][1] ) {	// the next tag is an end tag
							$nest--;
							$inside = $q[2][0];
							$length += $q[1][1];
							if ( $nest > 0 )	// the last tag is not content
								$length += $q[2][1] - $q[1][1];
						} else {	// the next tag is a start tag
							$nest++;
							$inside = $r[4][0];
							$length += $r[4][1];
						}
					} while ( $nest > 0 );

					$content = count( $q ) < 3 ? $p[4][0] : substr( $text, $p[4][1], $length );
				}

				if( count( $q ) < 3 ) {
					# No end tag -- let it run out to the end of the text.
					$tail = '';
					$text = '';
				} else {
					$tail = $q[1][0];
					$text = $q[2][0];
				}
			}

			$matches[$marker] = array( $element,
				$content,
				Sanitizer::decodeTagAttributes( $attributes ),
				"<$element$attributes$close$content$tail" );
		}
		return $stripped;
	}


I recognized that the method of insert javascript in ShowHideExtension function of this code is not work in some condition. Please see below for fixed version. -- a. i.

<?php
# WikiMedia ShowHide extension v0.1
#
# Based on example code from
# http://meta.wikimedia.org/wiki/Write_your_own_MediaWiki_extension
# Contains code from MediaWiki's Skin.php and wikibits.js
#
# All other code is copyright © 2005 Nikola Smolenski <smolensk@eunet.yu>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# To install, copy the extension to your extensions directory and add line
# include("extensions/ShowHide.php");
# to the bottom of your LocalSettings.php
#
# Example syntax:
#
# <showhide>
# Some text (usually title) which will not be hidden
# <hide>Text which will be hidden</hide>
# </showhide>
#
# If <show></show> tags are used instead of <hide></hide>, the text will be
# shown by default
#
# For more information see its page at
# http://meta.wikimedia.org/wiki/ShowHide_Extension

$wgExtensionFunctions[]="wfShowHideExtension";

function wfShowHideExtension()
{
	$GLOBALS['wgParser']->setHook("showhide","ShowHideExtension");
}

function ShowHideExtension($in,$params,&$parser)
{
	static $numrun=0;

	$out='';
	if($numrun==0) {
		$out=
"<script type=\"text/javascript\"><!--
shWas=new Array();
function showSHToggle(show,hide,num) {
	if(document.getElementById) {
		document.writeln('<span class=\'toctoggle\'>[<a href=\"javascript:toggleSH('+num+')\" class=\"internal\">' +
		'<span id=\"showlink'+num+'\" style=\"display:none;\">' + show + '</span>' +
		'<span id=\"hidelink'+num+'\">' + hide + '</span>' +
		'</a>]</span>');
	}
}
function toggleSH(num) {
	var shmain = document.getElementById('showhide'+num);
	var sh = document.getElementById('shinside'+num);
	var showlink=document.getElementById('showlink'+num);
	var hidelink=document.getElementById('hidelink'+num);
	if(sh.style.display == 'none') {
		sh.style.display = shWas[num];
		hidelink.style.display='';
		showlink.style.display='none';
		shmain.className = '';
	} else {
		shWas[num] = sh.style.display;
		sh.style.display = 'none';
		hidelink.style.display='none';
		showlink.style.display='';
		shmain.className = 'tochidden';
	}
} // --></script>";
	}
	$run = $numrun++;

	$out .= $parser->unstrip($parser->recursiveTagParse($in),$parser->mStripState);
	if(
		(
			($s=strpos($out,"<show>"))!==FALSE &&
			strpos($out,"</show>")>$s
		) || (
			($h=strpos($out,"<hide>"))!==FALSE &&
			strpos($out,"</hide>")>$h
		)
	) {
		if($s!==FALSE)
			$act="show";
		else
			$act="hide";

		if (isset($params['showbuttontext']))
			$showbuttontext = $params['showbuttontext'];
		else
			$showbuttontext = wfMsg('showtoc');

		if (isset($params['hidebuttontext']))
			$hidebuttontext = $params['hidebuttontext'];
		else
			$hidebuttontext = wfMsg('hidetoc');

		$hideline = '<script type="text/javascript">showSHToggle("' . addslashes($showbuttontext) . '","' . addslashes($hidebuttontext) . '",' . $run . ')</script>';

		$out=rtrim(substr($out,0,strpos($out,"<$act>")),"\n\r") . $hideline . substr($out,strpos($out,"<$act>"));
		$out=str_replace(
			array("<$act>","</$act>"),
			array("<div id=\"shinside$run\">","</div>"),
			$out
		);
		$out="<span id=\"showhide$run\">".trim($out)."</span>";
		if($act=="hide")
			$out.="<script type=\"text/javascript\">toggleSH($run)</script><p></p>";
	}
	return $out;
}
?>

Bugs fixed codes of nesting

This extractTagsAndParams function is fixed a bug on comment treatment. -- a. i.

	function extractTagsAndParams( $elements, $text, &$matches, $uniq_prefix = '' ) {
		static $n = 1;
		$stripped = '';
		$matches = array();

		$taglist = implode( '|', $elements );
		$start = "/<($taglist)(\\s+[^>]*?|\\s*?)(\/?>)|<(!--)/i";

		while ( '' != $text ) {
			$p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE );
			$stripped .= $p[0][0];
			if( count( $p ) < 5 )
				break;

			if( count( $p ) > 5 ) {
				// comment
				$element    = $p[4][0];
				$attributes = '';
				$close      = '';
				$inside     = $p[5][0];
			} else {
				// tag
				$element    = $p[1][0];
				$attributes = $p[2][0];
				$close      = $p[3][0];
				$inside     = $p[4][0];
			}

			$marker = "$uniq_prefix-$element-" . sprintf('%08X', $n++) . '-QINU';
			$stripped .= $marker;

			if ( $close === '/>' ) {
				// Empty element tag, <tag />
				$content = null;
				$text = $inside;
				$tail = null;
			} else {
				if( $element == '!--' ) {
					$q = preg_split( '/(-->)/', $inside, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE );
					$content = $q[0][0];
				} else {
					$nest = 1;
					$length = 0;
					do {
						// search end tag
						if ( count( $q = preg_split( "/(<\\/$element\\s*>)/i", $inside, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE ) ) < 3 )
							break;

						// search start tag
						$r = preg_split( "/(<$element(\\s+[^>]*?|\\s*?)(\/?)>)|(<!--)/i", $inside, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE );
						if ( count( $r ) == 6 and $r[4][1] < $q[1][1] ) {	// the next tag is a comment
							$length += $r[5][1];
							// skip the comment
							if ( count( $q = preg_split( '/(-->)/', $r[5][0], 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE ) ) < 3 )
								break;

							$inside = $q[2][0];
							$length += $q[2][1];
						} else if ( count( $r ) < 5 or $q[1][1] < $r[1][1] ) {	// the next tag is an end tag
							$nest--;
							$inside = $q[2][0];
							$length += $q[1][1];
							if ( $nest > 0 )	// the last tag is not content
								$length += $q[2][1] - $q[1][1];
						} else {	// the next tag is a start tag
							$nest++;
							$inside = $r[4][0];
							$length += $r[4][1];
						}
					} while ( $nest > 0 );

					$content = count( $q ) < 3 ? $p[4][0] : substr( $text, $p[4][1], $length );
				}

				if( count( $q ) < 3 ) {
					# No end tag -- let it run out to the end of the text.
					$tail = '';
					$text = '';
				} else {
					$tail = $q[1][0];
					$text = $q[2][0];
				}
			}

			$matches[$marker] = array( $element,
				$content,
				Sanitizer::decodeTagAttributes( $attributes ),
				"<$element$attributes$close$content$tail" );
		}
		return $stripped;
	}

This ShowHideExtension is fixed a bug on insert javascript in some condition. -- a. i.

<?php
# WikiMedia ShowHide extension v0.1
#
# Based on example code from
# http://meta.wikimedia.org/wiki/Write_your_own_MediaWiki_extension
# Contains code from MediaWiki's Skin.php and wikibits.js
#
# All other code is copyright © 2005 Nikola Smolenski <smolensk@eunet.yu>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# To install, copy the extension to your extensions directory and add line
# include("extensions/ShowHide.php");
# to the bottom of your LocalSettings.php
#
# Example syntax:
#
# <showhide>
# Some text (usually title) which will not be hidden
# <hide>Text which will be hidden</hide>
# </showhide>
#
# If <show></show> tags are used instead of <hide></hide>, the text will be
# shown by default
#
# For more information see its page at
# http://meta.wikimedia.org/wiki/ShowHide_Extension

$wgExtensionFunctions[] = "wfShowHideExtension";
$wgHooks['ParserAfterTidy'][] = 'addShowHideScriptHook';

function wfShowHideExtension() {
	$GLOBALS['wgParser']->setHook("showhide", "ShowHideExtension");
}

function ShowHideExtension($in, $params, &$parser) {
	static $numrun = 0;
	$run = $numrun++;

	$out = $parser->unstrip($parser->recursiveTagParse($in), $parser->mStripState);
	if(
		(
			($s = strpos($out, "<show>")) !== FALSE &&
			strpos($out, "</show>") > $s
		) || (
			($h = strpos($out, "<hide>")) !== FALSE &&
			strpos($out, "</hide>") > $h
		)
	) {
		$GLOBALS['showhidescript'] = true;

		if($s !== FALSE)
			$act = "show";
		else
			$act = "hide";

		if (isset($params['showbuttontext']))
			$showbuttontext = $params['showbuttontext'];
		else
			$showbuttontext = wfMsg('showtoc');

		if (isset($params['hidebuttontext']))
			$hidebuttontext = $params['hidebuttontext'];
		else
			$hidebuttontext = wfMsg('hidetoc');

		$hideline = '<script type="text/javascript">showSHToggle("' . addslashes($showbuttontext) . '","' . addslashes($hidebuttontext) . '",' . $run . ')</script>';

		$out = rtrim(substr($out, 0, strpos($out, "<$act>")), "\n\r") . $hideline . substr($out, strpos($out, "<$act>"));
		$out = str_replace(
			array("<$act>", "</$act>"),
			array("<div id=\"shinside$run\">", "</div>"),
			$out
		);
		$out = "<span id=\"showhide$run\">" . trim($out) . "</span>";
		if($act == "hide")
			$out .= "<script type=\"text/javascript\">toggleSH($run)</script><p></p>";
	}
	return $out;
}

function addShowHideScriptHook(&$parser, &$text) {
	if ($GLOBALS['showhidescript']) {
		$GLOBALS['showhidescript'] = false;
		$text = "<script type=\"text/javascript\"><!--
shWas=new Array();
function showSHToggle(show,hide,num) {
        if(document.getElementById) {
                document.writeln('<span class=\'toctoggle\'>[<a href=\"javascript:toggleSH('+num+')\" class=\"internal\">' +
                '<span id=\"showlink'+num+'\" style=\"display:none;\">' + show + '</span>' +
                '<span id=\"hidelink'+num+'\">' + hide + '</span>' +
                '</a>]</span>');
        }
}
function toggleSH(num) {
        var shmain = document.getElementById('showhide'+num);
        var sh = document.getElementById('shinside'+num);
        var showlink=document.getElementById('showlink'+num);
        var hidelink=document.getElementById('hidelink'+num);
        if(sh.style.display == 'none') {
                sh.style.display = shWas[num];
                hidelink.style.display='';
                showlink.style.display='none';
                shmain.className = '';
        } else {
                shWas[num] = sh.style.display;
                sh.style.display = 'none';
                hidelink.style.display='none';
                showlink.style.display='';
                shmain.className = 'tochidden';
        }
} // --></script>" . $text;
	}
}
?>


An example is

<showhide>
A
<hide>
B
<showhide>
C
<hide>
D
</hide>
</showhide>
E
</hide>
</showhide>

Similar extension

edit

If I understood Extension:ShowHide correctly, Extension:ToggleDisplay offers a similar functionality with some more configuration options. RV1971 17:18, 4 September 2007 (UTC)Reply

Return to "ShowHide" page.