No comments have been added to this post yet.

Test Php Highlight plugin

朋友Legend基于以前那个老高亮插件全新编写的PHP代码高亮显示插件,特点看来有:

  • 不用替代实体字符(但是代码中间不能有</pre>);
  • 不用使用换行符;
  • 超出行宽进行滚动显示(依靠CSS)
  • ……

目前凭肉眼能看到这些,可能还有一些细节。根据我自己Blog的情况我定义了一点样式,效果如下:

< ?php
/*
Plugin Name: PHP Syntax Highlighter
Plugin URI: http://www.UGiA.cn
Description: highlight u php Syntax
Version: 0.1
Author: legend
Author URI: http://www.ugia.cn/
*/

function highlight_php($code)
{
	// remove empty codes
	if (trim($code) == '')
	{
		return '';
	}

	//remove smilies
	$code = str_replace('"', '"', $code);

	if (!is_array($codefind))
	{
		$codefind1 = array(
			'<br>',		// <br /> to nothing
			'<br />'	// <br /> to nothing
		);
		$codereplace1 = array(
			'',
			''
		);

		$codefind2 = array(
			'&gt;',		// &gt; to >
			'&lt;',		// &lt; to < '&quot;',	// &quot; to ",
			'&amp;',	// &amp; to &
		);
		$codereplace2 = array(
			'>',
			'< ',
			'"',
			'&',
		);
	}

	// remove htmlspecialchars'd bits and excess spacing
	$code = trim(str_replace($codefind1, $codereplace1, $code));
	$code = str_replace($codefind2, $codereplace2, $code); // finish replacements

	// do we have an opening <? tag?
	if (!preg_match('#^\s*<\s?\?#si', $code))
	{
		// if not, replace leading newlines and stuff in a <?php tag and a closing tag at the end
		$code = "<?php BEGIN__PHP_SYNTAX_HIGLIGHTER $code \r\nEND__PHP_SYNTAX_HIGLIGHTER ?>";
		$addedtags = true;
	}
	else
	{
		$addedtags = false;
		$code = preg_replace("/^\s*< \s\?/isU", "<?", $code);
		$code = preg_replace("/<\/>$/isU", "", $code);
	}


	// highlight the string
	$oldlevel = error_reporting(0);
	if (PHP_VERSION  >= '4.2.0')
	{
		$buffer = highlight_string($code, fals);
	}
	else
	{
		@ob_start();
		highlight_string($code);
		$buffer = @ob_get_contents();
		@ob_end_clean();
	}
	error_reporting($oldlevel);

	// if we added tags above, now get rid of them from the resulting string
	if ($addedtags)
	{
		$buffer = preg_replace(array(
			'#(< |&lt;)\?.*php( |&nbsp;)BEGIN__PHP_SYNTAX_HIGLIGHTER #siU',
			'#END__PHP_SYNTAX_HIGLIGHTER( |&nbsp;)\?(>|&gt;)#siU'
		), '', $buffer);
	}
	$buffer = str_replace('[', '&#91;', $buffer);
	$buffer = preg_replace("/< \/?code>/isU", "", $buffer);
	$buffer = preg_replace('/&amp;#([0-9]+);/', '&#$1;', $buffer); // allow unicode entities back through
	$buffer = preg_replace('/<font COLOR="#/i', '<span class="php_', $buffer);
    $buffer = preg_replace('/<\/FONT>/i', '', $buffer);	
	
	$code = &$buffer;

	return $code;
}

