hostease主机优惠

Mediawiki中文技术论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6024|回复: 0

MediaWiki扩展:内容折叠隐藏

[复制链接]
发表于 2010-12-27 14:36:20 | 显示全部楼层 |阅读模式
Godaddy主机最新优惠码
<showhide> 部分不需要隐藏的文本(通常为标题) __HIDER__ <hide>隐现文本内容</hide> </showhide>

这个折叠隐藏扩展可以制作通过开关进行显示/隐藏的内容元素,表现方式如同表格。

本扩展的创意来自Avala因创建了一系列非对其的国家列表(类似于NATO列表)而受到的指责。一方面,这种列表非常合理,但另一方面又完全不常用。本扩展使得这种列表合理化。

本扩展可以在ShowHide Wikimedia SCG的crash wiki看到实地应用。由于并非mediawiki的内置功能,所以其他wiki网站并不一定能见到本功能;你可以访问测试页面来观察实际例子,以了解和测试其工作表现。

注意,本扩展并未经过深入的测试,并可能存有小错误。目前,它正在Veggie Van Gogh wiki站点上使用。

在Mediawiki中增加本特性的请求参见bug #1257.

语法


本扩展的语法如下:

  1. <showhide>
  2. 部分不需要隐藏的文本(通常为标题) __HIDER__
  3. <hide>隐现文本内容</hide>
  4. </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

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

  31. $wgExtensionFunctions[]="wfShowHideExtension";

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

  36. function ShowHideExtension($in)
  37. {
  38.     global $wgOut;
  39.     static $numrun=0;

  40.     $out=$wgOut->parse($in);
  41.     if(
  42.         strpos($out,"__HIDER__")!==FALSE &&
  43.         ((
  44.             ($s=strpos($out,"<show>"))!==FALSE &&
  45.             strpos($out,"</show>")>$s
  46.         ) || (
  47.             ($h=strpos($out,"<hide>"))!==FALSE &&
  48.             strpos($out,"</hide>")>$h
  49.         ))
  50.     ) {
  51.         if($numrun==0) {
  52.             $out=
  53. "<script type="text/javascript"><!--
  54. shWas=new Array();
  55. function showSHToggle(show,hide,num) {
  56.     if(document.getElementById) {
  57.         document.writeln('<span class=\'toctoggle\'>[<a href="javascript:toggleSH('+num+')" class="internal">' +
  58.         '<span id="showlink'+num+'" style="display:none;">' + show + '</span>' +
  59.         '<span id="hidelink'+num+'">' + hide + '</span>' +
  60.         '</a>]</span>');
  61.     }
  62. }
  63. function toggleSH(num) {
  64.     var shmain = document.getElementById('showhide'+num);
  65.     var sh = document.getElementById('shinside'+num);
  66.     var showlink=document.getElementById('showlink'+num);
  67.     var hidelink=document.getElementById('hidelink'+num);
  68.     if(sh.style.display == 'none') {
  69.         sh.style.display = shWas[num];
  70.         hidelink.style.display='';
  71.         showlink.style.display='none';
  72.         shmain.className = '';
  73.     } else {
  74.         shWas[num] = sh.style.display;
  75.         sh.style.display = 'none';
  76.         hidelink.style.display='none';
  77.         showlink.style.display='';
  78.         shmain.className = 'tochidden';
  79.     }
  80. } // --></script>
  81. ".$out;
  82.         }
  83.         $numrun++;

  84.         if($s!==FALSE)
  85.             $act="show";
  86.         else
  87.             $act="hide";

  88.         $hideline = ' <script type="text/javascript">showSHToggle("' . addslashes( wfMsg('showtoc') ) . '","' . addslashes( wfMsg('hidetoc') ) . '",' . $numrun . ')</script>';

  89.         $out=str_replace("__HIDER__","$hideline",$out);
  90.         $out=str_replace(
  91.             array("<$act>",                "</$act>"),
  92.             array("<div id="shinside$numrun">","</div>"),
  93.             $out
  94.         );
  95.         $out="<span id="showhide$numrun">$out</span>";
  96.         if($act=="hide")
  97.             $out.="<script type="text/javascript">toggleSH($numrun)</script>";
  98.     }
  99.     return $out;
  100. }
  101. ?>
