美国虚拟主机Hostease

Mediawiki中文技术论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7302|回复: 0

MediaWiki扩展:语法高亮处理

[复制链接]
发表于 2010-12-24 16:07:54 | 显示全部楼层 |阅读模式
BlueHost主机优惠码
本文取自元维基,欢迎共同翻译、整理与大家共享,促进中文wiki发展^_^

This extension can be seen in action on the Ciscavate.org wiki: [1] The GeSHiHighlight main page is at Ciscavate:MediaWiki_Extensions

The advantages of GeSHiHighlight over GeSHiColor are 1) it handles more langugages, 2) is still being maintained, and 3) seems to work better in general, although I can't vouch for that personally.

(there is a more active-seeming page for GeSHiHilight at: User:Ajqnic:GeSHiHighlight

History (Brief!)

The pre tags in mediawiki are nice, but they don't quite cut it for source code. With that in mind, I went questing for a syntax highlighting extension, and found "SyntaxHighlight" (http://www.wickle.com/wikis/index.php/Syntax_Highlight_extension) by Coffman, but I disagree with the use of two tags--one to specify you are in a code block, and another to specify the language:

  1. <code><perl/> foreach(@myarr){print $_;}</code>
复制代码

To get arround that, I hacked Coffman's 1.2 release to accept language tags:

  1. <perl>foreach(@myarr){print $_;}</perl>
  2. <java>public class foo{ /* .... */ }</java>
复制代码
That version is available here: ...... but I'm not planning on updating it again.

There is another drawback--SyntaxHighlight uses enscript, which (IMO) produces horrible results---better than just pre when dealing with source code, but I want more than comments, strings and keywords to be fontified.

Enter GeSHi.

MediaWiki and GeSHi

GeSHi (found here: http://qbnz.com/highlighter/index.php ) is a syntax highlighing library written in php. I'm new to php (and by new, I mean I wrote my first line of php code about 4 hours prior writing this--see above) and I was amazed at how easy it was to use GeSHi (this says nothing about my love/hate for php in general...).

Why GeSHi?

    * Better highlighting. Flat-out kicks enscript's ass. I'm not fond of the default colors, but that can be dealt with later.
    * php. enscript, being an external program was writing files with md5hash names, which caused update issues when I was testing, command line params changed causing issues, not quite as elegant. (Using an external program was also my first inclination when thinking about this problem--I actually almost discarded GeSHi because of this narrow-mindedness.)
    * It seems to be actively developed. The release I'm using now is only about a week old.

So, where's the source? Here:

  1. <?php
  2. # GeSHiHighlight.php
  3. #
  4. # By: E. Rogan Creswick (aka: Largos)
  5. # creswick@gmail.com
  6. # wiki.ciscavate.org
  7. #
  8. # License: GeSHi Highlight is released under the Gnu Public License (GPL), and comes with no warranties.
  9. # The text of the GPL can be found here: http://www.gnu.org/licenses/gpl.html
  10. # Loosely based on SyntaxHighlight.php by Coffman, (www.wickle.com)

  11. class SyntaxSettings {};

  12. $wgSyntaxSettings = new SyntaxSettings;                                                                                                                                                                                                                                 
  13. $wgExtensionFunctions[] = "wfSyntaxExtension";                                                                                                                 

  14. function wfSyntaxExtension() {                                                                                                                                 
  15.         global $wgParser;
  16.         $langArray = geshi_list_languages("extensions\\geshi\\geshi");
  17. #       $langArray = array("actionscript","ada","apache","asm","asp","bash",
  18. #                          "caddcl","cadlisp","c","cpp","css","delphi",
  19. #                          "html4strict","java","javascript","lisp", "lua",
  20. #                          "nsis","oobas","pascal","perl","php-brief","php",
  21. #                          "python","qbasic","sql","vb","visualfoxpro","xml");

  22.         foreach ( $langArray as $lang ){
  23.              if ($lang == "") continue;                                          
  24.              $wgParser->setHook( $lang,                                                                                                                           
  25.                 create_function( '$text', '$geshi = new GeSHi(rtrim(ltrim($text,"\n\r")), ' . $lang . ', "includes/geshi");
  26.                                         return $geshi->parse_code();'));         
  27.         }                                                                                                                                                      
  28. }        

  29. /**
  30. * function: geshi_list_languages
  31. * -------------------------
  32. * List supported languages by reading the files in the geshi/geshi subdirectory
  33. * (added by JeffK -- Jeff, any more contact info?)  -- I haven't tested the code is is, will do that shortly. -Rogan
  34. *
  35. */
  36. function geshi_list_languages ( $path = 'geshi/' )
  37. {
  38.     if ($handle = opendir($path)) {
  39.        /* Loop over the directory. */
  40.        while (false !== ($file = readdir($handle))) {
  41.            /* Drop the current and parent dir entries */
  42.            if( "." !== $file && ".." !== $file )
  43.            {
  44.            /* Drop files that dont end with .php */
  45.            if( ".php" == substr($file, strrpos($file, "."),4))
  46.            {
  47.                $lang_list .= substr($file, 0, strrpos($file, ".")).",";
  48.            }
  49.            }
  50.        }

  51.        closedir($handle);
  52.     }
  53.     return explode(",", $lang_list);
  54. }                                                                                                                                                      
  55. ?>
复制代码

Yep, that's IT. Bang. pretty syntax highlighting with 28 language tags, for 28 languages (disclaimer: I haven't tested all these, and I have suspicions about html4strict working out of the box, but still.)

Installation

    * Install GeSHi per the GeSHi install instructions (link above)
    * Don't forget to include GeSHi also:  
  1.    include_once("geshi.php")
复制代码
* Either cut & paste the above, or downloade GeSHiHighlight.php here: add file
    * Put GeSHiHighlight.php in your mediaWiki extensions folder
    * Add:  
  1.   include("extensions/GeSHiHighlight.php");
复制代码



      to LocalSettings.php
回复

使用道具 举报

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

本版积分规则

美国HostEase虚拟主机一年198元

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

GMT+8, 2024-11-24 17:50 , Processed in 0.046629 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

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