MediaWiki扩展:内容折叠隐藏
<showhide> 部分不需要隐藏的文本(通常为标题) __HIDER__ <hide>隐现文本内容</hide> </showhide>这个折叠隐藏扩展可以制作通过开关进行显示/隐藏的内容元素,表现方式如同表格。
本扩展的创意来自Avala因创建了一系列非对其的国家列表(类似于NATO列表)而受到的指责。一方面,这种列表非常合理,但另一方面又完全不常用。本扩展使得这种列表合理化。
本扩展可以在ShowHide Wikimedia SCG的crash wiki看到实地应用。由于并非mediawiki的内置功能,所以其他wiki网站并不一定能见到本功能;你可以访问测试页面来观察实际例子,以了解和测试其工作表现。
注意,本扩展并未经过深入的测试,并可能存有小错误。目前,它正在Veggie Van Gogh wiki站点上使用。
在Mediawiki中增加本特性的请求参见bug #1257.
语法
本扩展的语法如下:
<showhide>
部分不需要隐藏的文本(通常为标题) __HIDER__
<hide>隐现文本内容</hide>
</showhide>
__HIDER__ 是放置隐现开关链接的地方。用户通过点击隐现开关,可显示/隐藏位于<hide></hide>标签之间的内容。 在上面的例子里,可被隐现的内容是“隐现文本内容”,
如果使用<show></show>标签,隐现文本内容将默认处于显示状态,通过点击隐现开关可以把它折叠隐藏起来。
源代码
v 0.1
当忘记html_entity_decode()以及 $wgOut->addHTML(),而仅仅把JS粘贴到$out时,有时可能不能正常工作(在PHP 5.x下测试) --Smerf
请注意,这个扩展有违反XHTML 1.0 Transitional兼容性的地方!! --Smerf
<?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 __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://meta.wikimedia.org/wiki/ShowHide_Extension
$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,"<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;
hidelink.style.display='';
showlink.style.display='none';
shmain.className = '';
} else {
shWas = 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("<$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;
}
?>
v 0.1.1
PHP5 version with corrections according to 根据Smerf's的意见.
注意:我无法让下面这个版本在MediaWiki 1.5版本上正确运行。下面的内容可以在首次加载时正确工作,但经过cache加载,由$wgOut->addHtml插入的一次性 Javascript代码无法加进去导致了这个问题。前面的v.0.1则在我这里能正常工作。
<?php
# WikiMedia 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://meta.wikimedia.org/wiki/ShowHide_Extension
$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;
hidelink.style.display='';
showlink.style.display='none';
shmain.className = '';
} else {
shWas = 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;
}
?>
其他
还有其他的版本,由Austin Che编写。参见 http://austin.mit.edu/mediawiki/showhide.php.txt
页:
[1]