Extension:ShowHide

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
ShowHide

Release status: unknown

Implementation Tag
Description This extension enables creation of wiki elements which could be shown and hidden.
Author(s) Nikola Smolenski
License No license specified
Download see below

The ShowHide extension enables creation of wiki elements which could be shown and hidden, like Table of Contents.

The idea for this came after Avala was criticised for creating a list of non-aligned countries, similar to the list of NATO members. On the one hand, the list is perfectly logical, on the other, completely impractical. This extension would make such lists possible. I remembered this idea when I saw some articles on Serbian Wikipedia, for example Бановићи, and the bottom of the page on George H. W. Bush.

The extension could be seen in action at Wikimedia SCG's crash wiki ShowHide test page. Due to its nature, no examples can be given here; you should visit the test page to see several examples and test how it works.

      • The link above is dead ***

Note that the extension is not thoroughly tested, and there might be bugs. Currently, it is in use on the Veggie Van Gogh wiki.

The request to implement this feature in Mediawiki is at bug #1257.

Contents

[edit] Related

IIRC there are lots of related extensions / functions in MW. Unfortunately I can't find them right now. Please replace this text with some related stuff.

[edit] Syntax

Syntax is a bit more advanced than what is usual for an extension:

<showhide>
Some text (usually title) which will not be hidden __HIDER__
<hide>Text which will be hidden</hide>
</showhide>

__HIDER__ is the place where link will be put which will show or hide the text between <hide></hide> tags. In the above example, it will be the string "Text which will be hidden".

If <show></show> tags are used, the text will be shown by default, and could be hidden by clicking on hider.


I created a patch that remove __HIDER__ from syntax --a.i.

