allwiki首页  
天下维客 你可以修改的网络知识库
首页最近更改优秀条目专题展示电脑科技词典软件学习网络知识电脑安全明星时尚天下百科
 

MediaWiki扩展:语法高亮处理

天下维客,你可以修改的网络知识库

Jump to: navigation, search
MediaWiki扩展简明教程 Inputbox帮助文档 使用Spam黑名单 内容折叠隐藏 动态文章列表 指定图片链接
代码高亮处理 预置初始编辑文本 页面访问限制 所见即所得编辑器 QQ在线信息
交流:扩展使用 投票与评分 内容随机展示 wikitex 改进设想 ...更多
加入google地图和51地图标注功能 Ajax五颗星评分插件

本文取自元维基,欢迎共同翻译、整理与大家共享,促进中文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:

 <code><perl/> foreach(@myarr){print $_;}</code>

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

<perl>foreach(@myarr){print $_;}</perl> 
<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:

<?php
# GeSHiHighlight.php
# 
# By: E. Rogan Creswick (aka: Largos)
# creswick@gmail.com
# wiki.ciscavate.org
#
# License: GeSHi Highlight is released under the Gnu Public License (GPL), and comes with no warranties.
# The text of the GPL can be found here: http://www.gnu.org/licenses/gpl.html
# Loosely based on SyntaxHighlight.php by Coffman, (www.wickle.com)
 
class SyntaxSettings {};
 
$wgSyntaxSettings = new SyntaxSettings;                                                                                                                                                                                                                                 
$wgExtensionFunctions[] = "wfSyntaxExtension";                                                                                                                 
 
function wfSyntaxExtension() {                                                                                                                                 
        global $wgParser;
        $langArray = geshi_list_languages("extensions\\geshi\\geshi");
 #       $langArray = array("actionscript","ada","apache","asm","asp","bash",
 #                          "caddcl","cadlisp","c","cpp","css","delphi",
 #                          "html4strict","java","javascript","lisp", "lua",
 #                          "nsis","oobas","pascal","perl","php-brief","php",
 #                          "python","qbasic","sql","vb","visualfoxpro","xml");
 
        foreach ( $langArray as $lang ){
             if ($lang == "") continue;                                           
             $wgParser->setHook( $lang,                                                                                                                           
                create_function( '$text', '$geshi = new GeSHi(rtrim(ltrim($text,"\n\r")), ' . $lang . ', "includes/geshi");
                                        return $geshi->parse_code();'));          
        }                                                                                                                                                      
}        
 
/**
* function: geshi_list_languages
* -------------------------
* List supported languages by reading the files in the geshi/geshi subdirectory
* (added by JeffK -- Jeff, any more contact info?)  -- I haven't tested the code is is, will do that shortly. -Rogan
*
*/
function geshi_list_languages ( $path = 'geshi/' )
{
	if ($handle = opendir($path)) {
	   /* Loop over the directory. */
	   while (false !== ($file = readdir($handle))) {
	       /* Drop the current and parent dir entries */
	       if( "." !== $file && ".." !== $file )
	       {
		   /* Drop files that dont end with .php */
		   if( ".php" == substr($file, strrpos($file, "."),4))
		   {
		       $lang_list .= substr($file, 0, strrpos($file, ".")).",";
		   }
	       }
	   }
 
	   closedir($handle);
	}
	return explode(",", $lang_list);
}                                                                                                                                                      
?>

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:
    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:
    include("extensions/GeSHiHighlight.php");
    to LocalSettings.php


Have fun :)

--Rogan

原文参见: GeSHiHighlight

MediaWiki扩展简明教程 Inputbox帮助文档 使用Spam黑名单 内容折叠隐藏 动态文章列表 指定图片链接
代码高亮处理 预置初始编辑文本 页面访问限制 所见即所得编辑器 QQ在线信息
交流:扩展使用 投票与评分 内容随机展示 wikitex 改进设想 ...更多
加入google地图和51地图标注功能 Ajax五颗星评分插件
mediawiki图标

MediaWiki是全球最著名和最流行的开源wiki程序,运行于PHP+MySQL环境。MediaWiki从2002年2月25日被维基百科全书选用,并有大量其他应用实例。

MediaWiki功能齐备,中文支持良好,且学习资源丰富,是建立wiki网站的首选后台程序。目前国内的天下维客等站点都采用这套系统。

MediaWiki的开发得到维基媒体基金会的支持,一直保值着持续更新。目前最新版本为1.12.x。

mediawiki知识


mediawiki建站

mediawiki资源

国外站需代理访问

→ 更多mediawiki资料  相关资源:wiki知识 wiki文摘  QQ群:22134343 / 3680101-wiki建站与应用 5903157-电脑使用

Personal tools
工具
金银币拍卖 金币拍卖预展  金银币网店 熊猫金银币 生肖金银币