复制代码




v 0.1.1

PHP5 version with corrections according to 根据Smerf's的意见.

    注意:我无法让下面这个版本在MediaWiki 1.5版本上正确运行。下面的内容可以在首次加载时正确工作,但经过cache加载,由$wgOut->addHtml插入的一次性 Javascript代码无法加进去导致了这个问题。前面的v.0.1则在我这里能正常工作。

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

  31. $wgExtensionFunctions[]="wfShowHideExtension";

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

  36. function ShowHideExtension($in)
  37. {
  38.     global $wgOut;
  39.     static $numrun=0;

  40.     $out=$wgOut->parse($in);
  41.     if(
  42.                 strpos($out,"__HIDER__")!==FALSE &&
  43.                 ((
  44.                         ($s=strpos($out,htmlentities("<show>")))!==FALSE &&
  45.                         strpos($out,htmlentities("</show>"))>$s
  46.                 ) || (
  47.                         ($h=strpos($out,htmlentities("<hide>")))!==FALSE &&
  48.                         strpos($out,htmlentities("</hide>"))>$h
  49.                 ))
  50.         ) {
  51.                 if($numrun==0) {
  52.                         $wgOut->addHTML(
  53. "<script type="text/javascript"><!--
  54. shWas=new Array();
  55. function showSHToggle(show,hide,num) {
  56.         if(document.getElementById) {
  57.                 document.writeln('<span class=\'toctoggle\'>[<a href="javascript:toggleSH('+num+')" class="internal">' +
  58.                 '<span id="showlink'+num+'" style="display:none;">' + show + '</span>' +
  59.                 '<span id="hidelink'+num+'">' + hide + '</span>' +
  60.                 '</a>]</span>');
  61.         }
  62. }
  63. function toggleSH(num) {
  64.         var shmain = document.getElementById('showhide'+num);
  65.         var sh = document.getElementById('shinside'+num);
  66.         var showlink=document.getElementById('showlink'+num);
  67.         var hidelink=document.getElementById('hidelink'+num);
  68.         if(sh.style.display == 'none') {
  69.                 sh.style.display = shWas[num];
  70.                 hidelink.style.display='';
  71.                 showlink.style.display='none';
  72.                 shmain.className = '';
  73.         } else {
  74.                 shWas[num] = sh.style.display;
  75.                 sh.style.display = 'none';
  76.                 hidelink.style.display='none';
  77.                 showlink.style.display='';
  78.                 shmain.className = 'tochidden';
  79.         }
  80. } // --></script>
  81. ");
  82.                 }
  83.                 $numrun++;

  84.                 if($s!==FALSE)
  85.                         $act="show";
  86.                 else
  87.                         $act="hide";

  88.                 $hideline = ' <script type="text/javascript">showSHToggle("' . addslashes( wfMsg('showtoc') ) . '","' . addslashes( wfMsg('hidetoc') ) . '",' . $numrun . ')</script>';

  89.                 $out=str_replace("__HIDER__","$hideline",$out);
  90.                 $out=str_replace(
  91.                         array(htmlentities("<$act>"),                htmlentities("</$act>")),
  92.                         array("<div id="shinside$numrun">","</div>"),
  93.                         $out
  94.                 );
  95.         $out="<span id="showhide$numrun">$out</span>";
  96.         if($act=="hide")
  97.             $out.="<script type="text/javascript">toggleSH($numrun)</script>";
  98.     }
  99.     return $out;
  100. }
  101. ?>
复制代码




其他

还有其他的版本,由Austin Che编写。参见 http://austin.mit.edu/mediawiki/showhide.php.txt
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

美国主机评测网站

Archiver|手机版|小黑屋|Mediawiki中文技术论坛

GMT+8, 2024-4-20 16:33 , Processed in 0.033254 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

快速回复 返回顶部 返回列表