if (function_exists('add_filter')) {
    function __php_syntax_hilight($content)
    {
        return preg_replace_callback("/<pre ([^>]*)>(.*?)< \/pre>/is",
                                     '__php_syntax_hilight_callback',
                                     $content);
    }

    function __php_syntax_hilight_callback($match)
    {
        $attr = $match[1];
        $code = $match[2];
         
        $code = str_replace("<br />", "", $code);
        $code = preg_replace("/\s*<p>/s", "\r\n\r\n", $code);
        $code = preg_replace("/< \/p>/s", "", $code);
        $code = str_replace("&#8220;", '"', $code);
        $code = str_replace("&#8221;", '"', $code);
        $code = str_replace("&#8216;", "'", $code);
        $code = str_replace("&#8217;", "'", $code);
        $code = str_replace("&#8211;", "--", $code);
               
        // Try to match the <pre lang="..."> tag, to determine what
        // programming language we need to hilight for,
        
        $re_lang = '/\s+lang\s*=\s*["']?([^"']+)["']?/xi';
        $num = preg_match($re_lang, $attr, $lang);
        if ($num)
        {
            $code = highlight_php($code);
            $attr = preg_replace($re_lang, '', $attr);
            return $lang[1] == "php" ? "<div class="php_code">".$code."</div>" : "&lt;pre$attr>$code&lt;/pre&gt;";
        }
        else
        {
            return "</pre><pre $attr>$code&lt;/pre&gt;";
        }
        //return "</pre><pre $attr>$code&lt;/pre&gt;";
        //return $return;

    }

    add_filter('the_content', '__php_syntax_hilight');
}
?>

以上就是这个插件的源码,你COPY下来保存,传到插件目录激活就可以用了,再定义一点CSS,可以参看我的CSS样式。对了忘了说一点,使用时应该用<pre lang=”php”>标签。

10 Comments

  1. 1 chelseaing Comments @ 10:15:27 on 2004-12-06

    把联想的缩写都放出来,基本上就能看懂了,不过为什么要秀源码出来?不明白.

  2. 2 lemonhall Comments @ 14:01:15 on 2004-12-06

    我十分讨厌这个插件,以前也有安装过。小小的一个功能,竟然需要上白个文件支持,后来我改用IDE输出单独的HTML页面,给个链接就好了。还可以加行号什么的,舒服些。

  3. 3 dark Comments @ 19:12:39 on 2004-12-06

    呵呵,源码就是插件文件呀,COPY下来就可以用了。免得再放出一个文件下载了。

  4. 4 chelseaing Comments @ 10:44:05 on 2004-12-07

    欧买糕的,半懂半不懂的,Dark这里真奇怪,换个电脑看就会出些乱码,有时候一个字大一个字小

  5. 5 lemonhall Comments @ 13:51:40 on 2004-12-07

    唉。。。不好意思,没仔细看POST,原来是一个全新的插件啊。。。以为是WP论坛上提供的那个呢。那个有上百个文件呢。。

  6. 6 dark Comments @ 19:49:55 on 2004-12-07

    To chelseaing:能否说说你的系统及浏览器版本。谢谢!

  7. 7 dark Comments @ 21:12:08 on 2004-12-07

    legend曾为设置这个等宽字体烦恼,嘿嘿,我在Firefox上学到一招,直接把font-family设置为monospace,呵呵,这样你浏览器设置是什么等宽字体就是什么显示,而不必拘泥去指定一系列字体。

  8. 8 chelseaing Comments @ 05:12:42 on 2004-12-08

    XP,IE6,看出来的字体有大有小
    98,IE6, 正常

  9. 9 dark Comments @ 21:16:00 on 2004-12-09

    应该是浏览器默认字体大小所致。你如果用IE你应该在Sidebar(就是首页右边)顶上看到一个图标,这个才是真正要推荐使用的浏览器,注意品味上面的那句话。用Firefox的用户则看不到。
    惨,刚才用IE看了一下,居然没有max-height的效果,一查,IE不支持这个属性,晕。

  10. 10 xinple Comments @ 00:04:11 on 2005-09-21

    我按照你的说明做了,复制代码,放到plugins文件夹, 登陆后台看激活了。

    结果有报错,首先是125行的一个正则表达式错误,单引号是五个,呵呵。prase error

    然后后面有个双引号里面的双引号没有转移。也报错。

    郁闷。

    我刚使用wordpress,想请教你一些问题。
    1、我用默认的code标签包含代码之后,发现其之间的html代码没有屏蔽。这样的话,我要帖出来的html代码显示不了,相反把版面搞乱了。

    2、我以前试过wordpress好像有tags的功能(也可能是记错了),就是发布日志的时候可以填几个tags,可是现在发现没有。不过我看很多其他的wp用户上面是有tags的,是不是安装的插件?

    ps:我看见你wp上面用来显示代码的挺好。不过你这里的代码显示和引用标签是自己改的嘛?还是插件啊? 可以提供出来share一下吗?

    谢谢。