--- ShowHide.php.v1.0
+++ ShowHide.php.new
@@ -43,14 +43,13 @@
 
        $out=$wgOut->parse($in);
        if(
-               strpos($out,"__HIDER__")!==FALSE &&
-               ((
+               (
                        ($s=strpos($out,"&lt;show&gt;"))!==FALSE &&
                        strpos($out,"&lt;/show&gt;")>$s
                ) || (
                        ($h=strpos($out,"&lt;hide&gt;"))!==FALSE &&
                        strpos($out,"&lt;/hide&gt;")>$h
-               ))
+               )
        ) {
                if($numrun==0) {
                        $out=
@@ -93,7 +92,7 @@
 
                $hideline = ' <script type="text/javascript">showSHToggle("' . addslashes( wfMsg('showtoc') ) . '","' . addsl
ashes( wfMsg('hidetoc') ) . '",' . $numrun . ')</script>';
 
-               $out=str_replace("__HIDER__","$hideline",$out);
+               $out=substr($out,0,strpos($out,"&lt;$act&gt;")) . $hideline . substr($out,strpos($out,"&lt;$act&gt;"));
                $out=str_replace(
                        array("&lt;$act&gt;",                "&lt;/$act&gt;"),
                        array("<div id=\"shinside$numrun\">","</div>"),

[edit] Source

Helpful hint: the copyright symbol and the script tags are messing up the display. Edit this page to copy the code! --Bytesmiths 03:14, 20 December 2005 (UTC)

[edit] v 0.1

Sometimes it won't work (tested in PHP 5.x) when you forget about html_entity_decode() and $wgOut->addHTML() instead of just pasting JS into $out --Smerf

Please note, that this extension breaks XHTML 1.0 Transitional compatibility!! --Smerf

<?php
# MediaWiki 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 __HIDER__
# <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://www.mediawiki.org/wiki/Extension:ShowHide
 
$wgExtensionFunctions[]="wfShowHideExtension";
 
function wfShowHideExtension()
{
	$GLOBALS['wgParser']->setHook("showhide","ShowHideExtension");
}
 
function ShowHideExtension($in)
{
	global $wgOut;
	static $numrun=0;
 
	$out=$wgOut->parse($in);
	if(
		strpos($out,"__HIDER__")!==FALSE &&
		((
			($s=strpos($out,"&lt;show&gt;"))!==FALSE &&
			strpos($out,"&lt;/show&gt;")>$s
		) || (
			($h=strpos($out,"&lt;hide&gt;"))!==FALSE &&
			strpos($out,"&lt;/hide&gt;")>$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";
 
		$hideline = ' <script type="text/javascript">showSHToggle("' . addslashes( wfMsg('showtoc') ) . '","' . addslashes( wfMsg('hidetoc') ) . '",' . $numrun . ')</script>';
 
		$out=str_replace("__HIDER__","$hideline",$out);
		$out=str_replace(
			array("&lt;$act&gt;",                "&lt;/$act&gt;"),
			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;
}

[edit] v 0.1.1

PHP5 version with corrections according to Smerf's comments.

Note the following version does not work correctly for me on MediaWiki 1.5. The following will work correctly on the first load, but after loading from cache, the one-time Javascript code inserted by $wgOut->addHtml does not get added leading to problems. v.0.1 above works fine for me.
<?php
# MediaWiki ShowHide extension v0.1.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 __HIDER__
# <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://www.mediawiki.org/wiki/Extension:ShowHide
 
$wgExtensionFunctions[]="wfShowHideExtension";
 
function wfShowHideExtension()
{
	$GLOBALS['wgParser']->setHook("showhide","ShowHideExtension");
}
 
function ShowHideExtension($in)
{
	global $wgOut;
	static $numrun=0;
 
	$out=$wgOut->parse($in);
	if(
                strpos($out,"__HIDER__")!==FALSE &&
                ((
                        ($s=strpos($out,htmlentities("<show>")))!==FALSE &&
                        strpos($out,htmlentities("</show>"))>$s
                ) || (
                        ($h=strpos($out,htmlentities("<hide>")))!==FALSE &&
                        strpos($out,htmlentities("</hide>"))>$h
                ))
        ) {
                if($numrun==0) {
                        $wgOut->addHTML(
"<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>
");
                }
                $numrun++;
 
                if($s!==FALSE)
                        $act="show";
                else
                        $act="hide";
 
                $hideline = ' <script type="text/javascript">showSHToggle("' . addslashes( wfMsg('showtoc') ) . '","' . addslashes( wfMsg('hidetoc') ) . '",' . $numrun . ')</script>';
 
                $out=str_replace("__HIDER__","$hideline",$out);
                $out=str_replace(
                        array(htmlentities("<$act>"),                htmlentities("</$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;
}

[edit] Other

Yet another version, by Austin Che, exists at http://austin.mit.edu/mediawiki/showhide.php.txt


without $wgOut (patch 3; ParserBeforeStrip and ParserAfterStrip hooks ware enabled) This patch was tested on mediawiki 1.8.2 (firefox2 and Konqueror) --a.i.

--- ShowHide.php.v1.0
+++ ShowHide.php.v1.0+patch3
@@ -19,7 +19,7 @@
 # Example syntax:
 #
 # <showhide>
-# Some text (usually title) which will not be hidden __HIDER__
+# Some text (usually title) which will not be hidden
 # <hide>Text which will be hidden</hide>
 # </showhide>
 #
@@ -36,21 +36,19 @@
 	$GLOBALS['wgParser']->setHook("showhide","ShowHideExtension");
 }
 
-function ShowHideExtension($in)
+function ShowHideExtension($in,$argv,&$parser)
 {
-	global $wgOut;
 	static $numrun=0;
 
-	$out=$wgOut->parse($in);
+	$out = $parser->unstrip($parser->recursiveTagParse($in),$parser->mStripState);
 	if(
-		strpos($out,"__HIDER__")!==FALSE &&
-		((
+		(
 			($s=strpos($out,"&lt;show&gt;"))!==FALSE &&
 			strpos($out,"&lt;/show&gt;")>$s
 		) || (
 			($h=strpos($out,"&lt;hide&gt;"))!==FALSE &&
 			strpos($out,"&lt;/hide&gt;")>$h
-		))
+		)
 	) {
 		if($numrun==0) {
 			$out=
@@ -93,7 +91,7 @@
 
 		$hideline = ' <script type="text/javascript">showSHToggle("' . addslashes( wfMsg('showtoc') ) . '","' . addslashes( wfMsg('hidetoc') ) . '",' . $numrun . ')</script>';
 
-		$out=str_replace("__HIDER__","$hideline",$out);
+		$out=rtrim(substr($out,0,strpos($out,"&lt;$act&gt;")),"\n\r") . $hideline . substr($out,strpos($out,"&lt;$act&gt;"));
 		$out=str_replace(
 			array("&lt;$act&gt;",                "&lt;/$act&gt;"),
 			array("<div id=\"shinside$numrun\">","</div>"),

And if you want to change button text, apply next patch too --a.i.

Additional syntax is

<showhide showbuttontext="show button text" hidebuttontext="hide button text">

or

<showhide showbuttontext="show button text">

.

<showhide hidebuttontext="hide button text">

is also possible.

--- ShowHide.php.v1.0+patch3
+++ ShowHide.php.v1.0+patch4
@@ -36,7 +36,7 @@
 	$GLOBALS['wgParser']->setHook("showhide","ShowHideExtension");
 }
 
-function ShowHideExtension($in,$argv,&$parser)
+function ShowHideExtension($in,$params,&$parser)
 {
 	static $numrun=0;
 
@@ -89,7 +89,17 @@
 		else
 			$act="hide";
 
-		$hideline = ' <script type="text/javascript">showSHToggle("' . addslashes( wfMsg('showtoc') ) . '","' . addslashes( wfMsg('hidetoc') ) . '",' . $numrun . ')</script>';
+		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,"&lt;$act&gt;")),"\n\r") . $hideline . substr($out,strpos($out,"&lt;$act&gt;"));
 		$out=str_replace(


I created a new patch of below, and have overridden old below version. But it was reverted automatically unfortunately. Please remove below that old unnecessary version someone can remove it (and also this text). -- a.i.

without $wgOut --a.i.

--- ShowHide.php.v1.0
+++ ShowHide.php.v1.0+patch
@@ -1,5 +1,5 @@
 <?php
-# WikiMedia ShowHide extension v0.1
+# WikiMedia ShowHide extension v0.1 + patch
 #
 # Based on example code from
 # http://meta.wikimedia.org/wiki/Write_your_own_MediaWiki_extension
@@ -7,6 +7,10 @@
 #
 # All other code is copyright © 2005 Nikola Smolenski <smolensk@eunet.yu>
 #
+# patch:
+# Contains code from MediaWiki's Parser.php
+# a.i.
+#
 # 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
@@ -19,7 +23,7 @@
 # Example syntax:
 #
 # <showhide>
-# Some text (usually title) which will not be hidden __HIDER__
+# Some text (usually title) which will not be hidden
 # <hide>Text which will be hidden</hide>
 # </showhide>
 #
@@ -29,28 +33,41 @@
 # For more information see its page at
 # http://meta.wikimedia.org/wiki/ShowHide_Extension
 
-$wgExtensionFunctions[]="wfShowHideExtension";
+$wgHooks['ParserBeforeTidy'][] = 'ShowHideHook';
 
-function wfShowHideExtension()
-{
-	$GLOBALS['wgParser']->setHook("showhide","ShowHideExtension");
+function ShowHideHook( &$parser, &$text ) {
+	$text = str_ireplace(
+		array("&lt;showhide&gt;","&lt;/showhide&gt;","&lt;show&gt;","&lt;/show&gt;","&lt;hide&gt;","&lt;/hide&gt;"),
+		array("<showhide>","</showhide>","<show>","</show>","<hide>","</hide>"),
+		$text);
+	$matches = array();
+	$text = Parser::extractTagsAndParams(array('showhide'), $text, $matches, $parser->mUniqPrefix);
+	$state = array();
+	foreach( $matches as $marker => $data ) {
+		list( $element, $content, $params, $tag ) = $data;
+		$tagName = strtolower( $element );
+		switch( $tagName ) {
+			case 'showhide':
+				$state['general'][$marker] = ShowHideExtension($content, $params, $parser);
+				break;
+		}
+	}
+	$text = $parser->unstrip($text, $state);
 }
 
-function ShowHideExtension($in)
+function ShowHideExtension($in, $argv, &$parser)
 {
-	global $wgOut;
 	static $numrun=0;
 
-	$out=$wgOut->parse($in);
+	$out=$in;
 	if(
-		strpos($out,"__HIDER__")!==FALSE &&
-		((
-			($s=strpos($out,"&lt;show&gt;"))!==FALSE &&
-			strpos($out,"&lt;/show&gt;")>$s
+		(
+			($s=strpos($out,"<show>"))!==FALSE &&
+			strpos($out,"</show>")>$s
 		) || (
-			($h=strpos($out,"&lt;hide&gt;"))!==FALSE &&
-			strpos($out,"&lt;/hide&gt;")>$h
-		))
+			($h=strpos($out,"<hide>"))!==FALSE &&
+			strpos($out,"</hide>")>$h
+		)
 	) {
 		if($numrun==0) {
 			$out=
@@ -93,9 +110,9 @@
 
 		$hideline = ' <script type="text/javascript">showSHToggle("' . addslashes( wfMsg('showtoc') ) . '","' . addslashes( wfMsg('hidetoc') ) . '",' . $numrun . ')</script>';
 
-		$out=str_replace("__HIDER__","$hideline",$out);
+		$out=rtrim(substr($out,0,strpos($out,"<$act>")),"\n\r") . $hideline . substr($out,strpos($out,"<$act>"));
 		$out=str_replace(
-			array("&lt;$act&gt;",                "&lt;/$act&gt;"),
+			array("<$act>","</$act>"),
 			array("<div id=\"shinside$numrun\">","</div>"),
 			$out
 		);
Personal tools